---
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
---
UP045 [*] Use `X | None` for type annotations
 --> UP045_1.py:5:10
  |
5 | def f(x: Optional[str]) -> None:
  |          ^^^^^^^^^^^^^
6 |     ...
  |
help: Convert to `X | None`
  |
4 |
  - def f(x: Optional[str]) -> None:
5 + def f(x: str | None) -> None:
6 |     ...
  |

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:9:10
   |
 9 | def f(x: typing.Optional[str]) -> None:
   |          ^^^^^^^^^^^^^^^^^^^^
10 |     ...
   |
help: Convert to `X | None`
   |
8  |
   - def f(x: typing.Optional[str]) -> None:
9  + def f(x: str | None) -> None:
10 |     ...
   |

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:14:8
   |
13 | def f() -> None:
14 |     x: Optional[str]
   |        ^^^^^^^^^^^^^
15 |     x = Optional[str]
   |
help: Convert to `X | None`
   |
13 | def f() -> None:
   -     x: Optional[str]
14 +     x: str | None
15 |     x = Optional[str]
   |

UP045 Use `X | None` for type annotations
  --> UP045_1.py:15:9
   |
13 | def f() -> None:
14 |     x: Optional[str]
15 |     x = Optional[str]
   |         ^^^^^^^^^^^^^
   |
help: Convert to `X | None`

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:18:15
   |
18 | def f(x: list[Optional[int]]) -> None:
   |               ^^^^^^^^^^^^^
19 |     ...
   |
help: Convert to `X | None`
   |
17 |
   - def f(x: list[Optional[int]]) -> None:
18 + def f(x: list[int | None]) -> None:
19 |     ...
   |

UP045 Use `X | None` for type annotations
  --> UP045_1.py:22:10
   |
22 | def f(x: Optional[int : float]) -> None:
   |          ^^^^^^^^^^^^^^^^^^^^^
23 |     ...
   |
help: Convert to `X | None`

UP045 Use `X | None` for type annotations
  --> UP045_1.py:26:10
   |
26 | def f(x: Optional[str, int : float]) -> None:
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^
27 |     ...
   |
help: Convert to `X | None`

UP045 Use `X | None` for type annotations
  --> UP045_1.py:30:10
   |
30 | def f(x: Optional[int, float]) -> None:
   |          ^^^^^^^^^^^^^^^^^^^^
31 |     ...
   |
help: Convert to `X | None`

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:36:28
   |
34 |   # Regression test for: https://github.com/astral-sh/ruff/issues/7131
35 |   class ServiceRefOrValue:
36 |       service_specification: Optional[
   |  ____________________________^
37 | |         list[ServiceSpecificationRef]
38 | |         | list[ServiceSpecification]
39 | |     ] = None
   | |_____^
   |
help: Convert to `X | None`
   |
35 | class ServiceRefOrValue:
   -     service_specification: Optional[
   -         list[ServiceSpecificationRef]
   -         | list[ServiceSpecification]
   -     ] = None
36 +     service_specification: list[ServiceSpecificationRef] | list[ServiceSpecification] | None = None
37 |
   |

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:44:28
   |
42 | # Regression test for: https://github.com/astral-sh/ruff/issues/7201
43 | class ServiceRefOrValue:
44 |     service_specification: Optional[str]is not True = None
   |                            ^^^^^^^^^^^^^
   |
help: Convert to `X | None`
   |
43 | class ServiceRefOrValue:
   -     service_specification: Optional[str]is not True = None
44 +     service_specification: str | None is not True = None
45 |
   |

UP045 Use `X | None` for type annotations
  --> UP045_1.py:49:6
   |
47 | # Test for: https://github.com/astral-sh/ruff/issues/18508
48 | # Optional[None] should not be offered a fix
49 | foo: Optional[None] = None
   |      ^^^^^^^^^^^^^^
   |
help: Convert to `X | None`

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:76:18
   |
74 | # Test for: https://github.com/astral-sh/ruff/issues/19746
75 | # Nested Optional types should be flattened
76 | nested_optional: Optional[Optional[str]] = None
   |                  ^^^^^^^^^^^^^^^^^^^^^^^
77 | nested_optional_typing: typing.Optional[Optional[int]] = None
78 | triple_nested_optional: Optional[Optional[Optional[str]]] = None
   |
help: Convert to `X | None`
   |
75 | # Nested Optional types should be flattened
   - nested_optional: Optional[Optional[str]] = None
76 + nested_optional: str | None = None
77 | nested_optional_typing: typing.Optional[Optional[int]] = None
   |

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:76:27
   |
74 | # Test for: https://github.com/astral-sh/ruff/issues/19746
75 | # Nested Optional types should be flattened
76 | nested_optional: Optional[Optional[str]] = None
   |                           ^^^^^^^^^^^^^
77 | nested_optional_typing: typing.Optional[Optional[int]] = None
78 | triple_nested_optional: Optional[Optional[Optional[str]]] = None
   |
help: Convert to `X | None`
   |
75 | # Nested Optional types should be flattened
   - nested_optional: Optional[Optional[str]] = None
76 + nested_optional: Optional[str | None] = None
77 | nested_optional_typing: typing.Optional[Optional[int]] = None
   |

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:77:25
   |
75 | # Nested Optional types should be flattened
76 | nested_optional: Optional[Optional[str]] = None
77 | nested_optional_typing: typing.Optional[Optional[int]] = None
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
78 | triple_nested_optional: Optional[Optional[Optional[str]]] = None
   |
help: Convert to `X | None`
   |
76 | nested_optional: Optional[Optional[str]] = None
   - nested_optional_typing: typing.Optional[Optional[int]] = None
77 + nested_optional_typing: int | None = None
78 | triple_nested_optional: Optional[Optional[Optional[str]]] = None
   |

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:77:41
   |
75 | # Nested Optional types should be flattened
76 | nested_optional: Optional[Optional[str]] = None
77 | nested_optional_typing: typing.Optional[Optional[int]] = None
   |                                         ^^^^^^^^^^^^^
78 | triple_nested_optional: Optional[Optional[Optional[str]]] = None
   |
help: Convert to `X | None`
   |
76 | nested_optional: Optional[Optional[str]] = None
   - nested_optional_typing: typing.Optional[Optional[int]] = None
77 + nested_optional_typing: typing.Optional[int | None] = None
78 | triple_nested_optional: Optional[Optional[Optional[str]]] = None
   |

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:78:25
   |
76 | nested_optional: Optional[Optional[str]] = None
77 | nested_optional_typing: typing.Optional[Optional[int]] = None
78 | triple_nested_optional: Optional[Optional[Optional[str]]] = None
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Convert to `X | None`
   |
77 | nested_optional_typing: typing.Optional[Optional[int]] = None
   - triple_nested_optional: Optional[Optional[Optional[str]]] = None
78 + triple_nested_optional: str | None = None
79 |
   |

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:78:34
   |
76 | nested_optional: Optional[Optional[str]] = None
77 | nested_optional_typing: typing.Optional[Optional[int]] = None
78 | triple_nested_optional: Optional[Optional[Optional[str]]] = None
   |                                  ^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Convert to `X | None`
   |
77 | nested_optional_typing: typing.Optional[Optional[int]] = None
   - triple_nested_optional: Optional[Optional[Optional[str]]] = None
78 + triple_nested_optional: Optional[str | None] = None
79 |
   |

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:78:43
   |
76 | nested_optional: Optional[Optional[str]] = None
77 | nested_optional_typing: typing.Optional[Optional[int]] = None
78 | triple_nested_optional: Optional[Optional[Optional[str]]] = None
   |                                           ^^^^^^^^^^^^^
   |
help: Convert to `X | None`
   |
77 | nested_optional_typing: typing.Optional[Optional[int]] = None
   - triple_nested_optional: Optional[Optional[Optional[str]]] = None
78 + triple_nested_optional: Optional[Optional[str | None]] = None
79 |
   |

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:81:6
   |
81 |   foo: Optional[
   |  ______^
82 | |     int
83 | |     # text
84 | | ] = None
   | |_^
   |
help: Convert to `X | None`
   |
80 |
   - foo: Optional[
   -     int
   -     # text
   - ] = None
81 + foo: int | None = None
82 |
   |
note: This is an unsafe fix and may change runtime behavior

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:89:13
   |
87 | # Regression test for: https://github.com/astral-sh/ruff/issues/23429
88 | # Optional[None | X] should not produce None | None
89 | bar: None | Optional[None | int] = None
   |             ^^^^^^^^^^^^^^^^^^^^
90 | bar: Optional[None | int] = None
91 | bar: Optional[int | None] = None
   |
help: Convert to `X | None`
   |
88 | # Optional[None | X] should not produce None | None
   - bar: None | Optional[None | int] = None
89 + bar: None | int | None = None
90 | bar: Optional[None | int] = None
   |

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:90:6
   |
88 | # Optional[None | X] should not produce None | None
89 | bar: None | Optional[None | int] = None
90 | bar: Optional[None | int] = None
   |      ^^^^^^^^^^^^^^^^^^^^
91 | bar: Optional[int | None] = None
92 | bar: Optional[None | int | str] = None
   |
help: Convert to `X | None`
   |
89 | bar: None | Optional[None | int] = None
   - bar: Optional[None | int] = None
90 + bar: int | None = None
91 | bar: Optional[int | None] = None
   |

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:91:6
   |
89 | bar: None | Optional[None | int] = None
90 | bar: Optional[None | int] = None
91 | bar: Optional[int | None] = None
   |      ^^^^^^^^^^^^^^^^^^^^
92 | bar: Optional[None | int | str] = None
93 | bar: Optional[None | None] = None
   |
help: Convert to `X | None`
   |
90 | bar: Optional[None | int] = None
   - bar: Optional[int | None] = None
91 + bar: int | None = None
92 | bar: Optional[None | int | str] = None
   |

UP045 [*] Use `X | None` for type annotations
  --> UP045_1.py:92:6
   |
90 | bar: Optional[None | int] = None
91 | bar: Optional[int | None] = None
92 | bar: Optional[None | int | str] = None
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^
93 | bar: Optional[None | None] = None
   |
help: Convert to `X | None`
   |
91 | bar: Optional[int | None] = None
   - bar: Optional[None | int | str] = None
92 + bar: int | str | None = None
93 | bar: Optional[None | None] = None
   |

UP045 Use `X | None` for type annotations
  --> UP045_1.py:93:6
   |
91 | bar: Optional[int | None] = None
92 | bar: Optional[None | int | str] = None
93 | bar: Optional[None | None] = None
   |      ^^^^^^^^^^^^^^^^^^^^^
   |
help: Convert to `X | None`
