本文整理匯總了Python中typing.overload方法的典型用法代碼示例。如果您正苦於以下問題:Python typing.overload方法的具體用法?Python typing.overload怎麽用?Python typing.overload使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類typing
的用法示例。
在下文中一共展示了typing.overload方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_typingOverloadAsync
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def test_typingOverloadAsync(self):
"""Allow intentional redefinitions via @typing.overload (async)"""
self.flakes("""
from typing import overload
@overload
async def f(s): # type: (None) -> None
pass
@overload
async def f(s): # type: (int) -> int
pass
async def f(s):
return s
""")
示例2: check_tuple
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def check_tuple(sub_validators: List[Validator[ResultT]]) -> Validator[Tuple[Any, ...]]:
def f(var_name: str, val: object) -> Tuple[Any, ...]:
if not isinstance(val, tuple):
raise ValidationError(_('{var_name} is not a tuple').format(var_name=var_name))
desired_len = len(sub_validators)
if desired_len != len(val):
raise ValidationError(_('{var_name} should have exactly {desired_len} items').format(
var_name=var_name, desired_len=desired_len,
))
for i, sub_validator in enumerate(sub_validators):
vname = f'{var_name}[{i}]'
sub_validator(vname, val[i])
return val
return f
# https://zulip.readthedocs.io/en/latest/testing/mypy.html#using-overload-to-accurately-describe-variations
示例3: test_overload_with_multiple_decorators
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def test_overload_with_multiple_decorators(self):
self.flakes("""
from typing import overload
dec = lambda f: f
@dec
@overload
def f(x): # type: (int) -> int
pass
@dec
@overload
def f(x): # type: (str) -> str
pass
@dec
def f(x): return x
""")
示例4: test_all
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def test_all(self):
from typing import __all__ as a
# Just spot-check the first and last of every category.
self.assertIn('AbstractSet', a)
self.assertIn('ValuesView', a)
self.assertIn('cast', a)
self.assertIn('overload', a)
if hasattr(contextlib, 'AbstractContextManager'):
self.assertIn('ContextManager', a)
# Check that io and re are not exported.
self.assertNotIn('io', a)
self.assertNotIn('re', a)
# Spot-check that stdlib modules aren't exported.
self.assertNotIn('os', a)
self.assertNotIn('sys', a)
# Check that Text is defined.
self.assertIn('Text', a)
# Check previously missing classes.
self.assertIn('SupportsBytes', a)
self.assertIn('SupportsComplex', a)
示例5: __iter__
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def __iter__(self) -> typing.Iterator[KeyInfo]:
"""Iterate over KeyInfo objects."""
for key_and_modifiers in self._iter_keys():
key = Qt.Key(int(key_and_modifiers) & ~Qt.KeyboardModifierMask)
modifiers = Qt.KeyboardModifiers( # type: ignore[call-overload]
int(key_and_modifiers) & Qt.KeyboardModifierMask)
yield KeyInfo(key=key, modifiers=modifiers)
示例6: overload
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def overload(x):
# type: (F) -> F
return x
示例7: overload
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def overload(x):
# type: (T) -> T
return x
示例8: __getitem__
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def __getitem__(self, index: int) -> Tensor:
if not self.atomic:
return self.tensors[index]
if index != 0:
raise IndexError('atomic batch allows index 0 only')
return self.tensor
# NOTE(sublee): pyflakes can't detect "overload" instead of "typing.overload".
示例9: overload
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def overload(f): # noqa: F811
return f
示例10: test_overload_fails
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def test_overload_fails(self):
from typing import overload
with self.assertRaises(RuntimeError):
@overload
def blah():
pass
示例11: test_all
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def test_all(self):
from typing import __all__ as a
# Just spot-check the first and last of every category.
assert 'AbstractSet' in a
assert 'ValuesView' in a
assert 'cast' in a
assert 'overload' in a
assert 'io' in a
assert 're' in a
# Spot-check that stdlib modules aren't exported.
assert 'os' not in a
assert 'sys' not in a
示例12: test_typingOverload
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def test_typingOverload(self):
"""Allow intentional redefinitions via @typing.overload"""
self.flakes("""
import typing
from typing import overload
@overload
def f(s): # type: (None) -> None
pass
@overload
def f(s): # type: (int) -> int
pass
def f(s):
return s
@typing.overload
def g(s): # type: (None) -> None
pass
@typing.overload
def g(s): # type: (int) -> int
pass
def g(s):
return s
""")
示例13: test_typingExtensionsOverload
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def test_typingExtensionsOverload(self):
"""Allow intentional redefinitions via @typing_extensions.overload"""
self.flakes("""
import typing_extensions
from typing_extensions import overload
@overload
def f(s): # type: (None) -> None
pass
@overload
def f(s): # type: (int) -> int
pass
def f(s):
return s
@typing_extensions.overload
def g(s): # type: (None) -> None
pass
@typing_extensions.overload
def g(s): # type: (int) -> int
pass
def g(s):
return s
""")
示例14: test_overload_in_class
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def test_overload_in_class(self):
self.flakes("""
from typing import overload
class C:
@overload
def f(self, x): # type: (int) -> int
pass
@overload
def f(self, x): # type: (str) -> str
pass
def f(self, x): return x
""")
示例15: test_overload_fails
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import overload [as 別名]
def test_overload_fails(self):
from typing import overload
with self.assertRaises(RuntimeError):
@overload
def blah():
pass
blah()