8000 Enable generic NamedTuples by ilevkivskyi · Pull Request #13396 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Enable generic NamedTuples #13396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 15, 2022
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
Add incremental tests
  • Loading branch information
ilevkivskyi committed Aug 13, 2022
commit 98a637d8dfa9054d3f41c05458a4cd8ec2208501
23 changes: 23 additions & 0 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -5893,3 +5893,26 @@ reveal_type(a.n)
tmp/c.py:4: note: Revealed type is "TypedDict('a.N', {'r': Union[TypedDict('b.M', {'r': Union[..., None], 'x': builtins.int}), None], 'x': builtins.int})"
tmp/c.py:5: error: Incompatible types in assignment (expression has type "Optional[N]", variable has type "int")
tmp/c.py:7: note: Revealed type is "TypedDict('a.N', {'r': Union[TypedDict('b.M', {'r': Union[..., None], 'x': builtins.int}), None], 'x': builtins.int})"

[case testGenericNamedTupleSerialization]
import b
[file a.py]
from typing import NamedTuple, Generic, TypeVar

T = TypeVar("T")
class NT(NamedTuple, Generic[T]):
key: int
value: T

[file b.py]
from a import NT
nt = NT(key=0, value="yes")
s: str = nt.value
[file b.py.2]
from a import NT
nt = NT(key=0, value=42)
s: str = nt.value
[builtins fixtures/tuple.pyi]
[out]
[out2]
tmp/b.py:3: error: Incompatible types in assignment (expression has type "int", variable has type "str")
30 changes: 30 additions & 0 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -3472,6 +3472,36 @@ f(a.x)
[out]
==

[case testNamedTupleUpdateGeneric]
import b
[file a.py]
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
[file a.py.2]
from typing import Generic, TypeVar, NamedTuple

T = TypeVar("T")
class Point(NamedTuple, Generic[T]):
x: int
y: T
[file b.py]
from a import Point
def foo() -> None:
p = Point(x=0, y=1)
i: int = p.y
[file b.py.3]
from a import Point
def foo() -> None:
p = Point(x=0, y="no")
i: int = p.y
[builtins fixtures/tuple.pyi]
[out]
==
==
b.py:4: error: Incompatible types in assignment (expression has type "str", variable has type "int")

[case testNamedTupleUpdateNonRecursiveToRecursiveFine]
# flags: --enable-recursive-aliases
import c
Expand Down
0