typeguard

Run-time type checker for Python

76 个版本 Python >=3.9
安装
pip install typeguard
poetry add typeguard
pipenv install typeguard
conda install typeguard
描述

.. image:: https://github.com/agronholm/typeguard/actions/workflows/test.yml/badge.svg :target: https://github.com/agronholm/typeguard/actions/workflows/test.yml :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 :alt: Documentation .. image:: https://tidelift.com/badges/package/pypi/typeguard :target: https://tidelift.com/subscription/pkg/pypi-typeguard :alt: Tidelift

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, and any arbitrary objects. It can be used together with static type checkers as an additional layer of type safety, to catch type violations that could only be detected at run time.

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

#. The check_type function:

  • like isinstance(), but supports arbitrary type annotations (within limits)

  • can be used as a cast() replacement, but with actual checking of the value #. 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) #. Code instrumentation:

  • entire modules, or individual functions (via @typechecked) are recompiled, with type checking code injected into them

  • automatically checks function arguments, return values and assignments to annotated local variables

  • for generator functions (regular and async), checks yield and send values

  • requires the original source code of the instrumented module(s) to be accessible

Two options are provided for code instrumentation:

#. the @typechecked function:

  • can be applied to functions individually #. the import hook (typeguard.install_import_hook()):

  • automatically instruments targeted modules on import

  • no manual code changes required in the target modules

  • requires the import hook to be installed before the targeted modules are imported

  • may clash with other import hooks

See the documentation_ for further information.

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

版本列表
4.5.2 2026-05-14
4.5.1 2026-02-19
4.5.0 2026-02-15
4.4.4 2025-06-18
4.4.3 2025-06-04
4.4.2 2025-02-16
4.4.1 2024-11-03
4.4.0 2024-10-27
4.3.0 2024-05-27
4.2.1 2024-03-24
4.2.0 2024-03-23
4.1.5 2023-09-11
4.1.4 2023-09-10
4.1.3 2023-08-26
4.1.2 2023-08-17
4.1.1 2023-08-15
4.1.0 2023-07-29
4.0.1 2023-07-27
4.0.0 2023-05-12
4.0.0rc6 2023-05-07
4.0.0rc5 2023-05-01
4.0.0rc4 2023-04-15
4.0.0rc3 2023-04-10
4.0.0rc2 2023-04-08
4.0.0rc1 2023-04-01
3.0.2 2023-03-22
3.0.1 2023-03-15
3.0.0 2023-03-14
3.0.0rc2 2023-03-01
3.0.0rc1 2023-02-26
3.0.0b2 2023-01-11
3.0.0b1 2023-01-09
2.13.3 2021-12-10
2.13.2 2021-11-22
2.13.1 2021-11-22
2.13.0 2021-10-10
2.12.1 2021-06-04
2.12.0 2021-04-01
2.11.1 2021-02-16
2.11.0 2021-02-16
2.10.0 2020-10-19
2.9.1 2020-06-07
2.9.0 2020-06-06
2.8.0 2020-06-02
2.7.1 2019-12-27
2.7.0 2019-12-10
2.6.1 2019-11-17
2.6.0 2019-11-06
2.5.1 2019-09-26
2.5.0 2019-08-26
2.4.1 2019-07-15
2.4.0 2019-07-14
2.3.1 2019-04-12
2.3.0 2019-03-27
2.2.2 2018-08-13
2.2.1 2018-08-12
2.2.0 2018-07-08
2.1.4 2018-01-07
2.1.3 2017-03-13
2.1.2 2017-03-12
2.1.1 2016-12-20
2.1.0 2016-12-17
2.0.2 2016-12-17
2.0.1 2016-12-10
2.0.0 2016-12-10
1.2.3 2016-09-14
1.2.2 2016-08-23
1.2.1 2016-06-28
1.2.0 2016-05-21
1.1.3 2016-05-09
1.1.2 2016-05-08
1.1.1 2016-01-03
1.1.0 2016-01-02
1.0.2 2016-01-01
1.0.1 2016-01-01
1.0.0 2015-12-29