6666 Iterator ,
6767 KeysView ,
6868 List ,
69- Optional ,
7069 Protocol ,
7170 Tuple ,
7271 TypeVar ,
73- Union ,
7472 cast ,
7573)
7674
7977from .lang import Language , LanguageContext
8078from .lang ._common import IncludeGenerator
8179
82- if sys .version_info < (3 , 9 ):
83- # Python 3.8 has a bug. This is a workaround per https://stackoverflow.com/a/66518591/310659
84- def _register (self , cls , method = None ): # type: ignore
85- if hasattr (cls , "__func__" ):
86- setattr (cls , "__annotations__" , cls .__func__ .__annotations__ )
87- return self .dispatcher .register (cls , func = method )
88-
89- singledispatchmethod .register = _register # type: ignore
90-
9180# +--------------------------------------------------------------------------------------------------------------------+
9281
9382
@@ -96,7 +85,7 @@ class AsyncResultProtocol(Protocol):
9685 Defines the protocol for a duck-type compatible with multiprocessing.pool.AsyncResult.
9786 """
9887
99- def get (self , timeout : Optional [ Any ] = None ) -> Any :
88+ def get (self , timeout : Any | None = None ) -> Any :
10089 """
10190 See multiprocessing.pool.AsyncResult.get
10291 """
@@ -113,7 +102,7 @@ def __init__(self, read_method: Callable[..., Any], args: Tuple[Any, ...]) -> No
113102 self .args = args
114103 self ._logger = logging .getLogger (NotAsyncResult .__name__ )
115104
116- def get (self , timeout : Optional [ Any ] = None ) -> Any :
105+ def get (self , timeout : Any | None = None ) -> Any :
117106 """
118107 Perform the work synchronously.
119108 """
@@ -130,7 +119,7 @@ def get(self, timeout: Optional[Any] = None) -> Any:
130119def _read_files_strategy (
131120 index : "Namespace" ,
132121 apply_method : ApplyMethodT ,
133- dsdl_files : Union [ Path , str , Iterable [Union [ Path , str ]] ],
122+ dsdl_files : Path | str | Iterable [Path | str ],
134123 job_timeout_seconds : float ,
135124 omit_dependencies : bool ,
136125 args : Iterable [Any ],
@@ -274,7 +263,7 @@ def wrap(
274263 """
275264 return Generatable (definition , input_types , path )
276265
277- def with_segments (self , * pathsegments : Union [ str , PathLike ] ) -> Path :
266+ def with_segments (self , * pathsegments : str | PathLike ) -> Path :
278267 """
279268 Path override: Construct a new path object from any number of path-like objects.
280269 We discard the Generatable type here and continue on with a default Path object.
@@ -380,11 +369,11 @@ def strop_namespace(cls, full_namespace: str, language_context: LanguageContext)
380369 def add_types (
381370 cls ,
382371 index : "Namespace" ,
383- types : Union [
384- Tuple [pydsdl .CompositeType , List [pydsdl .CompositeType ]],
385- List [Tuple [pydsdl .CompositeType , List [pydsdl .CompositeType ]]],
386- ] ,
387- extension : Optional [ str ] = None ,
372+ types : (
373+ Tuple [pydsdl .CompositeType , List [pydsdl .CompositeType ]]
374+ | List [Tuple [pydsdl .CompositeType , List [pydsdl .CompositeType ]]]
375+ ) ,
376+ extension : str | None = None ,
388377 ) -> None :
389378 """
390379 Add a set of types to a namespace tree building new nodes as needed.
@@ -441,9 +430,9 @@ def Identity(cls, output_path: Path, lctx: LanguageContext) -> "Namespace":
441430 def read_namespace (
442431 cls ,
443432 index : "Namespace" ,
444- root_namespace_directory : Union [ Path , str ] ,
445- lookup_directories : Optional [ Union [ Path , str , Iterable [Union [ Path , str ]]]] = None ,
446- print_output_handler : Optional [ Callable [[Path , int , str ], None ]] = None ,
433+ root_namespace_directory : Path | str ,
434+ lookup_directories : Path | str | Iterable [Path | str ] | None = None ,
435+ print_output_handler : Callable [[Path , int , str ], None ] | None = None ,
447436 allow_unregulated_fixed_port_id : bool = False ,
448437 allow_root_namespace_name_collision : bool = True ,
449438 ) -> "Namespace" :
@@ -477,9 +466,9 @@ def _(
477466 cls ,
478467 output_path : str ,
479468 lctx : LanguageContext ,
480- root_namespace_directory : Union [ Path , str ] ,
481- lookup_directories : Optional [ Union [ Path , str , Iterable [Union [ Path , str ]]]] = None ,
482- print_output_handler : Optional [ Callable [[Path , int , str ], None ]] = None ,
469+ root_namespace_directory : Path | str ,
470+ lookup_directories : Path | str | Iterable [Path | str ] | None = None ,
471+ print_output_handler : Callable [[Path , int , str ], None ] | None = None ,
483472 allow_unregulated_fixed_port_id : bool = False ,
484473 allow_root_namespace_name_collision : bool = True ,
485474 ) -> pydsdl .Any :
@@ -510,9 +499,9 @@ def _(
510499 cls ,
511500 output_path : Path ,
512501 lctx : LanguageContext ,
513- root_namespace_directory : Union [ Path , str ] ,
514- lookup_directories : Optional [ Union [ Path , str , Iterable [Union [ Path , str ]]]] = None ,
515- print_output_handler : Optional [ Callable [[Path , int , str ], None ]] = None ,
502+ root_namespace_directory : Path | str ,
503+ lookup_directories : Path | str | Iterable [Path | str ] | None = None ,
504+ print_output_handler : Callable [[Path , int , str ], None ] | None = None ,
516505 allow_unregulated_fixed_port_id : bool = False ,
517506 allow_root_namespace_name_collision : bool = True ,
518507 ) -> pydsdl .Any :
@@ -542,12 +531,12 @@ def _(
542531 def read_files (
543532 cls ,
544533 index : "Namespace" ,
545- dsdl_files : Union [ Path , str , Iterable [Union [ Path , str ]] ],
546- root_namespace_directories_or_names : Optional [ Union [ Path , str , Iterable [Union [ Path , str ]]]] ,
534+ dsdl_files : Path | str | Iterable [Path | str ],
535+ root_namespace_directories_or_names : Path | str | Iterable [Path | str ] | None ,
547536 jobs : int = 0 ,
548537 job_timeout_seconds : float = 0 ,
549- lookup_directories : Optional [ Union [ Path , str , Iterable [Union [ Path , str ]]]] = None ,
550- print_output_handler : Optional [ Callable [[Path , int , str ], None ]] = None ,
538+ lookup_directories : Path | str | Iterable [Path | str ] | None = None ,
539+ print_output_handler : Callable [[Path , int , str ], None ] | None = None ,
551540 allow_unregulated_fixed_port_id : bool = False ,
552541 omit_dependencies : bool = False ,
553542 ) -> "Namespace" :
@@ -590,12 +579,12 @@ def _(
590579 cls ,
591580 output_path : Path ,
592581 lctx : LanguageContext ,
593- dsdl_files : Optional [ Union [ Path , str , Iterable [Union [ Path , str ]]]] ,
594- root_namespace_directories_or_names : Optional [ Union [ Path , str , Iterable [Union [ Path , str ]]]] ,
582+ dsdl_files : Path | str | Iterable [Path | str ] | None ,
583+ root_namespace_directories_or_names : Path | str | Iterable [Path | str ] | None ,
595584 jobs : int = 0 ,
596585 job_timeout_seconds : float = 0 ,
597- lookup_directories : Optional [ Union [ Path , str , Iterable [Union [ Path , str ]]]] = None ,
598- print_output_handler : Optional [ Callable [[Path , int , str ], None ]] = None ,
586+ lookup_directories : Path | str | Iterable [Path | str ] | None = None ,
587+ print_output_handler : Callable [[Path , int , str ], None ] | None = None ,
599588 allow_unregulated_fixed_port_id : bool = False ,
600589 omit_dependencies : bool = False ,
601590 ) -> pydsdl .Any :
@@ -633,12 +622,12 @@ def _(
633622 cls ,
634623 output_path : str ,
635624 lctx : LanguageContext ,
636- dsdl_files : Optional [ Union [ Path , str , Iterable [Union [ Path , str ]]]] ,
637- root_namespace_directories_or_names : Optional [ Union [ Path , str , Iterable [Union [ Path , str ]]]] ,
625+ dsdl_files : Path | str | Iterable [Path | str ] | None ,
626+ root_namespace_directories_or_names : Path | str | Iterable [Path | str ] | None ,
638627 jobs : int = 0 ,
639628 job_timeout_seconds : float = 0 ,
640- lookup_directories : Optional [ Union [ Path , str , Iterable [Union [ Path , str ]]]] = None ,
641- print_output_handler : Optional [ Callable [[Path , int , str ], None ]] = None ,
629+ lookup_directories : Path | str | Iterable [Path | str ] | None = None ,
630+ print_output_handler : Callable [[Path , int , str ], None ] | None = None ,
642631 allow_unregulated_fixed_port_id : bool = False ,
643632 omit_dependencies : bool = False ,
644633 ) -> pydsdl .Any :
@@ -677,7 +666,7 @@ def __init__(
677666 full_namespace : str ,
678667 namespace_dir : Path ,
679668 language_context : LanguageContext ,
680- parent : Optional [ "Namespace" ] = None ,
669+ parent : "Namespace | None" = None ,
681670 ):
682671 if full_namespace .startswith ("." ):
683672 full_namespace = full_namespace [1 :]
@@ -741,7 +730,7 @@ def output_path(self) -> Path:
741730 return self ._output_path
742731
743732 @property
744- def parent (self ) -> Optional [ "Namespace" ] :
733+ def parent (self ) -> "Namespace | None" :
745734 """
746735 The parent namespace of this namespace or None if this is a root namespace.
747736 """
@@ -934,14 +923,14 @@ def get_all_namespaces(self) -> Generator[Tuple["Namespace", Path], None, None]:
934923 """
935924 yield from self ._recursive_namespace_generator (self )
936925
937- def get_all_types (self ) -> Generator [Tuple [pydsdl .Any , Union [ Generatable , Path ] ], None , None ]:
926+ def get_all_types (self ) -> Generator [Tuple [pydsdl .Any , Generatable | Path ], None , None ]:
938927 """
939928 Generates tuples relating datatypes and nested namespaces at and below this
940929 namespace to the path for each type's generated output.
941930 """
942931 yield from self ._recursive_data_type_and_namespace_generator (self )
943932
944- def find_output_path_for_type (self , compound_type : Union [ "Namespace" , pydsdl .CompositeType ] ) -> Path :
933+ def find_output_path_for_type (self , compound_type : "Namespace | pydsdl.CompositeType" ) -> Path :
945934 """
946935 Searches the entire namespace tree to find a mapping of the type to an
947936 output file path.
@@ -958,7 +947,7 @@ def find_output_path_for_type(self, compound_type: Union["Namespace", pydsdl.Com
958947 return root_namespace ._bfs_search_for_output_path (compound_type ) # pylint: disable=protected-access
959948
960949 def add_data_type (
961- self , dsdl_type : pydsdl .CompositeType , input_types : List [pydsdl .CompositeType ], extension : Optional [ str ]
950+ self , dsdl_type : pydsdl .CompositeType , input_types : List [pydsdl .CompositeType ], extension : str | None
962951 ) -> Generatable :
963952 """
964953 Add a datatype to this namespace.
@@ -1098,7 +1087,7 @@ def _recursive_namespace_generator(cls, namespace: "Namespace") -> Generator[Tup
10981087 @classmethod
10991088 def _recursive_data_type_and_namespace_generator (
11001089 cls , namespace : "Namespace"
1101- ) -> Generator [Tuple [pydsdl .Any , Union [ Path , Generatable ] ], None , None ]:
1090+ ) -> Generator [Tuple [pydsdl .Any , Path | Generatable ], None , None ]:
11021091 yield (namespace , namespace .output_path )
11031092
11041093 for data_type , output_path in namespace .get_nested_types ():
@@ -1113,8 +1102,8 @@ def _recursive_data_type_and_namespace_generator(
11131102
11141103def build_namespace_tree (
11151104 types : List [pydsdl .CompositeType ],
1116- root_namespace_dir : Union [ str , Path ] ,
1117- output_dir : Union [ str , Path ] ,
1105+ root_namespace_dir : str | Path ,
1106+ output_dir : str | Path ,
11181107 language_context : LanguageContext ,
11191108) -> Namespace :
11201109 """
0 commit comments