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 generic alias test
  • Loading branch information
ilevkivskyi committed Aug 13, 2022
commit 41fab831f57c3da68ba15008493430487e5dc025
3 changes: 3 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3640,6 +3640,9 @@ def visit_type_application(self, tapp: TypeApplication) -> Type:
if isinstance(item, Instance):
tp = type_object_type(item.type, self.named_type)
return self.apply_type_arguments_to_callable(tp, item.args, tapp)
elif isinstance(item, TupleType) and item.partial_fallback.type.is_named_tuple:
tp = type_object_type(item.partial_fallback.type, self.named_type)
return self.apply_type_arguments_to_callable(tp, item.partial_fallback.args, tapp)
else:
self.chk.fail(message_registry.ONLY_CLASS_APPLICATION, tapp)
return AnyType(TypeOfAny.from_error)
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,22 @@ NT[str](key=0, value=0) # E: Argument "value" to "NT" has incompatible type "in
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]

[case testGenericNamedTupleAlias]
from typing import NamedTuple, Generic, TypeVar, List

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

Alias = NT[List[T]]

an: Alias[str]
reveal_type(an) # N: Revealed type is "Tuple[builtins.int, builtins.list[builtins.str], fallback=__main__.NT[builtins.list[builtins.str]]]"
Alias[str](key=0, value=0) # E: Argument "value" to "NT" has incompatible type "int"; expected "List[str]"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]

[case testGenericNamedTupleMethods]
from typing import Generic, NamedTuple, TypeVar

Expand Down
0