本文整理匯總了Python中typing.get_args方法的典型用法代碼示例。如果您正苦於以下問題:Python typing.get_args方法的具體用法?Python typing.get_args怎麽用?Python typing.get_args使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類typing
的用法示例。
在下文中一共展示了typing.get_args方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_section_configuration_matches_type_specification
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import get_args [as 別名]
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)
示例2: get_args
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import get_args [as 別名]
def get_args(t):
return getattr(t, "__args__", None)
示例3: get_subtypes
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import get_args [as 別名]
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)
示例4: get_args
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import get_args [as 別名]
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))
示例5: _ti_get_args
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import get_args [as 別名]
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.