Python typing.get_args() Examples
The following are 5
code examples of typing.get_args().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
typing
, or try the search function
.
Example #1
Source File: test_constants.py From bot with MIT License | 6 votes |
def test_section_configuration_matches_type_specification(self): """"The section annotations should match the actual types of the sections.""" sections = ( cls for (name, cls) in inspect.getmembers(constants) if hasattr(cls, 'section') and isinstance(cls, type) ) for section in sections: for name, annotation in section.__annotations__.items(): with self.subTest(section=section.__name__, name=name, annotation=annotation): value = getattr(section, name) origin = typing.get_origin(annotation) annotation_args = typing.get_args(annotation) failure_msg = f"{value} is not an instance of {annotation}" if origin is typing.Union: is_instance = is_any_instance(value, annotation_args) self.assertTrue(is_instance, failure_msg) else: is_instance = is_annotation_instance(value, annotation) self.assertTrue(is_instance, failure_msg)
Example #2
Source File: data.py From dffml with MIT License | 5 votes |
def get_args(t): return getattr(t, "__args__", None)
Example #3
Source File: configuration.py From shaka-streamer with Apache License 2.0 | 5 votes |
def get_subtypes( type: Optional[Type]) -> Tuple[Optional[Type], Optional[Type]]: """For Dict hints, returns (keytype, valuetype). For List hints, returns (None, valuetype). For everything else, returns (None, None).""" # In Python 3.8+, you can use typing.get_args. It returns () if "type" # is something like "str" or "int" instead of "typing.List" or # "typing.Dict". if hasattr(typing, 'get_args'): args = typing.get_args(type) # type: ignore elif hasattr(type, '__args__'): # Before Python 3.8, you can use this undocumented attribute to get the # type parameters. If this doesn't exist, you are probably dealing with a # basic type like "str" or "int". args = type.__args__ # type: ignore else: args = () underlying = Field.get_underlying_type(type) if underlying is dict: return args if underlying is list: return (None, args[0]) return (None, None)
Example #4
Source File: schema.py From spins-b with GNU General Public License v3.0 | 5 votes |
def get_args(cls) -> Tuple: if typing_inspect.is_union_type(cls): try: return cls.__union_params__ except AttributeError: pass return cls.__args__ elif issubclass(cls, List): return cls.__args__ else: raise ValueError("Cannot get type arguments for {}".format(cls))
Example #5
Source File: defopt.py From defopt with MIT License | 5 votes |
def _ti_get_args(tp): import typing_inspect as ti if type(tp) is type(Literal): # Py<=3.6. return tp.__values__ return ti.get_args(tp, evaluate=True) # evaluate=True default on Py>=3.7.