8000 Prevent crash with Unpack of a fixed tuple in PEP695 type alias by sterliakov · Pull Request #18451 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Prevent crash with Unpack of a fixed tuple in PEP695 type alias #18451

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 5 commits into from
Jan 15, 2025
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
Next Next commit
Do not call get_proper_type for traversing type alias: that creates a…
… copy preventing union items update.
  • Loading branch information
sterliakov committed Jan 13, 2025
commit 0ceb6ada277292012b333d4923ceea89eb5f8227
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
Loading
0