Skip to content

Commit be7bc52

Browse files
committed
Disallow implicitly generic aliases when using PEP 613 explicit aliases
1 parent 21587f0 commit be7bc52

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

mypy/semanal.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,15 +2765,15 @@ def get_declared_metaclass(
27652765
return None, True, False # defer later in the caller
27662766

27672767
# Support type aliases, like `_Meta: TypeAlias = type`
2768+
metaclass_info: Node | None = sym.node
27682769
if (
27692770
isinstance(sym.node, TypeAlias)
2770-
and sym.node.no_args
2771-
and isinstance(sym.node.target, ProperType)
2772-
and isinstance(sym.node.target, Instance)
2771+
and not sym.node.python_3_12_type_alias
2772+
and not sym.node.alias_tvars
27732773
):
2774-
metaclass_info: Node | None = sym.node.target.type
2775-
else:
2776-
metaclass_info = sym.node
2774+
target = get_proper_type(sym.node.target)
2775+
if isinstance(target, Instance):
2776+
metaclass_info = target.type
27772777

27782778
if not isinstance(metaclass_info, TypeInfo) or metaclass_info.tuple_type is not None:
27792779
self.fail(f'Invalid metaclass "{metaclass_name}"', metaclass_expr)
@@ -4040,6 +4040,7 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
40404040
and not res.args
40414041
and not empty_tuple_index
40424042
and not pep_695
4043+
and not pep_613
40434044
)
40444045
if isinstance(res, ProperType) and isinstance(res, Instance):
40454046
if not validate_instance(res, self.fail, empty_tuple_index):

test-data/unit/check-type-aliases.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,3 +1238,15 @@ from typing import List, Union
12381238
A = Union[int, List[A]]
12391239
def func(x: A) -> int: ...
12401240
[builtins fixtures/tuple.pyi]
1241+
1242+
[case testAliasExplicitNoArgsNotGeneric]
1243+
from typing import List, assert_type
1244+
from typing_extensions import TypeAlias
1245+
1246+
Implicit = List
1247+
Explicit: TypeAlias = List
1248+
1249+
x1: Implicit[str]
1250+
x2: Explicit[str] # E: Bad number of arguments for type alias, expected 0, given 1
1251+
assert_type(x1, List[str])
1252+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)