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

Module typing

Classes [hide private]
  AbstractSet
Abstract base class for generic types.
  Any
Special type indicating an unconstrained type.
  AnyMeta
Metaclass for Any.
  AnyStr
  BinaryIO
Typed version of the return of open() in binary mode.
  ByteString
  CT
  Callable
Callable type; Callable[[int], str] is a function of (int) -> str.
  CallableMeta
Metaclass for Callable.
  Container
Abstract base class for generic types.
  DefaultDict
  Dict
  Final
Mix-in class to prevent instantiation.
  FrozenSet
  Generator
  Generic
Abstract base class for generic types.
  GenericMeta
Metaclass for generic types.
  Hashable
  IO
Generic base class for TextIO and BinaryIO.
  ItemsView
  Iterable
Abstract base class for generic types.
  Iterator
Abstract base class for generic types.
  KT
  KeysView
  List
  Mapping
Abstract base class for generic types.
  MappingView
Abstract base class for generic types.
  MutableMapping
Abstract base class for generic types.
  MutableSequence
Abstract base class for generic types.
  MutableSet
Abstract base class for generic types.
  Optional
Optional type.
  OptionalMeta
Metaclass for Optional.
  Reversible
  Sequence
Abstract base class for generic types.
  Set
  Sized
  SupportsAbs
  SupportsComplex
  SupportsFloat
  SupportsInt
  T
  T_co
  T_contra
  Text
unicode(object='') -> unicode object unicode(string[, encoding[, errors]]) -> unicode object
  TextIO
Typed version of the return of open() in text mode.
  Tuple
Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
  TupleMeta
Metaclass for Tuple.
  Type
A special construct usable to annotate class objects.
  TypeVar
Type variable.
  TypeVarMeta
  TypingMeta
Metaclass for every type defined below.
  Union
Union type; Union[X, Y] means either X or Y.
  UnionMeta
Metaclass for Union.
  VT
  VT_co
  V_co
  ValuesView
  _ForwardRef
Wrapper to hold a forward reference.
  _FrozenSetMeta
This metaclass ensures set is not a subclass of FrozenSet.
  _Protocol
Internal base class for protocol classes.
  _ProtocolMeta
Internal metaclass for _Protocol.
  _TypeAlias
Internal helper class for defining generic variants of concrete types.
  io
Wrapper namespace for IO generic classes.
  re
Wrapper namespace for re type aliases.
Functions [hide private]
 
NamedTuple(typename, fields)
Typed version of namedtuple.
 
NewType(name, tp)
NewType creates simple unique types with almost zero runtime overhead.
 
_eval_type(t, globalns, localns)
 
_geqv(a, b)
Return whether two generic classes are equivalent.
 
_get_defaults(func)
Internal helper to extract the default arguments, by name.
 
_get_type_vars(types, tvars)
 
_gorg(a)
Return the farthest origin of a generic class.
 
_next_in_mro(cls)
Helper for Generic.__new__.
 
_overload_dummy(*args, **kwds)
Helper for @overload to raise when called.
 
_qualname(x)
 
_type_check(arg, msg)
Check that the argument is a type, and return it.
 
_type_repr(obj)
Return the repr() of an object, special-casing types.
 
_type_vars(types)
 
cast(typ, val)
Cast a value to a type.
 
get_type_hints(obj, globalns=None, localns=None)
In Python 2 this is not supported and always returns None.
 
no_type_check(arg)
Decorator to indicate that annotations are not type hints.
 
no_type_check_decorator(decorator)
Decorator to give another decorator the @no_type_check effect.
 
overload(func)
Decorator for overloaded functions/methods.
Variables [hide private]
  Match = Match[~AnyStr]
  Pattern = Pattern[~AnyStr]
  TYPE_CHECKING = False
  __package__ = None
hash(x)
Function Details [hide private]

NamedTuple(typename, fields)

 

Typed version of namedtuple.

Usage:

   Employee = typing.NamedTuple('Employee', [('name', str), 'id', int)])

This is equivalent to:

   Employee = collections.namedtuple('Employee', ['name', 'id'])

The resulting class has one extra attribute: _field_types, giving a dict mapping field names to types. (The field names are in the _fields attribute, which is part of the namedtuple API.)

NewType(name, tp)

 

NewType creates simple unique types with almost zero runtime overhead. NewType(name, tp) is considered a subtype of tp by static type checkers. At runtime, NewType(name, tp) returns a dummy function that simply returns its argument. Usage:

   UserId = NewType('UserId', int)

   def name_by_id(user_id):
       # type: (UserId) -> str
       ...

   UserId('user')          # Fails type check

   name_by_id(42)          # Fails type check
   name_by_id(UserId(42))  # OK

   num = UserId(5) + 1     # type: int

_geqv(a, b)

 

Return whether two generic classes are equivalent.

The intention is to consider generic class X and any of its parameterized forms (X[T], X[int], etc.) as equivalent.

However, X is not equivalent to a subclass of X.

The relation is reflexive, symmetric and transitive.

_next_in_mro(cls)

 

Helper for Generic.__new__.

Returns the class after the last occurrence of Generic or Generic[...] in cls.__mro__.

_type_check(arg, msg)

 
Check that the argument is a type, and return it.

As a special case, accept None and return type(None) instead.
Also, _TypeAlias instances (e.g. Match, Pattern) are acceptable.

The msg argument is a human-readable error message, e.g.

    "Union[arg, ...]: arg should be a type."

We append the repr() of the actual value (truncated to 100 chars).

_type_repr(obj)

 

Return the repr() of an object, special-casing types.

If obj is a type, we return a shorter version than the default type.__repr__, based on the module and qualified name, which is typically enough to uniquely identify a type. For everything else, we fall back on repr(obj).

cast(typ, val)

 

Cast a value to a type.

This returns the value unchanged. To the type checker this signals that the return value has the designated type, but at runtime we intentionally don't check anything (we want this to be as fast as possible).

no_type_check(arg)

 

Decorator to indicate that annotations are not type hints.

The argument must be a class or function; if it is a class, it applies recursively to all methods defined in that class (but not to methods defined in its superclasses or subclasses).

This mutates the function(s) in place.

no_type_check_decorator(decorator)

 

Decorator to give another decorator the @no_type_check effect.

This wraps the decorator with something that wraps the decorated function in @no_type_check.

overload(func)

 
Decorator for overloaded functions/methods.

In a stub file, place two or more stub definitions for the same
function in a row, each decorated with @overload.  For example:

  @overload
  def utf8(value: None) -> None: ...
  @overload
  def utf8(value: bytes) -> bytes: ...
  @overload
  def utf8(value: str) -> bytes: ...

In a non-stub file (i.e. a regular .py file), do the same but
follow it with an implementation.  The implementation should *not*
be decorated with @overload.  For example:

  @overload
  def utf8(value: None) -> None: ...
  @overload
  def utf8(value: bytes) -> bytes: ...
  @overload
  def utf8(value: str) -> bytes: ...
  def utf8(value):
      # implementation goes here