API reference

typeguard

check_type(value, expected_type, *, argname='value', memo=None)

Ensure that value matches expected_type.

The types from the typing module do not support isinstance() or issubclass() so a number of type specific checks are required. This function knows which checker to call for which type.

Parameters
  • value (Any) – value to be checked against expected_type

  • expected_type (Any) – a class or generic type instance

  • argname (str) – name of the argument to check; used for error messages

Raises

TypeCheckError – if there is a type mismatch

Return type

None

check_argument_types(memo=None)

Check that the argument values match the annotated types.

Unless both args and kwargs are provided, the information will be retrieved from the previous stack frame (ie. from the function that called this).

Return type

bool

Returns

True

Raises

TypeError – if there is an argument type mismatch

check_return_type(retval, memo=None)

Check that the return value is compatible with the return value annotation in the function.

Parameters

retval – the value about to be returned from the call

Return type

bool

Returns

True

Raises

TypeError – if there is a type mismatch

@typechecked(*, always: bool = False) Callable[[T_CallableOrType], T_CallableOrType]
@typechecked(func: T_CallableOrType, *, always: bool = False) T_CallableOrType

Perform runtime type checking on the arguments that are passed to the wrapped function.

The return value is also checked against the return annotation if any.

If the __debug__ global variable is set to False, no wrapping and therefore no type checking is done, unless always is True.

This can also be used as a class decorator. This will wrap all type annotated methods, including @classmethod, @staticmethod, and @property decorated methods, in the class with the @typechecked decorator.

Parameters
  • func – the function or class to enable type checking for

  • alwaysTrue to enable type checks even in optimized mode

@typeguard_ignore(f)

This decorator is a noop during static type-checking.

Return type

TypeVar(_F)

class TypeCheckConfiguration(autoload_plugins=None, plugins=None, forward_ref_policy=ForwardRefPolicy.WARN, checker_lookup_functions=<factory>, typecheck_fail_callback=None)
class ForwardRefPolicy(value)

Defines how unresolved forward references are handled.

ERROR = 1

propagate the NameError when the forward reference lookup fails

IGNORE = 3

silently skip checks for unresolveable forward references

WARN = 2

emit a TypeHintWarning if the forward reference lookup fails

warn_on_error(exc, memo)

Emit a warning on a type mismatch.

Return type

Any

exception TypeCheckError(message)

Raised by typeguard’s type checkers when a type mismatch is detected.

exception TypeCheckWarning(message)

Emitted by typeguard’s type checkers when a type mismatch is detected.

exception TypeHintWarning

A warning that is emitted when a type hint in string form could not be resolved to an actual type.

typeguard.importhook

class TypeguardFinder(packages, original_pathfinder)

Wraps another path finder and instruments the module with @typechecked if should_instrument() returns True.

Should not be used directly, but rather via install_import_hook().

New in version 2.6.

should_instrument(module_name)

Determine whether the module with the given name should be instrumented.

Parameters

module_name (str) – full name of the module that is about to be imported (e.g. xyz.abc)

Return type

bool

install_import_hook(packages, *, cls=<class 'typeguard.importhook.TypeguardFinder'>)

Install an import hook that decorates classes and functions with @typechecked.

This only affects modules loaded after this hook has been installed.

Return type

ImportHookManager

Returns

a context manager that uninstalls the hook on exit (or when you call .uninstall())

New in version 2.6.