Skip to content
Merged
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
Fix regular tuple bases and newtypes
  • Loading branch information
ilevkivskyi committed Aug 9, 2022
commit 68263eeb68feb6e9e120cf3d66ba93e774325006
7 changes: 7 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1841,6 +1841,13 @@ def configure_tuple_base_class(self, defn: ClassDef, base: TupleType) -> Instanc
self.fail("Class has two incompatible bases derived from tuple", defn)
defn.has_incompatible_baseclass = True
info.tuple_type = base
[]))
if not info.tuple_alias:
info.tuple_alias = TypeAlias(target, info.fullname, info.line, info.column)
else:
if has_placeholder(info.tuple_alias.target):
self.defer(force_progress=True)
info.tuple_alias.target = target

if base.partial_fallback.type.fullname == "builtins.tuple":
# Fallback can only be safely calculated after semantic analysis, since base
Expand Down
1 change: 1 addition & 0 deletions mypy/semanal_newtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def process_newtype_declaration(self, s: AssignmentStmt) -> bool:
name, old_type, old_type.partial_fallback, s.line
)
newtype_class_info.tuple_type = old_type
newtype_class_info.tuple_alias = old_type.partial_fallback.type.tuple_alias
elif isinstance(old_type, Instance):
if old_type.type.is_protocol:
self.fail("NewType cannot be used with protocol classes", s)
Expand Down
15 changes: 8 additions & 7 deletions test-data/unit/check-recursive-types.test
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ class B(Tuple[B, int]):
x: int

b, _ = x
reveal_type(b.x)
reveal_type(b.x) # N:Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

[case testBasicTupleClassesNewType]
Expand All @@ -492,17 +492,18 @@ from typing import Tuple, NamedTuple, NewType
x: C
class B(Tuple[B, int]):
x: int
C = NewType(B)
C = NewType("C", B)
b, _ = x
reveal_type(b.x)
reveal_type(b) # N: Revealed type is "Tuple[..., builtins.int, fallback=__main__.B]"
reveal_type(b.x) # N: Revealed type is "builtins.int"

y: BNT
y: CNT
class BNT(NamedTuple):
x: BNT
x: CNT
y: int
CNT = NewType(BNT)
CNT = NewType("CNT", BNT)
bnt, _ = y
reveal_type(bnt.y)
reveal_type(bnt.y) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

-- TODO: Add more fine-grained tests for various kinds of recursive types
Expand Down