Python typing.TypeVar() Examples

The following are 30 code examples of typing.TypeVar(). 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 vote down vote up
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 vote down vote up
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 #3
Source File: test_six.py    From six with MIT License 6 votes vote down vote up
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 #4
Source File: test_dataclasses.py    From dataclasses with Apache License 2.0 6 votes vote down vote up
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: custom_typing.py    From cotk with Apache License 2.0 6 votes vote down vote up
def _stringify(annotation: Any) -> str:
    """Stringify type annotation object."""
    if isinstance(annotation, str):
        return annotation
    elif isinstance(annotation, TypeVar):  # type: ignore
        return annotation.__name__
    elif not annotation:
        return repr(annotation)
    elif annotation is NoneType:  # type: ignore
        return 'None'
    elif getattr(annotation, '__module__', None) == 'builtins':
        return annotation.__qualname__
    elif annotation is Ellipsis:
        return '...'

    if sys.version_info >= (3, 7):  # py37+
        return _stringify_py37(annotation)
    else:
        return _stringify_py36(annotation) 
Example #6
Source File: trainer.py    From torecsys with MIT License 6 votes vote down vote up
def set_targets_name(self, targets_name: str) -> TypeVar("Trainer"):
        r"""Set targets_name of the trainer.
        
        Args:
            targets_name (str): targets name to be setted for getting targets field in batch.
        
        Raises:
            TypeError: when type of targets_name is not allowed.
        
        Returns:
            torecsys.trainer.Trainer: self
        """
        if not isinstance(targets_name, str):
            raise TypeError(f"{type(targets_name).__name__} not allowed.")

        self._targets_name = targets_name
        return self 
Example #7
Source File: typing_api.py    From dagster with Apache License 2.0 6 votes vote down vote up
def is_closed_python_set_type(ttype):
    '''
    A "closed" generic type has all of its type parameters parameterized
    by other closed or concrete types.

    e.g.

    Returns true for Set[string] but false for Set or set
    '''
    if ttype is None:
        return False
    if ttype is typing.Set:
        return False
    if not hasattr(ttype, '__args__'):
        return False
    if ttype.__args__ is None or len(ttype.__args__) != 1:
        return False

    inner_type = ttype.__args__[0]
    origin = _get_origin(ttype)

    return (origin == typing.Set or origin is set) and not isinstance(inner_type, typing.TypeVar) 
Example #8
Source File: test_typing.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
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 #9
Source File: index_field.py    From torecsys with MIT License 6 votes vote down vote up
def build_vocab(self, dataset: List[List[int]]) -> TypeVar("IndexField"):

        # Get current max token in vocab_dict
        max_tkn = self.current_max_token

        if isinstance(dataset, list):
            # Loop through build_vocab
            for element in dataset:
                if isinstance(element, list) or isinstance(element, int):
                    self.build_vocab(element)
                else:
                    raise TypeError(f"{type(element).__name__} not allowed.")
        
        elif isinstance(dataset, int):
            max_tkn += 1
            if dataset not in self.vocab_dict:
                self.vocab_dict[dataset] = max_tkn
        
        else:
            raise TypeError(f"{type(dataset).__name__} not allowed.")
        
        # Update inverse_vocab_dict
        self._build_inverse_dict()
        
        return self 
Example #10
Source File: gltf2_blender_gather_animations.py    From glTF-Blender-IO with Apache License 2.0 6 votes vote down vote up
def __link_samplers(animation: gltf2_io.Animation, export_settings):
    """
    Move animation samplers to their own list and store their indices at their previous locations.

    After gathering, samplers are stored in the channels properties of the animation and need to be moved
    to their own list while storing an index into this list at the position where they previously were.
    This behaviour is similar to that of the glTFExporter that traverses all nodes
    :param animation:
    :param export_settings:
    :return:
    """
    # TODO: move this to some util module and update gltf2 exporter also
    T = typing.TypeVar('T')

    def __append_unique_and_get_index(l: typing.List[T], item: T):
        if item in l:
            return l.index(item)
        else:
            index = len(l)
            l.append(item)
            return index

    for i, channel in enumerate(animation.channels):
        animation.channels[i].sampler = __append_unique_and_get_index(animation.samplers, channel.sampler) 
Example #11
Source File: trainer.py    From torecsys with MIT License 6 votes vote down vote up
def bind_inputs(self, inputs: _Inputs) -> TypeVar("Trainer"):
        r"""Bind inputs to the trainer.inputs
        
        Args:
            inputs (torecsys.inputs.base._Inputs): Inputs to be binded to the trainer.inputs.
        
        Raises:
            TypeError: whether type of inputs is not allowed to be setted.
        
        Returns:
            torecsys.trainer.Trainer: self
        """
        # Check if the type of inputs is allowed
        if not isinstance(inputs, _Inputs):
            raise TypeError("inputs must be a torecsys.inputs.base._Inputs object.")
        
        # Bind inputs to _inputs
        self._inputs = inputs

        # Apply cuda to inputs if is_cuda is True
        if self.is_cuda():
            self._inputs.cuda()

        return self 
Example #12
Source File: trainer.py    From torecsys with MIT License 6 votes vote down vote up
def train(self) -> TypeVar("Trainer"):
        r"""Set trainer to train mode.
        
        Returns:
            torecsys.trainer.Trainer: self
        """
        self._current_mode = "train"
        self._sequential.train()

        if self.has_criterion and isinstance(self.criterion, nn.Module):
            self.criterion.train()
        
        # if self.has_metrics and isinstance(self.metric, nn.Module):
        #     self.metric.train()
        
        return self 
Example #13
Source File: trainer.py    From torecsys with MIT License 6 votes vote down vote up
def cpu(self) -> TypeVar("Trainer"):
        r"""Disable GPU computation of trainer.
        
        Returns:
            torecsys.trainer.Trainer: self
        """
        if self.has_inputs:
            self._inputs.cpu()

        if self.has_model:
            self._model.cpu()
        
        if self.has_criterion:
            self.criterion.cpu()
        
        self._use_cuda = False
        self._devices = None
        
        return self 
Example #14
Source File: trainer.py    From torecsys with MIT License 6 votes vote down vote up
def to(self, device: Union[str, torch.device]) -> TypeVar("Trainer"):
        r"""Set device of trainer.
        
        Args:
            device (str): device to be used for training.
        
        Raises:
            TypeError: when device is not allowed
        
        Returns:
            torecsys.trainer.Trainer: self
        """
        if device == "cuda":
            return self.cuda()
        elif device == "cpu":
            return self.cpu()
        elif isinstance(device, torch.device):
            self.to(device.type)
        else:
            raise TypeError(f"{type(device).__name__} not allowed.") 
Example #15
Source File: trainer.py    From torecsys with MIT License 6 votes vote down vote up
def set_max_num_iterations(self, max_num_iterations: int) -> TypeVar("Trainer"):
        r"""Set maximum number of training iterations to the trainer
        
        Args:
            max_num_iterations (int): maximum number of training iterations.
        
        Raises:
            TypeError: when type of max_num_iterations is not allowed.
        
        Returns:
            torecsys.trainer.Trainer: self
        """
        if not isinstance(max_num_iterations, int):
            raise TypeError("max_num_iterations must be int.")
        
        self._max_num_iterations = max_num_iterations

        return self 
Example #16
Source File: trainer.py    From torecsys with MIT License 6 votes vote down vote up
def set_negative_size(self, negative_size: int) -> TypeVar("Trainer"):
        r"""Set number of negative samples to be generated per batch to the trainer
        
        Args:
            negative_size (int): number of negative samples to be generated per batch.
        
        Raises:
            TypeError: when type of negative_size is not allowed.
        
        Returns:
            torecsys.trainer.Trainer: self
        """
        if not isinstance(negative_size, int):
            raise TypeError("negative_size must be int.")
        
        self._negative_size = negative_size

        return self 
Example #17
Source File: trainer.py    From torecsys with MIT License 6 votes vote down vote up
def set_dtype(self, dtype: str) -> TypeVar("Trainer"):
        r"""Set data type of trainer.
        
        Args:
            dtype (str): data type of trainer to be setted.
        
        Raises:
            AssertionError: when dtype is not allowed.
        
        Returns:
            torecsys.trainer.Trainer: self
        """
        # Check if dtype is allowed
        if dtype not in ["double", "float", "half"]:
            raise AssertionError(f"{dtype} not found.")
        
        # Bind dtype to _dtype
        self._dtype = dtype

        # Call methods of _inputs and _model to applied data type changes
        self._inputs = getattr(self._inputs, dtype)()
        self._model = getattr(self._model, dtype)()

        return self 
Example #18
Source File: test_retype.py    From retype with MIT License 5 votes vote down vote up
def test_alias_typevar(self) -> None:
        pyi_txt = """
        from typing import TypeVar

        _T = TypeVar('_T', bound=str)
        SOME_GLOBAL: int

        def fun(error: _T) -> _T: ...
        """
        src_txt = """
        "Docstring"

        from __future__ import print_function

        import sys

        SOME_GLOBAL: int = 0

        def fun(error):
            return error
        """
        expected_txt = """
        "Docstring"

        from __future__ import print_function

        import sys

        from typing import TypeVar
        SOME_GLOBAL: int = 0
        _T = TypeVar('_T', bound=str)

        def fun(error: _T) -> _T:
            return error
        """
        self.assertReapply(pyi_txt, src_txt, expected_txt)
        self.assertReapplyVisible(pyi_txt, src_txt, expected_txt) 
Example #19
Source File: overloading.py    From overloading.py with MIT License 5 votes vote down vote up
def normalize_type(type_, level=0):
    """
    Reduces an arbitrarily complex type declaration into something manageable.
    """
    if not typing or not isinstance(type_, typing.TypingMeta) or type_ is AnyType:
        return type_
    if isinstance(type_, typing.TypeVar):
        if type_.__constraints__ or type_.__bound__:
            return type_
        else:
            return AnyType
    if issubclass(type_, typing.Union):
        if not type_.__union_params__:
            raise OverloadingError("typing.Union must be parameterized")
        return typing.Union[tuple(normalize_type(t, level) for t in type_.__union_params__)]
    if issubclass(type_, typing.Tuple):
        params = type_.__tuple_params__
        if level > 0 or params is None:
            return typing.Tuple
        elif type_.__tuple_use_ellipsis__:
            return typing.Tuple[normalize_type(params[0], level + 1), ...]
        else:
            return typing.Tuple[tuple(normalize_type(t, level + 1) for t in params)]
    if issubclass(type_, typing.Callable):
        return typing.Callable
    if isinstance(type_, typing.GenericMeta):
        base = find_base_generic(type_)
        if base is typing.Generic:
            return type_
        else:
            return GenericWrapper(type_, base, level > 0)
    raise OverloadingError("%r not supported yet" % type_) 
Example #20
Source File: test_types.py    From dacite with MIT License 5 votes vote down vote up
def test_is_instance_with_not_supported_generic_types():
    T = TypeVar("T")

    class X(Generic[T]):
        pass

    assert not is_instance(X[str](), X[str]) 
Example #21
Source File: trainer.py    From torecsys with MIT License 5 votes vote down vote up
def add_graph(self, 
                  samples_inputs : Dict[str, torch.Tensor] = None,
                  verbose        : bool = False) -> TypeVar("Trainer"):
        r"""Add graph data to summary.
        
        Args:
            samples_inputs (Dict[str, T]): Sample inputs, which is a dictionary of tensors feed to inputs wrapper.
            verboses (bool, optional): Whether to print graph structure in console. Defaults to True.
        """
        # Get sample_inputs from _loaders with _current_mode
        if samples_inputs is None:
            samples_inputs = next(iter(self._loaders[self._current_mode]))
        
        # Add graph to tensorboard writer
        self.writer.add_graph(self.sequential, samples_inputs, verbose=verbose) 
Example #22
Source File: test_pydoc.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_typing_pydoc(self):
        def foo(data: typing.List[typing.Any],
                x: int) -> typing.Iterator[typing.Tuple[int, typing.Any]]:
            ...
        T = typing.TypeVar('T')
        class C(typing.Generic[T], typing.Mapping[int, str]): ...
        self.assertEqual(pydoc.render_doc(foo).splitlines()[-1],
                         'f\x08fo\x08oo\x08o(data:List[Any], x:int)'
                         ' -> Iterator[Tuple[int, Any]]')
        self.assertEqual(pydoc.render_doc(C).splitlines()[2],
                         'class C\x08C(typing.Mapping)') 
Example #23
Source File: trainer.py    From torecsys with MIT License 5 votes vote down vote up
def jit(self) -> TypeVar("Trainer"):
        r"""To enable jit in the trainer.
        
        Args:
            self (Trainer): self.
        """
        raise NotImplementedError(".jit() is not implemented.") 
Example #24
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_basic_plain(self):
        T = TypeVar('T')
        # T equals itself.
        self.assertEqual(T, T)
        # T is an instance of TypeVar
        self.assertIsInstance(T, TypeVar) 
Example #25
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_typevar_instance_type_error(self):
        T = TypeVar('T')
        with self.assertRaises(TypeError):
            isinstance(42, T) 
Example #26
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_typevar_subclass_type_error(self):
        T = TypeVar('T')
        with self.assertRaises(TypeError):
            issubclass(int, T)
        with self.assertRaises(TypeError):
            issubclass(T, int) 
Example #27
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_constrained_error(self):
        with self.assertRaises(TypeError):
            X = TypeVar('X', int)
            X 
Example #28
Source File: test_dataclasses.py    From dataclasses with Apache License 2.0 5 votes vote down vote up
def test_generic_dataclasses(self):
        T = TypeVar('T')

        @dataclass
        class LabeledBox(Generic[T]):
            content: T
            label: str = '<unknown>'

        box = LabeledBox(42)
        self.assertEqual(box.content, 42)
        self.assertEqual(box.label, '<unknown>')

        # Subscripting the resulting class should work, etc.
        Alias = List[LabeledBox[int]] 
Example #29
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_union_constrained(self):
        A = TypeVar('A', str, bytes)
        self.assertNotEqual(Union[A, str], Union[A]) 
Example #30
Source File: overloading.py    From overloading.py with MIT License 5 votes vote down vote up
def derive_configuration(cls):
        """
        Collect the nearest type variables and effective parameters from the type,
        its bases, and their origins as necessary.
        """
        base_params = cls.base.__parameters__
        if hasattr(cls.type, '__args__'):
            # typing as of commit abefbe4
            tvars = {p: p for p in base_params}
            types = {}
            for t in iter_generic_bases(cls.type):
                if t is cls.base:
                    type_vars = tuple(tvars[p] for p in base_params)
                    parameters = (types.get(tvar, tvar) for tvar in type_vars)
                    break
                if t.__args__:
                    for arg, tvar in zip(t.__args__, t.__origin__.__parameters__):
                        if isinstance(arg, typing.TypeVar):
                            tvars[tvar] = tvars.get(arg, arg)
                        else:
                            types[tvar] = arg
        else:
            # typing 3.5.0
            tvars = [None] * len(base_params)
            for t in iter_generic_bases(cls.type):
                for i, p in enumerate(t.__parameters__):
                    if tvars[i] is None and isinstance(p, typing.TypeVar):
                        tvars[i] = p
                if all(tvars):
                    type_vars = tvars
                    parameters = cls.type.__parameters__
                    break
        cls.type_vars = type_vars
        cls.parameters = tuple(normalize_type(p, 1) for p in parameters)