本文整理汇总了Python中typing.typevar函数的典型用法代码示例。如果您正苦于以下问题:Python typevar函数的具体用法?Python typevar怎么用?Python typevar使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了typevar函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_generic_class_with_two_typeargs
def test_generic_class_with_two_typeargs(self):
t = typevar('t')
u = typevar('u')
class C(Generic[t, u]):
pass
self.assertIs(C[int, str], C)
self.assertIsInstance(C(), C)
self.assertIsInstance(C[int, str](), C)
示例2: test_generic_protocol
def test_generic_protocol(self):
t = typevar('t')
class P(Protocol[t]):
x = 1
class A:
x = 2
self.assertIsInstance(A(), P)
示例3: test_simple_generic_class
def test_simple_generic_class(self):
t = typevar('t')
class C(Generic[t]):
pass
self.assertIs(C[int], C)
self.assertIsInstance(C(), C)
self.assertIsInstance(C[int](), C)
示例4: test_abstract_generic_class
def test_abstract_generic_class(self):
t = typevar('t')
class C(AbstractGeneric[t]):
pass
class D:
pass
self.assertIs(C[int], C)
self.assertNotIsInstance(D(), C)
C.register(D)
self.assertIsInstance(D(), C)
示例5: test_typevar_in_overload
def test_typevar_in_overload(self):
t = typevar('t')
@overload
def f(x:t, y:str): return 1
@overload
def f(x, y): return 2
self.assertEqual(f((), 'x'), 1)
self.assertEqual(f((), 1.1), 2)
示例6: test_construct_class_with_abstract_method
def test_construct_class_with_abstract_method(self):
t = typevar('t')
class A(AbstractGeneric[t]):
@abstractmethod
def f(self): pass
class B(A):
def f(self): pass
with self.assertRaises(TypeError):
A()
B()
示例7: typevar
#!/usr/bin/env python3
import unittest
import random
import time
import pickle
import warnings
from math import log, exp, pi, fsum, sin
from test import support
from typing import Undefined, Any, Dict, List, Function, Generic, typevar
RT = typevar('RT', values=(random.Random, random.SystemRandom))
class TestBasicOps(unittest.TestCase, Generic[RT]):
# Superclass with tests common to all generators.
# Subclasses must arrange for self.gen to retrieve the Random instance
# to be tested.
gen = Undefined(RT) # Either Random or SystemRandom
def randomlist(self, n: int) -> List[float]:
"""Helper function to make a list of random numbers"""
return [self.gen.random() for i in range(n)]
def test_autoseed(self) -> None:
self.gen.seed()
state1 = self.gen.getstate()
time.sleep(0.1)
self.gen.seed() # diffent seeds at different times
state2 = self.gen.getstate()
示例8: typevar
from typing import typevar, Any
_FT = typevar("_FT")
def register(func: _FT, *args: Any, **kargs: Any) -> _FT:
pass
示例9: typevar
"""Generic abstract syntax tree node visitor"""
from typing import typevar, Generic
import mypy.nodes
T = typevar('T')
class NodeVisitor(Generic[T]):
"""Empty base class for parse tree node visitors.
The T type argument specifies the return type of the visit
methods. As all methods defined here return None by default,
subclasses do not always need to override all the methods.
TODO make the default return value explicit
"""
# Module structure
def visit_mypy_file(self, o: 'mypy.nodes.MypyFile') -> T:
pass
def visit_import(self, o: 'mypy.nodes.Import') -> T:
pass
def visit_import_from(self, o: 'mypy.nodes.ImportFrom') -> T:
pass
def visit_import_all(self, o: 'mypy.nodes.ImportAll') -> T:
pass
示例10: import
# Stubs for builtins
from typing import (
Undefined, typevar, AbstractGeneric, Iterator, Iterable, overload,
Sequence, Mapping, Tuple, List, Any, Dict, Function, Generic, Set,
AbstractSet, Sized, Reversible, SupportsInt, SupportsFloat, SupportsAbs,
SupportsRound, IO, builtinclass, ducktype, Union
)
from abc import abstractmethod, ABCMeta
# Note that names imported above are not automatically made visible via the
# implicit builtins import.
_T = typevar('_T')
_KT = typevar('_KT')
_VT = typevar('_VT')
_S = typevar('_S')
_T1 = typevar('_T1')
_T2 = typevar('_T2')
_T3 = typevar('_T3')
_T4 = typevar('_T4')
staticmethod = object() # Only valid as a decorator.
classmethod = object() # Only valid as a decorator.
property = object()
_byte_types = Union[bytes, bytearray]
@builtinclass
class object:
__doc__ = ''
示例11: typevar
# Builtins stub used in dictionary-related test cases.
from typing import typevar, Generic, Iterable, Iterator
T = typevar('T')
S = typevar('S')
class object:
def __init__(self) -> None: pass
class type: pass
class dict(Generic[T, S]): pass
class int: pass # for convenience
class str: pass # for keyword argument key type
class list(Iterable[T], Generic[T]): # needed by some test cases
def __iter__(self) -> Iterator[T]: pass
def __mul__(self, x: int) -> list[T]: pass
示例12: import
# Stubs for weakref
# NOTE: These are incomplete!
from typing import (
typevar, Generic, Any, Function, overload, Mapping, Iterator, Dict, Tuple,
Iterable
)
_T = typevar('_T')
_KT = typevar('_KT')
_VT = typevar('_VT')
class ReferenceType(Generic[_T]):
# TODO members
pass
def ref(o: _T, callback: Function[[ReferenceType[_T]],
Any] = None) -> ReferenceType[_T]: pass
# TODO callback
def proxy(object: _T) -> _T: pass
class WeakValueDictionary(Generic[_KT, _VT]):
# TODO tuple iterable argument?
@overload
def __init__(self) -> None: pass
@overload
def __init__(self, map: Mapping[_KT, _VT]) -> None: pass
def __len__(self) -> int: pass
示例13: test_typevar
def test_typevar(self):
t = typevar(u't')
self.assertEqual(t.name, u't')
示例14: import
"""Stubs for builtins"""
from typing import (
Undefined, typevar, AbstractGeneric, Iterator, Iterable, overload,
Sequence, Mapping, Tuple, List, Any, Dict, Function, Generic, Set,
AbstractSet, Sized, Reversible, SupportsInt, SupportsFloat, SupportsAbs,
SupportsRound, TextIO
)
from abc import abstractmethod, ABCMeta
T = typevar('T')
KT = typevar('KT')
VT = typevar('VT')
S = typevar('S')
T1 = typevar('T1')
T2 = typevar('T2')
T3 = typevar('T3')
T4 = typevar('T4')
staticmethod = object() # Only valid as a decorator.
property = object()
class object:
__doc__ = ''
__class__ = Undefined # type: type
def __init__(self) -> None: pass
def __eq__(self, o: object) -> bool: pass
示例15: cases
# These builtins stubs are used implicitly in parse-tree to icode generation
# test cases (testicodegen.py and test/data/icode-basic.test).
from typing import typevar, Generic
t = typevar('t')
class object:
def __init__(self) -> None: pass
class type: pass
class str: pass
# Primitive types are special in generated code.
class int:
def __add__(self, n: int) -> int: pass
def __sub__(self, n: int) -> int: pass
def __mul__(self, n: int) -> int: pass
def __neg__(self) -> int: pass
def __eq__(self, n: int) -> bool: pass
def __ne__(self, n: int) -> bool: pass
def __lt__(self, n: int) -> bool: pass
def __gt__(self, n: int) -> bool: pass
def __le__(self, n: int) -> bool: pass
def __ge__(self, n: int) -> bool: pass
class float: pass
class bool: pass
class list(Generic[t]): pass