本文整理匯總了Python中typing.UnionMeta方法的典型用法代碼示例。如果您正苦於以下問題:Python typing.UnionMeta方法的具體用法?Python typing.UnionMeta怎麽用?Python typing.UnionMeta使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類typing
的用法示例。
在下文中一共展示了typing.UnionMeta方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: type_cmp
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import UnionMeta [as 別名]
def type_cmp(t1, t2):
if t1 is AnyType and t2 is not AnyType:
return False
if t2 is AnyType and t1 is not AnyType:
return False
if t1 == t2:
return t1
if typing:
if isinstance(t1, typing.UnionMeta) and isinstance(t2, typing.UnionMeta):
common = t1.__union_set_params__ & t2.__union_set_params__
if common:
return next(iter(common))
elif isinstance(t1, typing.UnionMeta) and t2 in t1.__union_params__:
return t2
elif isinstance(t2, typing.UnionMeta) and t1 in t2.__union_params__:
return t1
return False
示例2: _get_param_value
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import UnionMeta [as 別名]
def _get_param_value(self, param):
"""Get the converted value for an inspect.Parameter."""
value = getattr(self.namespace, param.name)
typ = self._get_type(param)
if isinstance(typ, tuple):
raise TypeError("{}: Legacy tuple type annotation!".format(
self.name))
if hasattr(typing, 'UnionMeta'):
# Python 3.5.2
# pylint: disable=no-member,useless-suppression
is_union = isinstance(
typ, typing.UnionMeta) # type: ignore[attr-defined]
else:
is_union = getattr(typ, '__origin__', None) is typing.Union
if is_union:
# this is... slightly evil, I know
try:
types = list(typ.__args__)
except AttributeError:
# Python 3.5.2
types = list(typ.__union_params__)
# pylint: enable=no-member,useless-suppression
if param.default is not inspect.Parameter.empty:
types.append(type(param.default))
choices = self.get_arg_info(param).choices
value = argparser.multitype_conv(param, types, value,
str_choices=choices)
elif typ is str:
choices = self.get_arg_info(param).choices
value = argparser.type_conv(param, typ, value, str_choices=choices)
elif typ is bool: # no type conversion for flags
assert isinstance(value, bool)
elif typ is None:
pass
else:
value = argparser.type_conv(param, typ, value)
return value
示例3: is_Union
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import UnionMeta [as 別名]
def is_Union(tp):
"""Python version independent check if a type is typing.Union.
Tested with CPython 2.7, 3.5, 3.6 and Jython 2.7.1.
"""
if tp is Union:
return True
try:
# Python 3.6
return tp.__origin__ is Union
except AttributeError:
try:
return isinstance(tp, typing.UnionMeta)
except AttributeError:
return False