Reference

Behold the non-API of beartype!

Beartype.

For PEP 8 compliance, this namespace exposes a subset of the metadata constants provided by the top-level meta submodule commonly inspected by external automation.

beartype.__version__ = '0.7.1'

Human-readable package version as a .-delimited string.

For PEP 8 compliance, this specifier has the canonical name __version__ rather than that of a typical global (e.g., VERSION_STR).

beartype.__version_info__ = (0, 7, 1)

Machine-readable package version as a tuple of integers.

For PEP 8 compliance, this specifier has the canonical name __version_info__ rather than that of a typical global (e.g., VERSION_PARTS).

beartype.beartype(func: T) → T[source]

Decorate the passed pure-Python callable (e.g., function or method declared in Python rather than C) to validate both all annotated parameters passed to this callable and the annotated value returned by this callable if any.

This decorator performs rudimentary type checking based on Python 3.x function annotations, as officially documented by PEP 484 (“Type Hints”). While PEP 484 supports arbitrarily complex type composition, this decorator requires all parameter and return value annotations to be either:

  • Classes (e.g., int, OrderedDict).

  • Tuples of classes (e.g., (int, OrderedDict)).

If optimizations are enabled by the active Python interpreter (e.g., due to option -O passed to this interpreter), this decorator reduces to a noop.

Parameters

func (Callable) – Non-class callable (i.e., callable object that is not a class) to be decorated by a dynamically generated new callable wrapping this original callable with pure-Python type-checking.

Returns

Dynamically generated new callable wrapping this original callable with pure-Python type-checking.

Return type

Callable

Raises
  • BeartypeDecorHintException – If any annotation on this callable is neither: * A PEP-compliant type (i.e., instance or class complying with a PEP supported by beartype), including: * `PEP 484`_ types (i.e., instance or class declared by the stdlib typing module). * A PEP-noncompliant type (i.e., instance or class complying with beartype-specific semantics rather than a PEP), including: * Fully-qualified forward references (i.e., strings specified as fully-qualified classnames). * Tuple unions (i.e., tuples containing one or more classes and/or forward references).

  • BeartypeDecorHintPep563Exception – If `PEP 563`_ is active for this callable and evaluating a postponed annotation (i.e., annotation whose value is a string) on this callable raises an exception (e.g., due to that annotation referring to local state no longer accessible from this deferred evaluation).

  • BeartypeDecorParamNameException – If the name of any parameter declared on this callable is prefixed by the reserved substring __beartype_.

  • BeartypeDecorWrappeeException – If this callable is either: * Uncallable. * A class, which beartype currently fails to support. * A C-based callable (e.g., builtin, third-party C extension).

  • BeartypeDecorWrapperException – If this decorator erroneously generates a syntactically invalid wrapper function. This should never happen, but here we are, so this probably happened. Please submit an upstream issue with our issue tracker if you ever see this. (Thanks and abstruse apologies!)

  • . _PEP 484:https://www.python.org/dev/peps/pep-0484

  • . _PEP 563:https://www.python.org/dev/peps/pep-0563