Skip to content

Commit

Permalink
Do not call get_proper_type for traversing type alias: that creates a…
Browse files Browse the repository at this point in the history
… copy preventing union items update.
  • Loading branch information
sterliakov committed Jan 13, 2025
1 parent ee1f4c9 commit 0ceb6ad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion mypy/semanal_typeargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def visit_type_alias_type(self, t: TypeAliasType) -> None:
if not is_error:
# If there was already an error for the alias itself, there is no point in checking
# the expansion, most likely it will result in the same kind of error.
get_proper_type(t).accept(self)
t.alias.target.accept(self)

def visit_tuple_type(self, t: TupleType) -> None:
t.items = flatten_nested_tuples(t.items)
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -1972,3 +1972,19 @@ class D:
class G[Q]:
def g(self, x: Q): ...
d: G[str]

[case testTypeAliasNormalization]
from collections.abc import Callable
from typing import Unpack
from typing_extensions import TypeAlias

type RK_function_args = tuple[float, int]
type RK_functionBIS = Callable[[Unpack[RK_function_args], int], int]

def ff(a: float, b: int, c: int) -> int:
return 2

bis: RK_functionBIS = ff
res: int = bis(1.0, 2, 3)
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]
13 changes: 9 additions & 4 deletions test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -977,13 +977,18 @@ class C(Generic[T]): ...
class D(C[S]): ... # E: Invalid type argument value for "C"

U = TypeVar("U")
A = List[C[U]]
x: A[bytes] # E: Value of type variable "T" of "C" cannot be "bytes"
A = List[C[U]] # E: Type variable "U" not valid as type argument value for "C"
A2 = List[C[T]]
x: A[bytes]
x2: A2[bytes] # E: Value of type variable "T" of "A2" cannot be "bytes"

V = TypeVar("V", bound=int)
V2 = TypeVar("V2", bound=int)
class E(Generic[V]): ...
B = List[E[U]]
y: B[str] # E: Type argument "str" of "E" must be a subtype of "int"
B = List[E[U]] # E: Type argument "U" of "E" must be a subtype of "int"
B2 = List[E[V2]]
y: B[str]
y2: B2[str] # E: Type argument "str" of "B2" must be a subtype of "int"

[case testValidTypeAliasValuesMoreRestrictive]
from typing import TypeVar, Generic, List
Expand Down

0 comments on commit 0ceb6ad

Please sign in to comment.