Skip to content

Commit

Permalink
Fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Jul 29, 2023
1 parent f1441cf commit ddb2e52
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions mypy/semanal_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def check_namedtuple_classdef(
default_items: dict[str, Expression] = {}
statements: list[Statement] = []
for stmt in defn.defs.body:
statements.append(stmt)
if not isinstance(stmt, AssignmentStmt):
# Still allow pass or ... (for empty namedtuples).
if isinstance(stmt, PassStmt) or (
Expand All @@ -163,18 +164,17 @@ def check_namedtuple_classdef(
continue
# And nested classes, they need to be analyzed further:
if isinstance(stmt, ClassDef):
statements.append(stmt)
continue

statements.pop()
defn.removed_statements.append(stmt)
self.fail(NAMEDTUP_CLASS_ERROR, stmt)
elif len(stmt.lvalues) > 1 or not isinstance(stmt.lvalues[0], NameExpr):
# An assignment, but an invalid one.
statements.pop()
defn.removed_statements.append(stmt)
self.fail(NAMEDTUP_CLASS_ERROR, stmt)
else:
# Append name and type in this case...
statements.append(stmt)
name = stmt.lvalues[0].name
items.append(name)
if stmt.type is None:
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-class-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,18 @@ class A(NamedTuple):
class B(NamedTuple):
x: str
y: int = 1
def method(self) -> int: ...

# Correct:
A(1)
A.B('a')
A.B('a', 2)

b: A.B
reveal_type(b.x) # N: Revealed type is "builtins.str"
reveal_type(b.y) # N: Revealed type is "builtins.int"
reveal_type(b.method()) # N: Revealed type is "builtins.int"

# Incorrect:
A.B() # E: Missing positional argument "x" in call to "B"
A.B(1, 'a') # E: Argument 1 to "B" has incompatible type "int"; expected "str" \
Expand Down

0 comments on commit ddb2e52

Please sign in to comment.