crates/ruff_linter/resources/mdtest/pyupgrade/pep-695.md
UP040, UP046, UP047)target-version = "py313"
[lint]
preview = true
select = [
"non-pep695-type-alias",
"non-pep695-generic-class",
"non-pep695-generic-function",
]
TypeVar before a non-defaulted oneIn a PEP 695 type parameter list, a non-defaulted type parameter cannot follow a defaulted one, and reordering the parameters would change how positional arguments bind when subscripting the alias, class, or function. There is no valid, equivalent type parameter list in this case, so no diagnostic is emitted (see #27021).
non-pep695-type-alias (UP040)TypeAlias annotationfrom typing import TypeAlias, TypeVar
T = TypeVar("T", default=int)
S = TypeVar("S")
Pair: TypeAlias = tuple[T, S]
TypeAliasType callfrom typing import TypeAliasType, TypeVar
T = TypeVar("T", default=int)
S = TypeVar("S")
Pair = TypeAliasType("Pair", tuple[T, S], type_params=(T, S))
TypeVar comes firstfrom typing import TypeAlias, TypeVar
T = TypeVar("T", default=int)
S = TypeVar("S")
Pair: TypeAlias = tuple[S, T] # snapshot: non-pep695-type-alias
error[UP040]: Type alias `Pair` uses `TypeAlias` annotation instead of the `type` keyword
--> src/mdtest_snippet.py:6:1
|
6 | Pair: TypeAlias = tuple[S, T] # snapshot: non-pep695-type-alias
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: Use the `type` keyword
|
5 |
- Pair: TypeAlias = tuple[S, T] # snapshot: non-pep695-type-alias
6 + type Pair[S, T = int] = tuple[S, T] # snapshot: non-pep695-type-alias
|
note: This is an unsafe fix and may change runtime behavior
from typing import TypeAlias, TypeVar
T = TypeVar("T", default=int)
S = TypeVar("S", default=str)
Pair: TypeAlias = tuple[T, S] # error: [non-pep695-type-alias]
non-pep695-generic-class (UP046)TypeVar precedes a non-defaulted onefrom typing import Generic, TypeVar
T = TypeVar("T", default=int)
S = TypeVar("S")
class Pair(Generic[T, S]):
t: T
s: S
TypeVar comes firstfrom typing import Generic, TypeVar
T = TypeVar("T", default=int)
S = TypeVar("S")
class Pair(Generic[S, T]): # snapshot: non-pep695-generic-class
t: T
s: S
error[UP046]: Generic class `Pair` uses `Generic` subclass instead of type parameters
--> src/mdtest_snippet.py:6:12
|
6 | class Pair(Generic[S, T]): # snapshot: non-pep695-generic-class
| ^^^^^^^^^^^^^
help: Use type parameters
|
5 |
- class Pair(Generic[S, T]): # snapshot: non-pep695-generic-class
6 + class Pair[S, T = int]: # snapshot: non-pep695-generic-class
7 | t: T
|
note: This is an unsafe fix and may change runtime behavior
non-pep695-generic-function (UP047)TypeVar precedes a non-defaulted onefrom typing import TypeVar
T = TypeVar("T", default=int)
S = TypeVar("S")
def pair(t: T, s: S) -> tuple[T, S]:
return (t, s)
TypeVar comes firstfrom typing import TypeVar
T = TypeVar("T", default=int)
S = TypeVar("S")
def pair(s: S, t: T) -> tuple[S, T]: # snapshot: non-pep695-generic-function
return (s, t)
error[UP047]: Generic function `pair` should use type parameters
--> src/mdtest_snippet.py:6:5
|
6 | def pair(s: S, t: T) -> tuple[S, T]: # snapshot: non-pep695-generic-function
| ^^^^^^^^^^^^^^^^
help: Use type parameters
|
5 |
- def pair(s: S, t: T) -> tuple[S, T]: # snapshot: non-pep695-generic-function
6 + def pair[S, T = int](s: S, t: T) -> tuple[S, T]: # snapshot: non-pep695-generic-function
7 | return (s, t)
|
note: This is an unsafe fix and may change runtime behavior