Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updated tests to provide note to use None guard when accessing attrib…
…utes on union types
  • Loading branch information
angelawuuu committed Apr 24, 2024
commit a65ac35b1571407037c5f290df9c4a8b3846c3dd
6 changes: 4 additions & 2 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -2828,15 +2828,17 @@ class C:
a = None # E: Need type annotation for "a" (hint: "a: Optional[<type>] = ...")

def f(self, x) -> None:
C.a.y # E: Item "None" of "Optional[Any]" has no attribute "y"
C.a.y # E: Item "None" of "Optional[Any]" has no attribute "y" \
# N: You can use "if C.a is not None" to guard against a None value

[case testLocalPartialTypesAccessPartialNoneAttribute2]
# flags: --local-partial-types
class C:
a = None # E: Need type annotation for "a" (hint: "a: Optional[<type>] = ...")

def f(self, x) -> None:
self.a.y # E: Item "None" of "Optional[Any]" has no attribute "y"
self.a.y # E: Item "None" of "Optional[Any]" has no attribute "y" \
# N: You can use "if self.a is not None" to guard against a None value

-- Special case for assignment to '_'
-- ----------------------------------
Expand Down
6 changes: 4 additions & 2 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@ if int():
if int():
x = f('x') # E: Argument 1 to "f" has incompatible type "str"; expected "int"

x.x = 1 # E: Item "None" of "Optional[Node[int]]" has no attribute "x"
x.x = 1 # E: Item "None" of "Optional[Node[int]]" has no attribute "x" \
# N: You can use "if x is not None" to guard against a None value
if x is not None:
x.x = 1 # OK here

Expand Down Expand Up @@ -617,7 +618,8 @@ A = None # type: Any
class C(A):
pass
x = None # type: Optional[C]
x.foo() # E: Item "None" of "Optional[C]" has no attribute "foo"
x.foo() # E: Item "None" of "Optional[C]" has no attribute "foo" \
# N: You can use "if x is not None" to guard against a None value

[case testIsinstanceAndOptionalAndAnyBase]
from typing import Any, Optional
Expand Down
6 changes: 4 additions & 2 deletions test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,8 @@ a: Any
d: Dict[str, Tuple[List[Tuple[str, str]], str]]
x, _ = d.get(a, (None, None))

for y in x: pass # E: Item "None" of "Optional[List[Tuple[str, str]]]" has no attribute "__iter__" (not iterable)
for y in x: pass # E: Item "None" of "Optional[List[Tuple[str, str]]]" has no attribute "__iter__" (not iterable) \
# N: You can use "if x is not None" to guard against a None value
if x:
for s, t in x:
reveal_type(s) # N: Revealed type is "builtins.str"
Expand All @@ -950,7 +951,8 @@ x = None
d: Dict[str, Tuple[List[Tuple[str, str]], str]]
x, _ = d.get(a, (None, None))

for y in x: pass # E: Item "None" of "Optional[List[Tuple[str, str]]]" has no attribute "__iter__" (not iterable)
for y in x: pass # E: Item "None" of "Optional[List[Tuple[str, str]]]" has no attribute "__iter__" (not iterable) \
# N: You can use "if x is not None" to guard against a None value
if x:
for s, t in x:
reveal_type(s) # N: Revealed type is "builtins.str"
Expand Down