Skip to content

Commit

Permalink
move testcase to proper file, change its name and use more concise na…
Browse files Browse the repository at this point in the history
…me for variable (#8112)

Followed the discussion in #8107 (comment), refine the code merged in #7662 by:

- move testcase from `pythoneval.test` to `check-namedtuple.test` and rename it from `testNamedTupleAtRunTime` to `testAssignNamedTupleAsAttribute`
- change `s.lvalues[0]` to `lvalue` to make it more concise
  • Loading branch information
TH3CHARLie authored and ilevkivskyi committed Dec 9, 2019
1 parent e8ae122 commit ce0703a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,7 @@ def analyze_namedtuple_assign(self, s: AssignmentStmt) -> bool:
self.is_func_scope())
if not is_named_tuple:
return False
if isinstance(s.lvalues[0], MemberExpr):
if isinstance(lvalue, MemberExpr):
self.fail("NamedTuple type as an attribute is not supported", lvalue)
return False
# Yes, it's a valid namedtuple, but defer if it is not ready.
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -896,3 +896,12 @@ reveal_type(u.field_1) # N: Revealed type is 'typing.Mapping[Tuple[builtins.int
reveal_type(u.field_2) # N: Revealed type is 'Tuple[builtins.int, builtins.int, fallback=__main__.MyBaseTuple]'
reveal_type(u[0]) # N: Revealed type is 'typing.Mapping[Tuple[builtins.int, builtins.int, fallback=__main__.MyBaseTuple], builtins.int]'
reveal_type(u[1]) # N: Revealed type is 'Tuple[builtins.int, builtins.int, fallback=__main__.MyBaseTuple]'

[case testAssignNamedTupleAsAttribute]
from typing import NamedTuple

class A:
def __init__(self) -> None:
self.b = NamedTuple('x', [('s', str), ('n', int)]) # E: NamedTuple type as an attribute is not supported

reveal_type(A().b) # N: Revealed type is 'Any'
12 changes: 0 additions & 12 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1479,18 +1479,6 @@ def f_suppresses() -> int:
_testUnreachableWithStdlibContextManagersNoStrictOptional.py:9: error: Statement is unreachable
_testUnreachableWithStdlibContextManagersNoStrictOptional.py:15: error: Statement is unreachable

[case testNamedTupleAtRunTime]
from typing import NamedTuple

class A:
def __init__(self) -> None:
self.b = NamedTuple('x', [('s', str), ('n', int)])

reveal_type(A().b)
[out]
_testNamedTupleAtRunTime.py:5: error: NamedTuple type as an attribute is not supported
_testNamedTupleAtRunTime.py:7: note: Revealed type is 'Any'

[case testAsyncioFutureWait]
# mypy: strict-optional
from asyncio import Future, wait
Expand Down

0 comments on commit ce0703a

Please sign in to comment.