---
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
---
PYI061 [*] Use `None` rather than `Literal[None]`
 --> PYI061.py:4:25
  |
4 | def func1(arg1: Literal[None]):
  |                         ^^^^
5 |     ...
  |
help: Replace with `None`
  |
3 |
  - def func1(arg1: Literal[None]):
4 + def func1(arg1: None):
5 |     ...
  |

PYI061 [*] Use `None` rather than `Literal[None]`
 --> PYI061.py:8:25
  |
8 | def func2(arg1: Literal[None] | int):
  |                         ^^^^
9 |     ...
  |
help: Replace with `None`
  |
7 |
  - def func2(arg1: Literal[None] | int):
8 + def func2(arg1: None | int):
9 |     ...
  |

PYI061 [*] Use `None` rather than `Literal[None]`
  --> PYI061.py:12:24
   |
12 | def func3() -> Literal[None]:
   |                        ^^^^
13 |     ...
   |
help: Replace with `None`
   |
11 |
   - def func3() -> Literal[None]:
12 + def func3() -> None:
13 |     ...
   |

PYI061 [*] Use `Optional[Literal[...]]` rather than `Literal[None, ...]` 
  --> PYI061.py:16:30
   |
16 | def func4(arg1: Literal[int, None, float]):
   |                              ^^^^
17 |     ...
   |
help: Replace with `Optional[Literal[...]]`
   |
   - from typing import Literal, Union
1  + from typing import Literal, Union, Optional
2  |
--------------------------------------------------------------------------------
15 |
   - def func4(arg1: Literal[int, None, float]):
16 + def func4(arg1: Optional[Literal[int, float]]):
17 |     ...
   |

PYI061 [*] Use `None` rather than `Literal[None]`
  --> PYI061.py:20:25
   |
20 | def func5(arg1: Literal[None, None]):
   |                         ^^^^
21 |     ...
   |
help: Replace with `None`
   |
19 |
   - def func5(arg1: Literal[None, None]):
20 + def func5(arg1: None):
21 |     ...
   |

PYI061 [*] Use `None` rather than `Literal[None]`
  --> PYI061.py:20:31
   |
20 | def func5(arg1: Literal[None, None]):
   |                               ^^^^
21 |     ...
   |
help: Replace with `None`
   |
19 |
   - def func5(arg1: Literal[None, None]):
20 + def func5(arg1: None):
21 |     ...
   |

PYI061 [*] Use `Optional[Literal[...]]` rather than `Literal[None, ...]` 
  --> PYI061.py:26:5
   |
24 | def func6(arg1: Literal[
25 |     "hello",
26 |     None  # Comment 1
   |     ^^^^
27 |     , "world"
28 |     ]):
   |
help: Replace with `Optional[Literal[...]]`
   |
   - from typing import Literal, Union
1  + from typing import Literal, Union, Optional
2  |
--------------------------------------------------------------------------------
23 |
   - def func6(arg1: Literal[
   -     "hello",
   -     None  # Comment 1
   -     , "world"
   -     ]):
24 + def func6(arg1: Optional[Literal["hello", "world"]]):
25 |     ...
   |
note: This is an unsafe fix and may change runtime behavior

PYI061 [*] Use `None` rather than `Literal[None]`
  --> PYI061.py:33:5
   |
32 | def func7(arg1: Literal[
33 |     None  # Comment 1
   |     ^^^^
34 |     ]):
35 |     ...
   |
help: Replace with `None`
   |
31 |
   - def func7(arg1: Literal[
   -     None  # Comment 1
   -     ]):
32 + def func7(arg1: None):
33 |     ...
   |
note: This is an unsafe fix and may change runtime behavior

PYI061 Use `None` rather than `Literal[None]`
  --> PYI061.py:38:25
   |
38 | def func8(arg1: Literal[None] | None):
   |                         ^^^^
39 |     ...
   |
help: Replace with `None`

PYI061 [*] Use `None` rather than `Literal[None]`
  --> PYI061.py:42:31
   |
42 | def func9(arg1: Union[Literal[None], None]):
   |                               ^^^^
43 |     ...
   |
help: Replace with `None`
   |
41 |
   - def func9(arg1: Union[Literal[None], None]):
42 + def func9(arg1: Union[None, None]):
43 |     ...
   |

PYI061 [*] Use `None` rather than `Literal[None]`
  --> PYI061.py:52:9
   |
51 | # From flake8-pyi
52 | Literal[None]  # Y061 None inside "Literal[]" expression. Replace with "None"
   |         ^^^^
53 | Literal[True, None]  # Y061 None inside "Literal[]" expression. Replace with "Literal[True] | None"
   |
help: Replace with `None`
   |
51 | # From flake8-pyi
   - Literal[None]  # Y061 None inside "Literal[]" expression. Replace with "None"
52 + None  # Y061 None inside "Literal[]" expression. Replace with "None"
53 | Literal[True, None]  # Y061 None inside "Literal[]" expression. Replace with "Literal[True] | None"
   |

PYI061 [*] Use `Optional[Literal[...]]` rather than `Literal[None, ...]` 
  --> PYI061.py:53:15
   |
51 | # From flake8-pyi
52 | Literal[None]  # Y061 None inside "Literal[]" expression. Replace with "None"
53 | Literal[True, None]  # Y061 None inside "Literal[]" expression. Replace with "Literal[True] | None"
   |               ^^^^
54 |
55 | ###
   |
help: Replace with `Optional[Literal[...]]`
   |
   - from typing import Literal, Union
1  + from typing import Literal, Union, Optional
2  |
--------------------------------------------------------------------------------
52 | Literal[None]  # Y061 None inside "Literal[]" expression. Replace with "None"
   - Literal[True, None]  # Y061 None inside "Literal[]" expression. Replace with "Literal[True] | None"
53 + Optional[Literal[True]]  # Y061 None inside "Literal[]" expression. Replace with "Literal[True] | None"
54 |
   |

PYI061 [*] Use `None` rather than `Literal[None]`
  --> PYI061.py:62:9
   |
60 | # If Y061 and Y062 both apply, but all the duplicate members are None,
61 | # only emit Y061...
62 | Literal[None, None]  # Y061 None inside "Literal[]" expression. Replace with "None"
   |         ^^^^
63 | Literal[1, None, "foo", None]  # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo'] | None"
   |
help: Replace with `None`
   |
61 | # only emit Y061...
   - Literal[None, None]  # Y061 None inside "Literal[]" expression. Replace with "None"
62 + None  # Y061 None inside "Literal[]" expression. Replace with "None"
63 | Literal[1, None, "foo", None]  # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo'] | None"
   |

PYI061 [*] Use `None` rather than `Literal[None]`
  --> PYI061.py:62:15
   |
60 | # If Y061 and Y062 both apply, but all the duplicate members are None,
61 | # only emit Y061...
62 | Literal[None, None]  # Y061 None inside "Literal[]" expression. Replace with "None"
   |               ^^^^
63 | Literal[1, None, "foo", None]  # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo'] | None"
   |
help: Replace with `None`
   |
61 | # only emit Y061...
   - Literal[None, None]  # Y061 None inside "Literal[]" expression. Replace with "None"
62 + None  # Y061 None inside "Literal[]" expression. Replace with "None"
63 | Literal[1, None, "foo", None]  # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo'] | None"
   |

PYI061 [*] Use `Optional[Literal[...]]` rather than `Literal[None, ...]` 
  --> PYI061.py:63:12
   |
61 | # only emit Y061...
62 | Literal[None, None]  # Y061 None inside "Literal[]" expression. Replace with "None"
63 | Literal[1, None, "foo", None]  # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo'] | None"
   |            ^^^^
64 |
65 | # ... but if Y061 and Y062 both apply
   |
help: Replace with `Optional[Literal[...]]`
   |
   - from typing import Literal, Union
1  + from typing import Literal, Union, Optional
2  |
--------------------------------------------------------------------------------
62 | Literal[None, None]  # Y061 None inside "Literal[]" expression. Replace with "None"
   - Literal[1, None, "foo", None]  # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo'] | None"
63 + Optional[Literal[1, "foo"]]  # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo'] | None"
64 |
   |

PYI061 [*] Use `Optional[Literal[...]]` rather than `Literal[None, ...]` 
  --> PYI061.py:63:25
   |
61 | # only emit Y061...
62 | Literal[None, None]  # Y061 None inside "Literal[]" expression. Replace with "None"
63 | Literal[1, None, "foo", None]  # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo'] | None"
   |                         ^^^^
64 |
65 | # ... but if Y061 and Y062 both apply
   |
help: Replace with `Optional[Literal[...]]`
   |
   - from typing import Literal, Union
1  + from typing import Literal, Union, Optional
2  |
--------------------------------------------------------------------------------
62 | Literal[None, None]  # Y061 None inside "Literal[]" expression. Replace with "None"
   - Literal[1, None, "foo", None]  # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo'] | None"
63 + Optional[Literal[1, "foo"]]  # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo'] | None"
64 |
   |

PYI061 [*] Use `Optional[Literal[...]]` rather than `Literal[None, ...]` 
  --> PYI061.py:68:9
   |
66 | # and there are no None members in the Literal[] slice,
67 | # only emit Y062:
68 | Literal[None, True, None, True]  # Y062 Duplicate "Literal[]" member "True"
   |         ^^^^
   |
help: Replace with `Optional[Literal[...]]`
   |
   - from typing import Literal, Union
1  + from typing import Literal, Union, Optional
2  |
--------------------------------------------------------------------------------
67 | # only emit Y062:
   - Literal[None, True, None, True]  # Y062 Duplicate "Literal[]" member "True"
68 + Optional[Literal[True, True]]  # Y062 Duplicate "Literal[]" member "True"
69 |
   |

PYI061 [*] Use `Optional[Literal[...]]` rather than `Literal[None, ...]` 
  --> PYI061.py:68:21
   |
66 | # and there are no None members in the Literal[] slice,
67 | # only emit Y062:
68 | Literal[None, True, None, True]  # Y062 Duplicate "Literal[]" member "True"
   |                     ^^^^
   |
help: Replace with `Optional[Literal[...]]`
   |
   - from typing import Literal, Union
1  + from typing import Literal, Union, Optional
2  |
--------------------------------------------------------------------------------
67 | # only emit Y062:
   - Literal[None, True, None, True]  # Y062 Duplicate "Literal[]" member "True"
68 + Optional[Literal[True, True]]  # Y062 Duplicate "Literal[]" member "True"
69 |
   |

PYI061 Use `None` rather than `Literal[None]`
  --> PYI061.py:72:12
   |
71 | # Regression tests for https://github.com/astral-sh/ruff/issues/14567
72 | x: Literal[None] | None
   |            ^^^^
73 | y: None | Literal[None]
74 | z: Union[Literal[None], None]
   |
help: Replace with `None`

PYI061 Use `None` rather than `Literal[None]`
  --> PYI061.py:73:19
   |
71 | # Regression tests for https://github.com/astral-sh/ruff/issues/14567
72 | x: Literal[None] | None
73 | y: None | Literal[None]
   |                   ^^^^
74 | z: Union[Literal[None], None]
   |
help: Replace with `None`

PYI061 [*] Use `None` rather than `Literal[None]`
  --> PYI061.py:74:18
   |
72 | x: Literal[None] | None
73 | y: None | Literal[None]
74 | z: Union[Literal[None], None]
   |                  ^^^^
75 |
76 | a: int | Literal[None] | None
   |
help: Replace with `None`
   |
73 | y: None | Literal[None]
   - z: Union[Literal[None], None]
74 + z: Union[None, None]
75 |
   |

PYI061 Use `None` rather than `Literal[None]`
  --> PYI061.py:76:18
   |
74 | z: Union[Literal[None], None]
75 |
76 | a: int | Literal[None] | None
   |                  ^^^^
77 | b: None | Literal[None] | None
78 | c: (None | Literal[None]) | None
   |
help: Replace with `None`

PYI061 Use `None` rather than `Literal[None]`
  --> PYI061.py:77:19
   |
76 | a: int | Literal[None] | None
77 | b: None | Literal[None] | None
   |                   ^^^^
78 | c: (None | Literal[None]) | None
79 | d: None | (Literal[None] | None)
   |
help: Replace with `None`

PYI061 Use `None` rather than `Literal[None]`
  --> PYI061.py:78:20
   |
76 | a: int | Literal[None] | None
77 | b: None | Literal[None] | None
78 | c: (None | Literal[None]) | None
   |                    ^^^^
79 | d: None | (Literal[None] | None)
80 | e: None | ((None | Literal[None]) | None) | None
   |
help: Replace with `None`

PYI061 Use `None` rather than `Literal[None]`
  --> PYI061.py:79:20
   |
77 | b: None | Literal[None] | None
78 | c: (None | Literal[None]) | None
79 | d: None | (Literal[None] | None)
   |                    ^^^^
80 | e: None | ((None | Literal[None]) | None) | None
   |
help: Replace with `None`

PYI061 Use `None` rather than `Literal[None]`
  --> PYI061.py:80:28
   |
78 | c: (None | Literal[None]) | None
79 | d: None | (Literal[None] | None)
80 | e: None | ((None | Literal[None]) | None) | None
   |                            ^^^^
81 |
82 | # Test cases for operator precedence issue (https://github.com/astral-sh/ruff/issues/20265)
   |
help: Replace with `None`

PYI061 [*] Use `Optional[Literal[...]]` rather than `Literal[None, ...]` 
  --> PYI061.py:83:18
   |
82 | # Test cases for operator precedence issue (https://github.com/astral-sh/ruff/issues/20265)
83 | print(Literal[1, None].__dict__)  # Should become (Literal[1] | None).__dict__
   |                  ^^^^
84 | print(Literal[1, None].method())  # Should become (Literal[1] | None).method()
85 | print(Literal[1, None][0])  # Should become (Literal[1] | None)[0]
   |
help: Replace with `Optional[Literal[...]]`
   |
   - from typing import Literal, Union
1  + from typing import Literal, Union, Optional
2  |
--------------------------------------------------------------------------------
82 | # Test cases for operator precedence issue (https://github.com/astral-sh/ruff/issues/20265)
   - print(Literal[1, None].__dict__)  # Should become (Literal[1] | None).__dict__
83 + print(Optional[Literal[1]].__dict__)  # Should become (Literal[1] | None).__dict__
84 | print(Literal[1, None].method())  # Should become (Literal[1] | None).method()
   |

PYI061 [*] Use `Optional[Literal[...]]` rather than `Literal[None, ...]` 
  --> PYI061.py:84:18
   |
82 | # Test cases for operator precedence issue (https://github.com/astral-sh/ruff/issues/20265)
83 | print(Literal[1, None].__dict__)  # Should become (Literal[1] | None).__dict__
84 | print(Literal[1, None].method())  # Should become (Literal[1] | None).method()
   |                  ^^^^
85 | print(Literal[1, None][0])  # Should become (Literal[1] | None)[0]
86 | print(Literal[1, None] + 1)  # Should become (Literal[1] | None) + 1
   |
help: Replace with `Optional[Literal[...]]`
   |
   - from typing import Literal, Union
1  + from typing import Literal, Union, Optional
2  |
--------------------------------------------------------------------------------
83 | print(Literal[1, None].__dict__)  # Should become (Literal[1] | None).__dict__
   - print(Literal[1, None].method())  # Should become (Literal[1] | None).method()
84 + print(Optional[Literal[1]].method())  # Should become (Literal[1] | None).method()
85 | print(Literal[1, None][0])  # Should become (Literal[1] | None)[0]
   |

PYI061 [*] Use `Optional[Literal[...]]` rather than `Literal[None, ...]` 
  --> PYI061.py:85:18
   |
83 | print(Literal[1, None].__dict__)  # Should become (Literal[1] | None).__dict__
84 | print(Literal[1, None].method())  # Should become (Literal[1] | None).method()
85 | print(Literal[1, None][0])  # Should become (Literal[1] | None)[0]
   |                  ^^^^
86 | print(Literal[1, None] + 1)  # Should become (Literal[1] | None) + 1
87 | print(Literal[1, None] * 2)  # Should become (Literal[1] | None) * 2
   |
help: Replace with `Optional[Literal[...]]`
   |
   - from typing import Literal, Union
1  + from typing import Literal, Union, Optional
2  |
--------------------------------------------------------------------------------
84 | print(Literal[1, None].method())  # Should become (Literal[1] | None).method()
   - print(Literal[1, None][0])  # Should become (Literal[1] | None)[0]
85 + print(Optional[Literal[1]][0])  # Should become (Literal[1] | None)[0]
86 | print(Literal[1, None] + 1)  # Should become (Literal[1] | None) + 1
   |

PYI061 [*] Use `Optional[Literal[...]]` rather than `Literal[None, ...]` 
  --> PYI061.py:86:18
   |
84 | print(Literal[1, None].method())  # Should become (Literal[1] | None).method()
85 | print(Literal[1, None][0])  # Should become (Literal[1] | None)[0]
86 | print(Literal[1, None] + 1)  # Should become (Literal[1] | None) + 1
   |                  ^^^^
87 | print(Literal[1, None] * 2)  # Should become (Literal[1] | None) * 2
88 | print((Literal[1, None]).__dict__)  # Should become ((Literal[1] | None)).__dict__
   |
help: Replace with `Optional[Literal[...]]`
   |
   - from typing import Literal, Union
1  + from typing import Literal, Union, Optional
2  |
--------------------------------------------------------------------------------
85 | print(Literal[1, None][0])  # Should become (Literal[1] | None)[0]
   - print(Literal[1, None] + 1)  # Should become (Literal[1] | None) + 1
86 + print(Optional[Literal[1]] + 1)  # Should become (Literal[1] | None) + 1
87 | print(Literal[1, None] * 2)  # Should become (Literal[1] | None) * 2
   |

PYI061 [*] Use `Optional[Literal[...]]` rather than `Literal[None, ...]` 
  --> PYI061.py:87:18
   |
85 | print(Literal[1, None][0])  # Should become (Literal[1] | None)[0]
86 | print(Literal[1, None] + 1)  # Should become (Literal[1] | None) + 1
87 | print(Literal[1, None] * 2)  # Should become (Literal[1] | None) * 2
   |                  ^^^^
88 | print((Literal[1, None]).__dict__)  # Should become ((Literal[1] | None)).__dict__
   |
help: Replace with `Optional[Literal[...]]`
   |
   - from typing import Literal, Union
1  + from typing import Literal, Union, Optional
2  |
--------------------------------------------------------------------------------
86 | print(Literal[1, None] + 1)  # Should become (Literal[1] | None) + 1
   - print(Literal[1, None] * 2)  # Should become (Literal[1] | None) * 2
87 + print(Optional[Literal[1]] * 2)  # Should become (Literal[1] | None) * 2
88 | print((Literal[1, None]).__dict__)  # Should become ((Literal[1] | None)).__dict__
   |

PYI061 [*] Use `Optional[Literal[...]]` rather than `Literal[None, ...]` 
  --> PYI061.py:88:19
   |
86 | print(Literal[1, None] + 1)  # Should become (Literal[1] | None) + 1
87 | print(Literal[1, None] * 2)  # Should become (Literal[1] | None) * 2
88 | print((Literal[1, None]).__dict__)  # Should become ((Literal[1] | None)).__dict__
   |                   ^^^^
   |
help: Replace with `Optional[Literal[...]]`
   |
   - from typing import Literal, Union
1  + from typing import Literal, Union, Optional
2  |
--------------------------------------------------------------------------------
87 | print(Literal[1, None] * 2)  # Should become (Literal[1] | None) * 2
   - print((Literal[1, None]).__dict__)  # Should become ((Literal[1] | None)).__dict__
88 + print((Optional[Literal[1]]).__dict__)  # Should become ((Literal[1] | None)).__dict__
   |
