本文整理匯總了Python中typing._ForwardRef方法的典型用法代碼示例。如果您正苦於以下問題:Python typing._ForwardRef方法的具體用法?Python typing._ForwardRef怎麽用?Python typing._ForwardRef使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類typing
的用法示例。
在下文中一共展示了typing._ForwardRef方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: resolve_annotations
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import _ForwardRef [as 別名]
def resolve_annotations(raw_annotations: Dict[str, Type[Any]], module_name: Optional[str]) -> Dict[str, Type[Any]]:
"""
Partially taken from typing.get_type_hints.
Resolve string or ForwardRef annotations into type objects if possible.
"""
if module_name:
base_globals: Optional[Dict[str, Any]] = sys.modules[module_name].__dict__
else:
base_globals = None
annotations = {}
for name, value in raw_annotations.items():
if isinstance(value, str):
if sys.version_info >= (3, 7):
value = ForwardRef(value, is_argument=False)
else:
value = ForwardRef(value)
try:
value = _eval_type(value, base_globals, None)
except NameError:
# this is ok, it can be fixed with update_forward_refs
pass
annotations[name] = value
return annotations
示例2: test_forwardref_instance_type_error
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import _ForwardRef [as 別名]
def test_forwardref_instance_type_error(self):
fr = typing._ForwardRef('int')
with self.assertRaises(TypeError):
isinstance(42, fr)
示例3: test_forwardref_subclass_type_error
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import _ForwardRef [as 別名]
def test_forwardref_subclass_type_error(self):
fr = typing._ForwardRef('int')
with self.assertRaises(TypeError):
issubclass(int, fr)
示例4: test_forward_equality
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import _ForwardRef [as 別名]
def test_forward_equality(self):
fr = typing._ForwardRef('int')
self.assertEqual(fr, typing._ForwardRef('int'))
self.assertNotEqual(List['int'], List[int])
示例5: test_forward_repr
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import _ForwardRef [as 別名]
def test_forward_repr(self):
self.assertEqual(repr(List['int']), "typing.List[_ForwardRef('int')]")
示例6: get_forward_ref_type
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import _ForwardRef [as 別名]
def get_forward_ref_type() -> Any:
# ignoring mypy on the import as it catches (_)ForwardRef as invalid, use for
# introspection is valid:
# https://docs.python.org/3/library/typing.html#typing.ForwardRef
if "ForwardRef" in dir(typing):
return typing.ForwardRef # type: ignore
return typing._ForwardRef # type: ignore
示例7: ensure_forward_ref
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import _ForwardRef [as 別名]
def ensure_forward_ref(self, service, factory, instance, **kwargs):
if isinstance(service, str):
self.register(ForwardRef(service), factory, instance, **kwargs)
示例8: evaluate_forwardref
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import _ForwardRef [as 別名]
def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:
return type_._eval_type(globalns, localns)
示例9: update_field_forward_refs
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import _ForwardRef [as 別名]
def update_field_forward_refs(field: 'ModelField', globalns: Any, localns: Any) -> None:
"""
Try to update ForwardRefs on fields based on this ModelField, globalns and localns.
"""
if field.type_.__class__ == ForwardRef:
field.type_ = evaluate_forwardref(field.type_, globalns, localns or None)
field.prepare()
if field.sub_fields:
for sub_f in field.sub_fields:
update_field_forward_refs(sub_f, globalns=globalns, localns=localns)
示例10: resolve_fw_decl
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import _ForwardRef [as 別名]
def resolve_fw_decl(in_type, module_name=None, globs=None, level=0,
search_stack_depth=2):
'''Resolves forward references in ``in_type``, see
https://www.python.org/dev/peps/pep-0484/#forward-references.
Note:
``globs`` should be a dictionary containing values for the names
that must be resolved in ``in_type``. If ``globs`` is not provided, it
will be created by ``__globals__`` from the module named ``module_name``,
plus ``__locals__`` from the last ``search_stack_depth`` stack frames (Default: 2),
beginning at the calling function. This is to resolve cases where ``in_type`` and/or
types it fw-references are defined inside a function.
To prevent walking the stack, set ``search_stack_depth=0``.
Ideally provide a proper ``globs`` for best efficiency.
See ``util.get_function_perspective_globals`` for obtaining a ``globs`` that can be
cached. ``util.get_function_perspective_globals`` works like described above.
'''
# Also see discussion at https://github.com/Stewori/pytypes/pull/43
if in_type in _fw_resolve_cache:
return _fw_resolve_cache[in_type], True
if globs is None:
#if not module_name is None:
globs = util.get_function_perspective_globals(module_name, level+1,
level+1+search_stack_depth)
if isinstance(in_type, _basestring):
# For the case that a pure forward ref is given as string
out_type = eval(in_type, globs)
_fw_resolve_cache[in_type] = out_type
return out_type, True
elif isinstance(in_type, ForwardRef):
# Todo: Mabe somehow get globs from in_type.__forward_code__
if not in_type.__forward_evaluated__:
in_type.__forward_value__ = eval(in_type.__forward_arg__, globs)
in_type.__forward_evaluated__ = True
return in_type, True
elif is_Tuple(in_type):
return in_type, any([resolve_fw_decl(in_tp, None, globs)[1] \
for in_tp in get_Tuple_params(in_type)])
elif is_Union(in_type):
return in_type, any([resolve_fw_decl(in_tp, None, globs)[1] \
for in_tp in get_Union_params(in_type)])
elif is_Callable(in_type):
args, res = get_Callable_args_res(in_type)
ret = any([resolve_fw_decl(in_tp, None, globs)[1] \
for in_tp in args])
ret = resolve_fw_decl(res, None, globs)[1] or ret
return in_type, ret
elif hasattr(in_type, '__args__') and in_type.__args__ is not None:
return in_type, any([resolve_fw_decl(in_tp, None, globs)[1] \
for in_tp in in_type.__args__])
return in_type, False