Package horizons :: Package ext :: Module typing :: Class TypeVar
[hide private]
[frames] | no frames]

Type TypeVar

object --+        
      type --+    
    TypingMeta --+

Type variable.


  T = TypeVar('T')  # Can be anything
  A = TypeVar('A', str, bytes)  # Must be str or bytes

Type variables exist primarily for the benefit of static type
checkers.  They serve as the parameters for generic types as well
as for generic function definitions.  See class Generic for more
information on generic types.  Generic functions work as follows:

  def repeat(x: T, n: int) -> Sequence[T]:
      '''Return a list containing n references to x.'''
      return [x]*n

  def longest(x: A, y: A) -> A:
      '''Return the longest of two strings.'''
      return x if len(x) >= len(y) else y

The latter example's signature is essentially the overloading
of (str, str) -> str and (bytes, bytes) -> bytes.  Also note
that if the arguments are instances of some subclass of str,
the return type is still plain str.

At runtime, isinstance(x, T) will raise TypeError.  However,
issubclass(C, T) is true for any class C, and issubclass(str, A)
and issubclass(bytes, A) are true, and issubclass(int, A) is
false.  (TODO: Why is this needed?  This may change.  See #136.)

Type variables may be marked covariant or contravariant by passing
covariant=True or contravariant=True.  See PEP 484 for more
details.  By default type variables are invariant.

Type variables can be introspected. e.g.:

  T.__name__ == 'T'
  T.__constraints__ == ()
  T.__covariant__ == False
  T.__contravariant__ = False
  A.__constraints__ == (str, bytes)

Nested Classes [hide private]
Instance Methods [hide private]
__instancecheck__(self, instance)
check if an object is an instance
__subclasscheck__(self, cls)
check if a class is a subclass
_get_type_vars(self, tvars)
    Inherited from TypingMeta
the object's type
__init__(self, *args, **kwds)
x.__init__(...) initializes x; see help(type(x)) for signature
_eval_type(self, globalns, localns)
Override this in subclasses to interpret forward references.
    Inherited from type
__call__(x, ...)
x.__delattr__('name') <==> del
__eq__(x, y)
__ge__(x, y)
x.__getattribute__('name') <==>
__gt__(x, y)
__le__(x, y)
__lt__(x, y)
__ne__(x, y)
x.__setattr__('name', value) <==> = value
list of immediate subclasses
return a type's method resolution order
    Inherited from object
default object formatter
helper for pickle
helper for pickle
size of object in memory, in bytes
Abstract classes can override this to customize issubclass().
Class Methods [hide private]
    Inherited from TypingMeta
assert_no_subclassing(cls, bases)
Static Methods [hide private]
a new object with type S, a subtype of T
__new__(cls, name, *constraints, **kwargs)
Class Variables [hide private]
    Inherited from TypingMeta
  _is_protocol = False
Properties [hide private]
    Inherited from type
    Inherited from object
the object's class
Method Details [hide private]

__instancecheck__(self, instance)


check if an object is an instance

Returns: bool
Overrides: type.__instancecheck__
(inherited documentation)

__new__(cls, name, *constraints, **kwargs)
Static Method

Returns: a new object with type S, a subtype of T
Overrides: object.__new__
(inherited documentation)

(Representation operator)



Overrides: object.__repr__
(inherited documentation)

__subclasscheck__(self, cls)


check if a class is a subclass

Returns: bool
Overrides: type.__subclasscheck__
(inherited documentation)

_get_type_vars(self, tvars)

Overrides: TypingMeta._get_type_vars