---
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
---
UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of the `type` keyword
 --> UP040.py:5:1
  |
4 | # UP040
5 | x: typing.TypeAlias = int
  | ^^^^^^^^^^^^^^^^^^^^^^^^^
6 | x: TypeAlias = int
  |
help: Use the `type` keyword
  |
4 | # UP040
  - x: typing.TypeAlias = int
5 + type x = int
6 | x: TypeAlias = int
  |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of the `type` keyword
 --> UP040.py:6:1
  |
4 | # UP040
5 | x: typing.TypeAlias = int
6 | x: TypeAlias = int
  | ^^^^^^^^^^^^^^^^^^
7 |
8 | # UP040 simple generic
  |
help: Use the `type` keyword
  |
5 | x: typing.TypeAlias = int
  - x: TypeAlias = int
6 + type x = int
7 |
  |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of the `type` keyword
  --> UP040.py:10:1
   |
 8 | # UP040 simple generic
 9 | T = typing.TypeVar["T"]
10 | x: typing.TypeAlias = list[T]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11 |
12 | # UP040 call style generic
   |
help: Use the `type` keyword
   |
9  | T = typing.TypeVar["T"]
   - x: typing.TypeAlias = list[T]
10 + type x[T] = list[T]
11 |
   |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of the `type` keyword
  --> UP040.py:14:1
   |
12 | # UP040 call style generic
13 | T = typing.TypeVar("T")
14 | x: typing.TypeAlias = list[T]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15 |
16 | # UP040 bounded generic
   |
help: Use the `type` keyword
   |
13 | T = typing.TypeVar("T")
   - x: typing.TypeAlias = list[T]
14 + type x[T] = list[T]
15 |
   |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of the `type` keyword
  --> UP040.py:18:1
   |
16 | # UP040 bounded generic
17 | T = typing.TypeVar("T", bound=int)
18 | x: typing.TypeAlias = list[T]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19 |
20 | # UP040 constrained generic
   |
help: Use the `type` keyword
   |
17 | T = typing.TypeVar("T", bound=int)
   - x: typing.TypeAlias = list[T]
18 + type x[T: int] = list[T]
19 |
   |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of the `type` keyword
  --> UP040.py:22:1
   |
20 | # UP040 constrained generic
21 | T = typing.TypeVar("T", int, str)
22 | x: typing.TypeAlias = list[T]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23 |
24 | # UP040 contravariant generic
   |
help: Use the `type` keyword
   |
21 | T = typing.TypeVar("T", int, str)
   - x: typing.TypeAlias = list[T]
22 + type x[T: (int, str)] = list[T]
23 |
   |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of the `type` keyword
  --> UP040.py:26:1
   |
24 | # UP040 contravariant generic
25 | T = typing.TypeVar("T", contravariant=True)
26 | x: typing.TypeAlias = list[T]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27 |
28 | # UP040 covariant generic
   |
help: Use the `type` keyword
   |
25 | T = typing.TypeVar("T", contravariant=True)
   - x: typing.TypeAlias = list[T]
26 + type x[T] = list[T]
27 |
   |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of the `type` keyword
  --> UP040.py:30:1
   |
28 | # UP040 covariant generic
29 | T = typing.TypeVar("T", covariant=True)
30 | x: typing.TypeAlias = list[T]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31 |
32 | # UP040 in class scope
   |
help: Use the `type` keyword
   |
29 | T = typing.TypeVar("T", covariant=True)
   - x: typing.TypeAlias = list[T]
30 + type x[T] = list[T]
31 |
   |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of the `type` keyword
  --> UP040.py:36:5
   |
34 | class Foo:
35 |     # reference to global variable
36 |     x: typing.TypeAlias = list[T]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37 |
38 |     # reference to class variable
   |
help: Use the `type` keyword
   |
35 |     # reference to global variable
   -     x: typing.TypeAlias = list[T]
36 +     type x[T] = list[T]
37 |
   |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `y` uses `TypeAlias` annotation instead of the `type` keyword
  --> UP040.py:40:5
   |
38 |     # reference to class variable
39 |     TCLS = typing.TypeVar["TCLS"]
40 |     y: typing.TypeAlias = list[TCLS]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41 |
42 | # UP040 won't add generics in fix
   |
help: Use the `type` keyword
   |
39 |     TCLS = typing.TypeVar["TCLS"]
   -     y: typing.TypeAlias = list[TCLS]
40 +     type y[TCLS] = list[TCLS]
41 |
   |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of the `type` keyword
  --> UP040.py:44:1
   |
42 | # UP040 won't add generics in fix
43 | T = typing.TypeVar(*args)
44 | x: typing.TypeAlias = list[T]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
45 |
46 | # `default` was added in Python 3.13
   |
help: Use the `type` keyword
   |
43 | T = typing.TypeVar(*args)
   - x: typing.TypeAlias = list[T]
44 + type x = list[T]
45 |
   |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `Decorator` uses `TypeAlias` annotation instead of the `type` keyword
  --> UP040.py:57:1
   |
55 | # type alias.
56 | T = typing.TypeVar["T"]
57 | Decorator: TypeAlias = typing.Callable[[T], T]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Use the `type` keyword
   |
56 | T = typing.TypeVar["T"]
   - Decorator: TypeAlias = typing.Callable[[T], T]
57 + type Decorator[T] = typing.Callable[[T], T]
58 |
   |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `PositiveList` uses `TypeAliasType` assignment instead of the `type` keyword
  --> UP040.py:67:1
   |
65 |   # https://github.com/astral-sh/ruff/issues/11422
66 |   T = TypeVar("T")
67 | / PositiveList = TypeAliasType(
68 | |     "PositiveList", list[Annotated[T, Gt(0)]], type_params=(T,)
69 | | )
   | |_^
70 |
71 |   # Bound
   |
help: Use the `type` keyword
   |
66 | T = TypeVar("T")
   - PositiveList = TypeAliasType(
   -     "PositiveList", list[Annotated[T, Gt(0)]], type_params=(T,)
   - )
67 + type PositiveList[T] = list[Annotated[T, Gt(0)]]
68 |
   |

UP040 [*] Type alias `PositiveList` uses `TypeAliasType` assignment instead of the `type` keyword
  --> UP040.py:73:1
   |
71 |   # Bound
72 |   T = TypeVar("T", bound=SupportGt)
73 | / PositiveList = TypeAliasType(
74 | |     "PositiveList", list[Annotated[T, Gt(0)]], type_params=(T,)
75 | | )
   | |_^
76 |
77 |   # Multiple bounds
   |
help: Use the `type` keyword
   |
72 | T = TypeVar("T", bound=SupportGt)
   - PositiveList = TypeAliasType(
   -     "PositiveList", list[Annotated[T, Gt(0)]], type_params=(T,)
   - )
73 + type PositiveList[T: SupportGt] = list[Annotated[T, Gt(0)]]
74 |
   |

UP040 [*] Type alias `Tuple3` uses `TypeAliasType` assignment instead of the `type` keyword
  --> UP040.py:81:1
   |
79 | T2 = TypeVar("T2")
80 | T3 = TypeVar("T3")
81 | Tuple3 = TypeAliasType("Tuple3", tuple[T1, T2, T3], type_params=(T1, T2, T3))
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
82 |
83 | # No type_params
   |
help: Use the `type` keyword
   |
80 | T3 = TypeVar("T3")
   - Tuple3 = TypeAliasType("Tuple3", tuple[T1, T2, T3], type_params=(T1, T2, T3))
81 + type Tuple3[T1: SupportGt, T2, T3] = tuple[T1, T2, T3]
82 |
   |

UP040 [*] Type alias `PositiveInt` uses `TypeAliasType` assignment instead of the `type` keyword
  --> UP040.py:84:1
   |
83 | # No type_params
84 | PositiveInt = TypeAliasType("PositiveInt", Annotated[int, Gt(0)])
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85 | PositiveInt = TypeAliasType("PositiveInt", Annotated[int, Gt(0)], type_params=())
   |
help: Use the `type` keyword
   |
83 | # No type_params
   - PositiveInt = TypeAliasType("PositiveInt", Annotated[int, Gt(0)])
84 + type PositiveInt = Annotated[int, Gt(0)]
85 | PositiveInt = TypeAliasType("PositiveInt", Annotated[int, Gt(0)], type_params=())
   |

UP040 [*] Type alias `PositiveInt` uses `TypeAliasType` assignment instead of the `type` keyword
  --> UP040.py:85:1
   |
83 | # No type_params
84 | PositiveInt = TypeAliasType("PositiveInt", Annotated[int, Gt(0)])
85 | PositiveInt = TypeAliasType("PositiveInt", Annotated[int, Gt(0)], type_params=())
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86 |
87 | # OK: Other name
   |
help: Use the `type` keyword
   |
84 | PositiveInt = TypeAliasType("PositiveInt", Annotated[int, Gt(0)])
   - PositiveInt = TypeAliasType("PositiveInt", Annotated[int, Gt(0)], type_params=())
85 + type PositiveInt = Annotated[int, Gt(0)]
86 |
   |

UP040 [*] Type alias `AnyList` uses `TypeAliasType` assignment instead of the `type` keyword
  --> UP040.py:95:1
   |
93 | # `default` was added in Python 3.13
94 | T = typing.TypeVar("T", default=Any)
95 | AnyList = TypeAliasType("AnyList", list[T], type_params=(T,))
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
96 |
97 | # unsafe fix if comments within the fix
   |
help: Use the `type` keyword
   |
94 | T = typing.TypeVar("T", default=Any)
   - AnyList = TypeAliasType("AnyList", list[T], type_params=(T,))
95 + type AnyList[T = Any] = list[T]
96 |
   |

UP040 [*] Type alias `PositiveList` uses `TypeAliasType` assignment instead of the `type` keyword
   --> UP040.py:99:1
    |
 97 |   # unsafe fix if comments within the fix
 98 |   T = TypeVar("T")
 99 | / PositiveList = TypeAliasType(  # eaten comment
100 | |     "PositiveList", list[Annotated[T, Gt(0)]], type_params=(T,)
101 | | )
    | |_^
102 |
103 |   T = TypeVar("T")
    |
help: Use the `type` keyword
    |
98  | T = TypeVar("T")
    - PositiveList = TypeAliasType(  # eaten comment
    -     "PositiveList", list[Annotated[T, Gt(0)]], type_params=(T,)
    - )
99  + type PositiveList[T] = list[Annotated[T, Gt(0)]]
100 |
    |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `PositiveList` uses `TypeAliasType` assignment instead of the `type` keyword
   --> UP040.py:104:1
    |
103 |   T = TypeVar("T")
104 | / PositiveList = TypeAliasType(
105 | |     "PositiveList", list[Annotated[T, Gt(0)]], type_params=(T,)
106 | | ) # this comment should be okay
    | |_^
    |
help: Use the `type` keyword
    |
103 | T = TypeVar("T")
    - PositiveList = TypeAliasType(
    -     "PositiveList", list[Annotated[T, Gt(0)]], type_params=(T,)
    - ) # this comment should be okay
104 + type PositiveList[T] = list[Annotated[T, Gt(0)]] # this comment should be okay
105 |
    |

UP040 [*] Type alias `PositiveList` uses `TypeAliasType` assignment instead of the `type` keyword
   --> UP040.py:111:1
    |
109 |   # this comment will actually be preserved because it's inside the "value" part
110 |   T = TypeVar("T")
111 | / PositiveList = TypeAliasType(
112 | |     "PositiveList", list[
113 | |         Annotated[T, Gt(0)],  # preserved comment
114 | |     ], type_params=(T,)
115 | | )
    | |_^
116 |
117 |   T: TypeAlias = (
    |
help: Use the `type` keyword
    |
110 | T = TypeVar("T")
    - PositiveList = TypeAliasType(
    -     "PositiveList", list[
111 + type PositiveList[T] = list[
112 |         Annotated[T, Gt(0)],  # preserved comment
    -     ], type_params=(T,)
    - )
113 +     ]
114 |
    |

UP040 [*] Type alias `T` uses `TypeAlias` annotation instead of the `type` keyword
   --> UP040.py:117:1
    |
115 |   )
116 |
117 | / T: TypeAlias = (
118 | |     int
119 | |     | str
120 | | )
    | |_^
121 |
122 |   T: TypeAlias = ( # comment0
    |
help: Use the `type` keyword
    |
116 |
    - T: TypeAlias = (
117 + type T = (
118 |     int
    |
note: This is an unsafe fix and may change runtime behavior

UP040 [*] Type alias `T` uses `TypeAlias` annotation instead of the `type` keyword
   --> UP040.py:122:1
    |
120 |   )
121 |
122 | / T: TypeAlias = ( # comment0
123 | |     # comment1
124 | |     int  # comment2
125 | |     # comment3
126 | |     | # comment4
127 | |     # comment5
128 | |     str  # comment6
129 | |     # comment7
130 | | ) # comment8
    | |_^
131 |
132 |   # Test case for TypeVar with default - should be converted when preview mode is enabled
    |
help: Use the `type` keyword
    |
121 |
    - T: TypeAlias = ( # comment0
122 + type T = ( # comment0
123 |     # comment1
    |
note: This is an unsafe fix and may change runtime behavior
