33import inspect
44import logging
55import os
6+ from collections .abc import Callable
67from contextlib import contextmanager
78from contextvars import ContextVar
89from typing import ( # type: ignore[attr-defined]
9- Callable ,
1010 Generic ,
1111 Optional ,
1212 Protocol ,
1313 TypeVar ,
14- Union ,
1514 _GenericAlias ,
1615)
1716
@@ -52,17 +51,17 @@ def __call__(self, class_type: type[ClassType], *args, **kwargs) -> ClassType:
5251InstantiatorsDictType = dict [tuple [type , bool ], InstantiatorCallable ]
5352
5453
55- parent_parser : ContextVar [Optional [ ArgumentParser ] ] = ContextVar ("parent_parser" , default = None )
54+ parent_parser : ContextVar [ArgumentParser | None ] = ContextVar ("parent_parser" , default = None )
5655parser_capture : ContextVar [bool ] = ContextVar ("parser_capture" , default = False )
57- defaults_cache : ContextVar [Optional [ Namespace ] ] = ContextVar ("defaults_cache" , default = None )
58- lenient_check : ContextVar [Union [ bool , str ] ] = ContextVar ("lenient_check" , default = False )
56+ defaults_cache : ContextVar [Namespace | None ] = ContextVar ("defaults_cache" , default = None )
57+ lenient_check : ContextVar [bool | str ] = ContextVar ("lenient_check" , default = False )
5958parsing_defaults : ContextVar [bool ] = ContextVar ("parsing_defaults" , default = False )
6059single_subcommand : ContextVar [bool ] = ContextVar ("single_subcommand" , default = True )
6160validating_defaults : ContextVar [bool ] = ContextVar ("validating_defaults" , default = False )
62- load_value_mode : ContextVar [Optional [ str ] ] = ContextVar ("load_value_mode" , default = None )
63- class_instantiators : ContextVar [Optional [ InstantiatorsDictType ] ] = ContextVar ("class_instantiators" , default = None )
61+ load_value_mode : ContextVar [str | None ] = ContextVar ("load_value_mode" , default = None )
62+ class_instantiators : ContextVar [InstantiatorsDictType | None ] = ContextVar ("class_instantiators" , default = None )
6463nested_links : ContextVar [list [dict ]] = ContextVar ("nested_links" , default = [])
65- applied_instantiation_links : ContextVar [Optional [ set ] ] = ContextVar ("applied_instantiation_links" , default = None )
64+ applied_instantiation_links : ContextVar [set | None ] = ContextVar ("applied_instantiation_links" , default = None )
6665path_dump_preserve_relative : ContextVar [bool ] = ContextVar ("path_dump_preserve_relative" , default = False )
6766
6867
@@ -115,17 +114,17 @@ def get_env_var_bool(name: str) -> bool:
115114
116115def set_parsing_settings (
117116 * ,
118- validate_defaults : Optional [ bool ] = None ,
119- config_read_mode_urls_enabled : Optional [ bool ] = None ,
120- config_read_mode_fsspec_enabled : Optional [ bool ] = None ,
117+ validate_defaults : bool | None = None ,
118+ config_read_mode_urls_enabled : bool | None = None ,
119+ config_read_mode_fsspec_enabled : bool | None = None ,
121120 docstring_parse_style : Optional ["docstring_parser.DocstringStyle" ] = None ,
122- docstring_parse_attribute_docstrings : Optional [ bool ] = None ,
123- parse_optionals_as_positionals : Optional [ bool ] = None ,
124- add_print_completion_argument : Optional [ bool ] = None ,
125- stubs_resolver_allow_py_files : Optional [ bool ] = None ,
126- omegaconf_absolute_to_relative_paths : Optional [ bool ] = None ,
127- subclasses_disabled : Optional [ list [Union [ type , Callable [[type ], bool ]]]] = None ,
128- subclasses_enabled : Optional [ list [Union [ type , str ]]] = None ,
121+ docstring_parse_attribute_docstrings : bool | None = None ,
122+ parse_optionals_as_positionals : bool | None = None ,
123+ add_print_completion_argument : bool | None = None ,
124+ stubs_resolver_allow_py_files : bool | None = None ,
125+ omegaconf_absolute_to_relative_paths : bool | None = None ,
126+ subclasses_disabled : list [type | Callable [[type ], bool ]] | None = None ,
127+ subclasses_enabled : list [type | str ] | None = None ,
129128) -> None :
130129 """
131130 Modify global parser settings that affect parser creation and parsing behavior.
@@ -329,7 +328,7 @@ def is_pure_dataclass(cls) -> bool:
329328
330329subclasses_enabled_types : set [type ] = set ()
331330subclasses_disabled_types : set [type ] = set ()
332- subclasses_disabled_selectors : dict [str , Callable [[type ], Union [ bool , int ] ]] = {
331+ subclasses_disabled_selectors : dict [str , Callable [[type ], bool | int ]] = {
333332 "is_pure_dataclass" : is_pure_dataclass ,
334333 "is_pydantic_model" : is_pydantic_model ,
335334 "is_attrs_class" : is_attrs_class ,
@@ -351,8 +350,8 @@ def is_subclasses_disabled(cls) -> bool:
351350
352351
353352def subclass_type_behavior (
354- subclasses_disabled : Optional [ list [Union [ type , Callable [[type ], bool ]]]] = None ,
355- subclasses_enabled : Optional [ list [Union [ type , str ]]] = None ,
353+ subclasses_disabled : list [type | Callable [[type ], bool ]] | None = None ,
354+ subclasses_enabled : list [type | str ] | None = None ,
356355) -> None :
357356 """Configures whether class types accept or not subclasses."""
358357 for enable_item in subclasses_enabled or []:
@@ -404,7 +403,7 @@ def setup_default_logger(data, level, caller):
404403 return logger
405404
406405
407- def parse_logger (logger : Union [ bool , str , dict , logging .Logger ] , caller ):
406+ def parse_logger (logger : bool | str | dict | logging .Logger , caller ):
408407 if not isinstance (logger , (bool , str , dict , logging .Logger )):
409408 raise ValueError (f"Expected logger to be an instance of (bool, str, dict, logging.Logger), but got { logger } ." )
410409 if isinstance (logger , dict ) and len (set (logger ) - {"name" , "level" }) > 0 :
@@ -425,7 +424,7 @@ def parse_logger(logger: Union[bool, str, dict, logging.Logger], caller):
425424class LoggerProperty :
426425 """Class designed to be inherited by other classes to add a logger property."""
427426
428- def __init__ (self , * args , logger : Union [ bool , str , dict , logging .Logger ] = False , ** kwargs ):
427+ def __init__ (self , * args , logger : bool | str | dict | logging .Logger = False , ** kwargs ):
429428 self .logger = logger
430429 super ().__init__ (* args , ** kwargs )
431430
@@ -444,7 +443,7 @@ def logger(self) -> logging.Logger:
444443 return self ._logger
445444
446445 @logger .setter
447- def logger (self , logger : Union [ bool , str , dict , logging .Logger ] ):
446+ def logger (self , logger : bool | str | dict | logging .Logger ):
448447 if logger is None :
449448 from ._deprecated import deprecation_warning , logger_property_none_message
450449
0 commit comments