aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/typeguard/README.rst
blob: 6d2ed01bac48c39ec040fd323ae52d442605fab1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
.. image:: https://travis-ci.com/agronholm/typeguard.svg?branch=master
  :target: https://travis-ci.com/agronholm/typeguard
  :alt: Build Status
.. image:: https://coveralls.io/repos/agronholm/typeguard/badge.svg?branch=master&service=github
  :target: https://coveralls.io/github/agronholm/typeguard?branch=master
  :alt: Code Coverage
.. image:: https://readthedocs.org/projects/typeguard/badge/?version=latest
  :target: https://typeguard.readthedocs.io/en/latest/?badge=latest

This library provides run-time type checking for functions defined with
`PEP 484 <https://www.python.org/dev/peps/pep-0484/>`_ argument (and return) type annotations.

Four principal ways to do type checking are provided, each with its pros and cons:

#. the ``check_argument_types()`` and ``check_return_type()`` functions:

   * debugger friendly (except when running with the pydev debugger with the C extension installed)
   * does not work reliably with dynamically defined type hints (e.g. in nested functions)
#. the ``@typechecked`` decorator:

   * automatically type checks yields and sends of returned generators (regular and async)
   * adds an extra frame to the call stack for every call to a decorated function
#. the stack profiler hook (``with TypeChecker('packagename'):``) (deprecated):

   * emits warnings instead of raising ``TypeError``
   * requires very few modifications to the code
   * multiple TypeCheckers can be stacked/nested
   * does not work reliably with dynamically defined type hints (e.g. in nested functions)
   * may cause problems with badly behaving debuggers or profilers
   * cannot distinguish between an exception being raised and a ``None`` being returned
#. the import hook (``typeguard.importhook.install_import_hook()``):

   * automatically annotates classes and functions with ``@typechecked`` on import
   * no code changes required in target modules
   * requires imports of modules you need to check to be deferred until after the import hook has
     been installed
   * may clash with other import hooks

See the documentation_ for further instructions.

.. _documentation: https://typeguard.readthedocs.io/en/latest/