Python typing.Generic() Examples
The following are 30
code examples of typing.Generic().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
typing
, or try the search function
.
Example #1
Source File: test_dataclasses.py From dataclasses with Apache License 2.0 | 7 votes |
def test_generic_extending(self): S = TypeVar('S') T = TypeVar('T') @dataclass class Base(Generic[T, S]): x: T y: S @dataclass class DataDerived(Base[int, T]): new_field: str Alias = DataDerived[str] c = Alias(0, 'test1', 'test2') self.assertEqual(astuple(c), (0, 'test1', 'test2')) class NonDataDerived(Base[int, T]): def new_method(self): return self.y Alias = NonDataDerived[float] c = Alias(10, 1.0) self.assertEqual(c.new_method(), 1.0)
Example #2
Source File: test_typing.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_any_is_subclass(self): # Any should be considered a subclass of everything. assert issubclass(Any, Any) assert issubclass(Any, typing.List) assert issubclass(Any, typing.List[int]) assert issubclass(Any, typing.List[T]) assert issubclass(Any, typing.Mapping) assert issubclass(Any, typing.Mapping[str, int]) assert issubclass(Any, typing.Mapping[KT, VT]) assert issubclass(Any, Generic) assert issubclass(Any, Generic[T]) assert issubclass(Any, Generic[KT, VT]) assert issubclass(Any, AnyStr) assert issubclass(Any, Union) assert issubclass(Any, Union[int, str]) assert issubclass(Any, typing.Match) assert issubclass(Any, typing.Match[str]) # These expressions must simply not fail. typing.Match[Any] typing.Pattern[Any] typing.IO[Any]
Example #3
Source File: test_typing.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_pickle(self): T = TypeVar('T') class B(Generic[T]): pass global C # pickle wants to reference the class by name class C(B[int]): pass c = C() c.foo = 42 c.bar = 'abc' for proto in range(pickle.HIGHEST_PROTOCOL + 1): z = pickle.dumps(c, proto) x = pickle.loads(z) self.assertEqual(x.foo, 42) self.assertEqual(x.bar, 'abc') self.assertEqual(x.__dict__, {'foo': 42, 'bar': 'abc'})
Example #4
Source File: test_dataclasses.py From dataclasses with Apache License 2.0 | 6 votes |
def test_generic_dynamic(self): T = TypeVar('T') @dataclass class Parent(Generic[T]): x: T Child = make_dataclass('Child', [('y', T), ('z', Optional[T], None)], bases=(Parent[int], Generic[T]), namespace={'other': 42}) self.assertIs(Child[int](1, 2).z, None) self.assertEqual(Child[int](1, 2, 3).z, 3) self.assertEqual(Child[int](1, 2, 3).other, 42) # Check that type aliases work correctly. Alias = Child[T] self.assertEqual(Alias[int](1, 2).x, 1) # Check MRO resolution. self.assertEqual(Child.__mro__, (Child, Parent, Generic, object))
Example #5
Source File: test_typing.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_type_erasure(self): T = TypeVar('T') class Node(Generic[T]): def __init__(self, label: T, left: 'Node[T]' = None, right: 'Node[T]' = None): self.label = label # type: T self.left = left # type: Optional[Node[T]] self.right = right # type: Optional[Node[T]] def foo(x: T): a = Node(x) b = Node[T](x) c = Node[Any](x) self.assertIs(type(a), Node) self.assertIs(type(b), Node) self.assertIs(type(c), Node) self.assertEqual(a.label, x) self.assertEqual(b.label, x) self.assertEqual(c.label, x) foo(42)
Example #6
Source File: test_typing.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_type_erasure(self): T = TypeVar('T') class Node(Generic[T]): def __init__(self, label: T, left: 'Node[T]' = None, right: 'Node[T]' = None): self.label = label # type: T self.left = left # type: Optional[Node[T]] self.right = right # type: Optional[Node[T]] def foo(x: T): a = Node(x) b = Node[T](x) c = Node[Any](x) assert type(a) is Node assert type(b) is Node assert type(c) is Node foo(42)
Example #7
Source File: test_six.py From six with MIT License | 6 votes |
def test_with_metaclass_typing(): try: import typing except ImportError: pytest.skip("typing module required") class Meta(type): pass if sys.version_info[:2] < (3, 7): # Generics with custom metaclasses were broken on older versions. class Meta(Meta, typing.GenericMeta): pass T = typing.TypeVar('T') class G(six.with_metaclass(Meta, typing.Generic[T])): pass class GA(six.with_metaclass(abc.ABCMeta, typing.Generic[T])): pass assert isinstance(G, Meta) assert isinstance(GA, abc.ABCMeta) assert G[int] is not G[G[int]] assert GA[int] is not GA[GA[int]] assert G.__bases__ == (typing.Generic,) assert G.__orig_bases__ == (typing.Generic[T],)
Example #8
Source File: test_typing.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_generic_errors(self): T = TypeVar('T') S = TypeVar('S') with self.assertRaises(TypeError): Generic[T]() with self.assertRaises(TypeError): Generic[T][T] with self.assertRaises(TypeError): Generic[T][S] with self.assertRaises(TypeError): isinstance([], List[int]) with self.assertRaises(TypeError): issubclass(list, List[int]) with self.assertRaises(TypeError): class NewGeneric(Generic): ... with self.assertRaises(TypeError): class MyGeneric(Generic[T], Generic[S]): ... with self.assertRaises(TypeError): class MyGeneric(List[T], Generic[S]): ...
Example #9
Source File: test_typing.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_chain_repr(self): T = TypeVar('T') S = TypeVar('S') class C(Generic[T]): pass X = C[Tuple[S, T]] self.assertEqual(X, C[Tuple[S, T]]) self.assertNotEqual(X, C[Tuple[T, S]]) Y = X[T, int] self.assertEqual(Y, X[T, int]) self.assertNotEqual(Y, X[S, int]) self.assertNotEqual(Y, X[T, str]) Z = Y[str] self.assertEqual(Z, Y[str]) self.assertNotEqual(Z, Y[int]) self.assertNotEqual(Z, Y[T]) self.assertTrue(str(Z).endswith( '.C[typing.Tuple[str, int]]'))
Example #10
Source File: test_typing.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_pickle(self): global C # pickle wants to reference the class by name T = TypeVar('T') class B(Generic[T]): pass class C(B[int]): pass c = C() c.foo = 42 c.bar = 'abc' for proto in range(pickle.HIGHEST_PROTOCOL + 1): z = pickle.dumps(c, proto) x = pickle.loads(z) self.assertEqual(x.foo, 42) self.assertEqual(x.bar, 'abc') self.assertEqual(x.__dict__, {'foo': 42, 'bar': 'abc'}) simples = [Any, Union, Tuple, Callable, ClassVar, List, typing.Iterable] for s in simples: for proto in range(pickle.HIGHEST_PROTOCOL + 1): z = pickle.dumps(s, proto) x = pickle.loads(z) self.assertEqual(s, x)
Example #11
Source File: _activity.py From ballistica with MIT License | 6 votes |
def _setup_player_and_team_types(self) -> None: """Pull player and team types from our typing.Generic params.""" # TODO: There are proper calls for pulling these in Python 3.8; # should update this code when we adopt that. # NOTE: If we get Any as PlayerType or TeamType (generally due # to no generic params being passed) we automatically use the # base class types, but also warn the user since this will mean # less type safety for that class. (its better to pass the base # player/team types explicitly vs. having them be Any) if not TYPE_CHECKING: self._playertype = type(self).__orig_bases__[-1].__args__[0] if not isinstance(self._playertype, type): self._playertype = Player print(f'ERROR: {type(self)} was not passed a Player' f' type argument; please explicitly pass ba.Player' f' if you do not want to override it.') self._teamtype = type(self).__orig_bases__[-1].__args__[1] if not isinstance(self._teamtype, type): self._teamtype = Team print(f'ERROR: {type(self)} was not passed a Team' f' type argument; please explicitly pass ba.Team' f' if you do not want to override it.') assert issubclass(self._playertype, Player) assert issubclass(self._teamtype, Team)
Example #12
Source File: test_typing_annotations.py From typecheck-decorator with BSD 2-Clause "Simplified" License | 6 votes |
def test_MyGeneric_OK_and_not_OK(): for element1 in diverse_collection: for element2 in diverse_collection: if type(element1) == type(element2): continue mygen = MyGeneric(element1) mygen.append(element1) # binds X mygen.append(element1) # checks X binding: OK print(element1, element2) if (issubclass(type(element1), type(element2)) or issubclass(type(element2), type(element1))): mygen.append(element2) # conforms to X binding else: with expected(tc.InputParameterError("")): mygen.append(element2) # violates X binding # TODO: test Generic class with multiple inheritance ############################################################################ # type variable with bound or constraint
Example #13
Source File: _typing.py From pytablewriter with MIT License | 6 votes |
def _get_protocol_attrs(cls): attrs = set() for base in cls.__mro__[:-1]: # without object if base.__name__ in ('Protocol', 'Generic'): continue annotations = getattr(base, '__annotations__', {}) for attr in list(base.__dict__.keys()) + list(annotations.keys()): if (not attr.startswith('_abc_') and attr not in ( '__abstractmethods__', '__annotations__', '__weakref__', '_is_protocol', '_is_runtime_protocol', '__dict__', '__args__', '__slots__', '__next_in_mro__', '__parameters__', '__origin__', '__orig_bases__', '__extra__', '__tree_hash__', '__doc__', '__subclasshook__', '__init__', '__new__', '__module__', '_MutableMapping__marker', '_gorg')): attrs.add(attr) return attrs
Example #14
Source File: _typing.py From pytablewriter with MIT License | 6 votes |
def __class_getitem__(cls, params): if not isinstance(params, tuple): params = (params,) if not params and cls is not Tuple: raise TypeError( "Parameter list to {}[...] cannot be empty".format(cls.__qualname__)) msg = "Parameters to generic types must be types." params = tuple(_type_check(p, msg) for p in params) if cls is Protocol: # Generic can only be subscripted with unique type variables. if not all(isinstance(p, TypeVar) for p in params): i = 0 while isinstance(params[i], TypeVar): i += 1 raise TypeError( "Parameters to Protocol[...] must all be type variables." " Parameter {} is {}".format(i + 1, params[i])) if len(set(params)) != len(params): raise TypeError( "Parameters to Protocol[...] must all be unique") else: # Subscripting a regular Generic subclass. _check_generic(cls, params) return _GenericAlias(cls, params)
Example #15
Source File: request_components.py From alexa-skills-kit-sdk-for-python with Apache License 2.0 | 6 votes |
def get_request_handler_chain(self, handler_input): # type: (Input) -> Union[GenericRequestHandlerChain, None] """Get the request handler chain that can handle the dispatch input. :param handler_input: Generic input passed to the dispatcher. :type handler_input: Input :return: Handler Chain that can handle the input. :rtype: Union[None, GenericRequestHandlerChain] """ for chain in self.request_handler_chains: handler = chain.request_handler # type: AbstractRequestHandler if handler.can_handle(handler_input=handler_input): return chain return None
Example #16
Source File: exception_components.py From alexa-skills-kit-sdk-for-python with Apache License 2.0 | 6 votes |
def get_handler(self, handler_input, exception): # type: (Input, Exception) -> Union[AbstractExceptionHandler, None] """Get the exception handler that can handle the input and exception. :param handler_input: Generic input passed to the dispatcher. :type handler_input: Input :param exception: Exception thrown by :py:class:`ask_sdk_runtime.dispatch.GenericRequestDispatcher` dispatch method. :type exception: Exception :return: Exception Handler that can handle the input or None. :rtype: Union[None, ask_sdk_runtime.dispatch_components.exception_components.AbstractExceptionHandler] """ for handler in self.exception_handlers: if handler.can_handle( handler_input=handler_input, exception=exception): return handler return None
Example #17
Source File: test_typing.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_parameterized_slots(self): T = TypeVar('T') class C(Generic[T]): __slots__ = ('potato',) c = C() c_int = C[int]() self.assertEqual(C.__slots__, C[str].__slots__) c.potato = 0 c_int.potato = 0 with self.assertRaises(AttributeError): c.tomato = 0 with self.assertRaises(AttributeError): c_int.tomato = 0 def foo(x: C['C']): ... self.assertEqual(get_type_hints(foo, globals(), locals())['x'], C[C]) self.assertEqual(get_type_hints(foo, globals(), locals())['x'].__slots__, C.__slots__) self.assertEqual(copy(C[int]), deepcopy(C[int]))
Example #18
Source File: types_generics.py From pydantic with MIT License | 6 votes |
def validate(cls, v, field: ModelField): if not isinstance(v, cls): # The value is not even a TastingModel raise TypeError('Invalid value') if not field.sub_fields: # Generic parameters were not provided so we don't try to validate # them and just return the value as is return v aged_f = field.sub_fields[0] quality_f = field.sub_fields[1] errors = [] # Here we don't need the validated value, but we want the errors valid_value, error = aged_f.validate(v.aged, {}, loc='aged') if error: errors.append(error) # Here we don't need the validated value, but we want the errors valid_value, error = quality_f.validate(v.quality, {}, loc='quality') if error: errors.append(error) if errors: raise ValidationError(errors, cls) # Validation passed without errors, return the same instance received return v
Example #19
Source File: test_typing.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_dict(self): T = TypeVar('T') class B(Generic[T]): pass b = B() b.foo = 42 self.assertEqual(b.__dict__, {'foo': 42}) class C(B[int]): pass c = C() c.bar = 'abc' self.assertEqual(c.__dict__, {'bar': 'abc'})
Example #20
Source File: test_typing_annotations.py From typecheck-decorator with BSD 2-Clause "Simplified" License | 5 votes |
def test_TypeVar_constraint_not_OK(): with (expected(tc.InputParameterError(""))): foo_with_constraint("str1", b"bytes1") with (expected(tc.InputParameterError(""))): foo_with_constraint(("b","y"), ("t","e")) ############################################################################ # Generic classes subclass relationship:
Example #21
Source File: test_typing_annotations.py From typecheck-decorator with BSD 2-Clause "Simplified" License | 5 votes |
def test_Sequence_int_with_wrong_result(): with expected(tc.ReturnValueError( "has returned an incompatible value: [1, '2']")): foo_Sequence_int_to_List_int([1], "2") ############################################################################ # Generic stand-alone functions
Example #22
Source File: test_typing_annotations.py From typecheck-decorator with BSD 2-Clause "Simplified" License | 5 votes |
def test_typing_module_weirdness(): # This was a Py3.4 bug in typing 3.5.0.1: assert issubclass(tg.Iterable, tg.Generic) == (sys.version_info >= (3,5)) # ItemsView comes out with three parameters: # one (T_co) from Iterable (via MappingView), # two (KT, VT_co) from Generic. # T_co must in fact be Tuple[KT, VT_co], but how would we know that? # Three parameters makes no sense; this is a mess. assert tg.ItemsView.__parameters__ == (tg.T_co, tg.KT, tg.VT_co) # This is an assumption in GenericMetaChecker._can_have_instance: assert not issubclass(tg.Sequence[X], tg.Iterable[X]) # very strange! ############################################################################ # Generic type with fixed content type
Example #23
Source File: test_typing_annotations.py From typecheck-decorator with BSD 2-Clause "Simplified" License | 5 votes |
def test_Sequence_X_int_notOK(): with expected(tc.InputParameterError( "foo_Sequence_X_to_Sequence_X() has got an incompatible value for x: a_string")): foo_Sequence_X_to_Sequence_X([1, 2], "a_string") ############################################################################ # TypeVarNamespace, Generic class with TypeVar binding on the instance level
Example #24
Source File: test_typing.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_init(self): T = TypeVar('T') S = TypeVar('S') with self.assertRaises(TypeError): Generic[T, T] with self.assertRaises(TypeError): Generic[T, S, T]
Example #25
Source File: _typing.py From pytablewriter with MIT License | 5 votes |
def __getitem__(self, params): # We also need to copy this from GenericMeta.__getitem__ to get # special treatment of "Protocol". (Comments removed for brevity.) if not isinstance(params, tuple): params = (params,) if not params and _gorg(self) is not Tuple: raise TypeError( "Parameter list to %s[...] cannot be empty" % self.__qualname__) msg = "Parameters to generic types must be types." params = tuple(_type_check(p, msg) for p in params) if self in (Generic, Protocol): if not all(isinstance(p, TypeVar) for p in params): raise TypeError( "Parameters to %r[...] must all be type variables" % self) if len(set(params)) != len(params): raise TypeError( "Parameters to %r[...] must all be unique" % self) tvars = params args = params elif self in (Tuple, Callable): tvars = _type_vars(params) args = params elif self.__origin__ in (Generic, Protocol): raise TypeError("Cannot subscript already-subscripted %s" % repr(self)) else: _check_generic(self, params) tvars = _type_vars(params) args = params prepend = (self,) if self.__origin__ is None else () return self.__class__(self.__name__, prepend + self.__bases__, _no_slots_copy(self.__dict__), tvars=tvars, args=args, origin=self, extra=self.__extra__, orig_bases=self.__orig_bases__)
Example #26
Source File: request_components.py From alexa-skills-kit-sdk-for-python with Apache License 2.0 | 5 votes |
def process(self, handler_input, response): # type: (Input, Output) -> None """Process the input and the output after the Handler is run. :param handler_input: Generic input passed to the dispatcher. :type handler_input: Input :param response: Execution result of the Handler on dispatch input. :type response: Union[None, Output] :rtype: None """ raise NotImplementedError
Example #27
Source File: request_components.py From alexa-skills-kit-sdk-for-python with Apache License 2.0 | 5 votes |
def can_handle(self, handler_input): # type: (Input) -> bool """Returns true if Request Handler can handle the dispatch input. :param handler_input: Generic input passed to the dispatcher. :type handler_input: Input :return: Boolean value that tells the dispatcher if the current input can be handled by this handler. :rtype: bool """ raise NotImplementedError
Example #28
Source File: request_components.py From alexa-skills-kit-sdk-for-python with Apache License 2.0 | 5 votes |
def handle(self, handler_input): # type: (Input) -> Union[None, Output] """Handles the dispatch input and provides an output for dispatcher to return. :param handler_input: Generic input passed to the dispatcher. :type handler_input: Input :return: Generic Output for the dispatcher to return or None :rtype: Union[Output, None] """ raise NotImplementedError
Example #29
Source File: request_components.py From alexa-skills-kit-sdk-for-python with Apache License 2.0 | 5 votes |
def process(self, handler_input): # type: (Input) -> None """Process the input before the Handler is run. :param handler_input: Generic input passed to the dispatcher. :type handler_input: Input :rtype: None """ raise NotImplementedError
Example #30
Source File: _typing.py From pytablewriter with MIT License | 5 votes |
def _next_in_mro(cls): # noqa """This function exists for compatibility with old typing versions.""" next_in_mro = object for i, c in enumerate(cls.__mro__[:-1]): if isinstance(c, GenericMeta) and _gorg(c) is Generic: next_in_mro = cls.__mro__[i + 1] return next_in_mro