1919from json .decoder import JSONDecodeError
2020from pathlib import Path
2121from re import Pattern
22- from typing import Any , Optional , Union
22+ from typing import Any
2323
2424import click
2525from click .core import ParameterSource
@@ -114,8 +114,8 @@ def from_configuration(
114114
115115
116116def read_pyproject_toml (
117- ctx : click .Context , param : click .Parameter , value : Optional [ str ]
118- ) -> Optional [ str ] :
117+ ctx : click .Context , param : click .Parameter , value : str | None
118+ ) -> str | None :
119119 """Inject Black configuration from "pyproject.toml" into defaults in `ctx`.
120120
121121 Returns the path to a successfully found and read configuration file, None
@@ -193,7 +193,7 @@ def spellcheck_pyproject_toml_keys(
193193
194194
195195def target_version_option_callback (
196- c : click .Context , p : Union [ click .Option , click .Parameter ] , v : tuple [str , ...]
196+ c : click .Context , p : click .Option | click .Parameter , v : tuple [str , ...]
197197) -> list [TargetVersion ]:
198198 """Compute the target versions from a --target-version flag.
199199
@@ -204,7 +204,7 @@ def target_version_option_callback(
204204
205205
206206def enable_unstable_feature_callback (
207- c : click .Context , p : Union [ click .Option , click .Parameter ] , v : tuple [str , ...]
207+ c : click .Context , p : click .Option | click .Parameter , v : tuple [str , ...]
208208) -> list [Preview ]:
209209 """Compute the features from an --enable-unstable-feature flag."""
210210 return [Preview [val ] for val in v ]
@@ -224,8 +224,8 @@ def re_compile_maybe_verbose(regex: str) -> Pattern[str]:
224224def validate_regex (
225225 ctx : click .Context ,
226226 param : click .Parameter ,
227- value : Optional [ str ] ,
228- ) -> Optional [ Pattern [str ]] :
227+ value : str | None ,
228+ ) -> Pattern [str ] | None :
229229 try :
230230 return re_compile_maybe_verbose (value ) if value is not None else None
231231 except re .error as e :
@@ -516,7 +516,7 @@ def validate_regex(
516516@click .pass_context
517517def main ( # noqa: C901
518518 ctx : click .Context ,
519- code : Optional [ str ] ,
519+ code : str | None ,
520520 line_length : int ,
521521 target_version : list [TargetVersion ],
522522 check : bool ,
@@ -535,15 +535,15 @@ def main( # noqa: C901
535535 enable_unstable_feature : list [Preview ],
536536 quiet : bool ,
537537 verbose : bool ,
538- required_version : Optional [ str ] ,
538+ required_version : str | None ,
539539 include : Pattern [str ],
540- exclude : Optional [ Pattern [str ]] ,
541- extend_exclude : Optional [ Pattern [str ]] ,
542- force_exclude : Optional [ Pattern [str ]] ,
543- stdin_filename : Optional [ str ] ,
544- workers : Optional [ int ] ,
540+ exclude : Pattern [str ] | None ,
541+ extend_exclude : Pattern [str ] | None ,
542+ force_exclude : Pattern [str ] | None ,
543+ stdin_filename : str | None ,
544+ workers : int | None ,
545545 src : tuple [str , ...],
546- config : Optional [ str ] ,
546+ config : str | None ,
547547 no_cache : bool ,
548548) -> None :
549549 """The uncompromising code formatter."""
@@ -738,19 +738,19 @@ def get_sources(
738738 quiet : bool ,
739739 verbose : bool ,
740740 include : Pattern [str ],
741- exclude : Optional [ Pattern [str ]] ,
742- extend_exclude : Optional [ Pattern [str ]] ,
743- force_exclude : Optional [ Pattern [str ]] ,
741+ exclude : Pattern [str ] | None ,
742+ extend_exclude : Pattern [str ] | None ,
743+ force_exclude : Pattern [str ] | None ,
744744 report : "Report" ,
745- stdin_filename : Optional [ str ] ,
745+ stdin_filename : str | None ,
746746) -> set [Path ]:
747747 """Compute the set of files to be formatted."""
748748 sources : set [Path ] = set ()
749749
750750 assert root .is_absolute (), f"INTERNAL ERROR: `root` must be absolute but is { root } "
751751 using_default_exclude = exclude is None
752752 exclude = re_compile_maybe_verbose (DEFAULT_EXCLUDES ) if exclude is None else exclude
753- gitignore : Optional [ dict [Path , PathSpec ]] = None
753+ gitignore : dict [Path , PathSpec ] | None = None
754754 root_gitignore = get_gitignore (root )
755755
756756 for s in src :
@@ -994,7 +994,7 @@ def format_file_in_place(
994994def format_stdin_to_stdout (
995995 fast : bool ,
996996 * ,
997- content : Optional [ str ] = None ,
997+ content : str | None = None ,
998998 write_back : WriteBack = WriteBack .NO ,
999999 mode : Mode ,
10001000 lines : Collection [tuple [int , int ]] = (),
@@ -1267,7 +1267,7 @@ def _format_str_once(
12671267 }
12681268 if supports_feature (versions , feature )
12691269 }
1270- block : Optional [ LinesBlock ] = None
1270+ block : LinesBlock | None = None
12711271 for current_line in line_generator .visit (src_node ):
12721272 block = elt .maybe_empty_lines (current_line )
12731273 dst_blocks .append (block )
@@ -1335,7 +1335,7 @@ def decode_bytes(src: bytes, mode: Mode) -> tuple[FileContent, Encoding, NewLine
13351335
13361336
13371337def get_features_used ( # noqa: C901
1338- node : Node , * , future_imports : Optional [ set [str ]] = None
1338+ node : Node , * , future_imports : set [str ] | None = None
13391339) -> set [Feature ]:
13401340 """Return a set of (relatively) new Python features used in this file.
13411341
@@ -1496,7 +1496,7 @@ def get_features_used( # noqa: C901
14961496 return features
14971497
14981498
1499- def _contains_asexpr (node : Union [ Node , Leaf ] ) -> bool :
1499+ def _contains_asexpr (node : Node | Leaf ) -> bool :
15001500 """Return True if `node` contains an as-pattern."""
15011501 if node .type == syms .asexpr_test :
15021502 return True
@@ -1513,7 +1513,7 @@ def _contains_asexpr(node: Union[Node, Leaf]) -> bool:
15131513
15141514
15151515def detect_target_versions (
1516- node : Node , * , future_imports : Optional [ set [str ]] = None
1516+ node : Node , * , future_imports : set [str ] | None = None
15171517) -> set [TargetVersion ]:
15181518 """Detect the version to target based on the nodes used."""
15191519 features = get_features_used (node , future_imports = future_imports )
0 commit comments