Skip to content

Allow better tuple type aliases, fix "Type application" error #12134

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3242,7 +3242,11 @@ def apply_type_arguments_to_callable(
tp = get_proper_type(tp)

if isinstance(tp, CallableType):
if len(tp.variables) != len(args):
if tp.is_type_obj() and tp.type_object().fullname == 'builtins.tuple':
# We special case `tuple` callable, because it can accept
# a different amount of args: `tuple[int]`, `tuple[int, str, str]`
return tp
elif len(tp.variables) != len(args):
self.msg.incompatible_type_application(len(tp.variables),
len(args), ctx)
return AnyType(TypeOfAny.from_error)
Expand Down
17 changes: 12 additions & 5 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2883,11 +2883,18 @@ def analyze_name_lvalue(self,
lvalue.fullname = var._fullname
else:
lvalue.fullname = lvalue.name
if self.is_func_scope():
if unmangle(name) == '_':
# Special case for assignment to local named '_': always infer 'Any'.
typ = AnyType(TypeOfAny.special_form)
self.store_declared_types(lvalue, typ)

# Special case for assignment to local named '_': always infer 'Any'.
is_special_unused = self.is_func_scope() and unmangle(name) == '_'

# Special case, `Any` is defined as `= object()` in typeshed,
# so we need to make it a real type variable.
is_typing_any = var.fullname == 'typing.Any'

if is_special_unused or is_typing_any:
typ = AnyType(TypeOfAny.special_form)
self.store_declared_types(lvalue, typ)

if is_final and self.is_final_redefinition(kind, name):
self.fail("Cannot redefine an existing name as final", lvalue)
else:
Expand Down
23 changes: 23 additions & 0 deletions test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,29 @@ A: TypeAlias = str
[builtins fixtures/bool.pyi]
[out]

[case testTupleWithDifferentArgsPy38]
# flags: --python-version 3.8
NotYet1 = tuple[float] # E: "tuple" is not subscriptable
NotYet2 = tuple[float, float] # E: "tuple" is not subscriptable
[builtins fixtures/tuple.pyi]

[case testTupleWithDifferentArgsStub]
# https://github.com/python/mypy/issues/11098
import tup
[file tup.pyi]
Correct1 = str | tuple[float, float, str]
Correct2 = tuple[float] | str
Correct3 = tuple[float, ...] | str

RHSAlias1: type = tuple[int, int]
RHSAlias2: type = tuple[int]
RHSAlias3: type = tuple[int, ...]

# Wrong:

WrongTypeElement = str | tuple[float, 1] # E: Invalid type: try using Literal[1] instead?
WrongEllipsis = str | tuple[float, float, ...] # E: Unexpected "..."
[builtins fixtures/tuple.pyi]

[case testLiteralStringPep675]
# flags: --python-version 3.11
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1603,3 +1603,21 @@ class Foo(Enum):
_testEnumValueWithPlaceholderNodeType.py:5: error: Incompatible types in assignment (expression has type "object", variable has type "Foo")
_testEnumValueWithPlaceholderNodeType.py:6: error: Incompatible types in assignment (expression has type "object", variable has type "Foo")
_testEnumValueWithPlaceholderNodeType.py:6: error: Name "Missing" is not defined

[case testTupleWithDifferentArgsPy310]
# https://github.com/python/mypy/issues/11098
# flags: --python-version 3.10
Correct1 = str | tuple[float, float, str]
Correct2 = tuple[float] | str
Correct3 = tuple[float, ...] | str

RHSAlias1: type = tuple[int, int]
RHSAlias2: type = tuple[int]
RHSAlias3: type = tuple[int, ...]

# Wrong:
WrongTypeElement = str | tuple[float, 1]
WrongEllipsis = tuple[float, float, ...] | str
[out]
_testTupleWithDifferentArgsPy310.py:12: error: Invalid type: try using Literal[1] instead?
_testTupleWithDifferentArgsPy310.py:13: error: Unexpected "..."