---
title: "beartype | pypx"
description: "Unbearably fast near-real-time pure-Python runtime-static type-checker."
canonical_url: "https://pypx.app/packages/beartype"
meta:
  "og:description": "Unbearably fast near-real-time pure-Python runtime-static type-checker."
  "og:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_beartype%2Cversion_0.23.1%2Csummary_Unbearably%2Bfast%2Bnear-real-time%2Bpure-Python%2Bruntime-static%2Btype-checker.%2Clicense_MIT%2Cp_Ii9wYWNrYWdlcy9iZWFydHlwZSI%2Cs_S4ZKWTy12SBU1igg.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "beartype 0.23.1"
  "twitter:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_beartype%2Cversion_0.23.1%2Csummary_Unbearably%2Bfast%2Bnear-real-time%2Bpure-Python%2Bruntime-static%2Btype-checker.%2Clicense_MIT%2Cp_Ii9wYWNrYWdlcy9iZWFydHlwZSI%2Cs_S4ZKWTy12SBU1igg.png"
---

> Index of every page on this site: [llms.txt](https://pypx.app/llms.txt)

# **beartype**

v0.23.1

Unbearably fast near-real-time pure-Python runtime-static type-checker.

1.5 MB Python 3.10+ py3-none-anyMIT

[** Diff **](https://pypx.app/packages/beartype/diff)

`$ uv add beartype`

:description lang=en: Beartype is an open-source pure-Python PEP-compliant constant-time runtime type checker emphasizing efficiency and portability.

=================

# [![beartype —\[ the bare-metal type checker \]—](https://raw.githubusercontent.com/beartype/beartype-assets/main/banner/logo.svg)](https://beartype.readthedocs.io)

[![beartype Read The Docs (RTD) status](https://readthedocs.org/projects/beartype/badge/?version=latest)](https://beartype.readthedocs.io/en/latest/?badge=latest) [![beartype continuous integration (CI) status](https://github.com/beartype/beartype/actions/workflows/python_test.yml/badge.svg)](https://github.com/beartype/beartype/actions?workflow=tests) [![beartype test coverage status](https://codecov.io/gh/beartype/beartype/branch/main/graph/badge.svg?token=E6F4YSY9ZQ)](https://codecov.io/gh/beartype/beartype)

⚠

[Beartype documentation lives at ReadTheDocs (RTD)](<https://pypx.app/beartype RTD_>)\_. It's readable, structured, and soothing to the deep folds of your big galactic brain. Open your mind to an ocean of mundane knowledge that will exhaust you at work. Enter... **the Bearpedia:**

https://beartype.readthedocs.io

The document you are now reading was once a monolithic \~316Kb file known to induce migraines in 22% of the whole devops population. For your safety, that document no longer exists. This is how much beartype cares.

**Beartype** is an [open-source](<https://pypx.app/beartype license_>)\_ [pure-Python](<https://pypx.app/beartype pure_>)\_ [PEP-compliant](<https://pypx.app/beartype PEPs_>)\_ [near-real-time](<https://pypx.app/beartype FAQ_>)\_ [hybrid runtime-static](<https://pypx.app/beartype hybrid_>)\_ [third-generation](<https://pypx.app/beartype third_>)\_ [type checker](<https://pypx.app/beartype ELI5_>)\_ emphasizing efficiency, usability, unsubstantiated jargon we just made up, and thrilling puns.

```bash
# Install beartype.
$ pip install beartype

# Edit the "{your_package}.__init__" submodule with your favourite IDE.
$ vim {your_package}/__init__.py      # <-- so, i see that you too vim
```

```python
from beartype.claw import beartype_this_package       # <-- hype comes
beartype_this_package()                               # <-- hype goes
```

Beartype now implicitly type-checks *all* annotated classes, callables, and variable assignments across *all* submodules of your package. Congrats. This day all bugs die.

But why stop at the burning tires in only *your* code? Your app depends on a sprawling ghetto of other packages, modules, and services. How riddled with infectious diseases is *that* code? You're about to find out.

```python
# ....................{ BIG BEAR                        }....................
# Warn about type hint violations in *OTHER* packages outside your control;
# only raise exceptions from violations in your package under your control.
# Again, at the very top of your "{your_package}.__init__" submodule:
from beartype import BeartypeConf                              # <-- this isn't your fault
from beartype.claw import beartype_all, beartype_this_package  # <-- you didn't sign up for this
beartype_this_package()                                        # <-- raise exceptions in your code
beartype_all(conf=BeartypeConf(violation_type=UserWarning))     # <-- emit warnings from other code
```

Beartype now implicitly type-checks *all* annotated classes, callables, and variable assignments across *all* submodules of *all* packages. When **your** package violates type safety, beartype raises an exception. When any **other** package violates type safety, beartype just emits a warning. The triumphal fanfare you hear is probably your userbase cheering. This is how the QA was won.

Beartype also publishes a `plethora of APIs for fine-grained control over type-checking <beartype APIs>`. For those who are about to QA, beartype salutes you. Would you like to know more?

\# So let's do this. $ python3

```python
# ....................{ RAISE THE PAW                   }....................
# Manually enforce type hints across individual classes and callables.
# Do this only if you want a(nother) repetitive stress injury.

# Import the @beartype decorator.
>>> from beartype import beartype      # <-- eponymous import; it's eponymous

# Annotate @beartype-decorated classes and callables with type hints.
>>> @beartype                          # <-- you too will believe in magic
... def quote_wiggum(lines: list[str]) -> None:
...     print('“{}”\n\t— Police Chief Wiggum'.format("\n ".join(lines)))

# Call those callables with valid parameters.
>>> quote_wiggum(["Okay, folks. Show's over!", " Nothing to see here. Show's…",])
“Okay, folks. Show's over!
 Nothing to see here. Show's…”
   — Police Chief Wiggum

# Call those callables with invalid parameters.
>>> quote_wiggum([b"Oh, my God! A horrible plane crash!", b"Hey, everybody! Get a load of this flaming wreckage!",])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 30, in quote_wiggum
  File "/home/springfield/beartype/lib/python3.9/site-packages/beartype/_decor/_code/_pep/_error/errormain.py", line 220, in get_beartype_violation
    raise exception_cls(
beartype.roar.BeartypeCallHintParamViolation: @beartyped
quote_wiggum() parameter lines=[b'Oh, my God! A horrible plane
crash!', b'Hey, everybody! Get a load of thi...'] violates type hint
list[str], as list item 0 value b'Oh, my God! A horrible plane crash!'
not str.

# ....................{ MAKE IT SO                      }....................
# Squash bugs by refining type hints with @beartype validators.
>>> from beartype.vale import Is  # <---- validator factory
>>> from typing import Annotated  # <---------------- if Python ≥ 3.9.0
# >>> from typing_extensions import Annotated   # <-- if Python < 3.9.0

# Validators are type hints constrained by lambda functions.
>>> ListOfStrings = Annotated[  # <----- type hint matching non-empty list of strings
...     list[str],  # <----------------- type hint matching possibly empty list of strings
...     Is[lambda lst: bool(lst)]  # <-- lambda matching non-empty object
... ]

# Annotate @beartype-decorated callables with validators.
>>> @beartype
... def quote_wiggum_safer(lines: ListOfStrings) -> None:
...     print('“{}”\n\t— Police Chief Wiggum'.format("\n ".join(lines)))

# Call those callables with invalid parameters.
>>> quote_wiggum_safer([])
beartype.roar.BeartypeCallHintParamViolation: @beartyped
quote_wiggum_safer() parameter lines=[] violates type hint
typing.Annotated[list[str], Is[lambda lst: bool(lst)]], as value []
violates validator Is[lambda lst: bool(lst)].

# ....................{ AT ANY TIME                     }....................
# Type-check anything against any type hint – anywhere at anytime.
>>> from beartype.door import (
...     is_bearable,  # <-------- like "isinstance(...)"
...     die_if_unbearable,  # <-- like "assert isinstance(...)"
... )
>>> is_bearable(['The', 'goggles', 'do', 'nothing.'], list[str])
True
>>> die_if_unbearable([0xCAFEBEEF, 0x8BADF00D], ListOfStrings)
beartype.roar.BeartypeDoorHintViolation: Object [3405692655, 2343432205]
violates type hint typing.Annotated[list[str], Is[lambda lst: bool(lst)]],
as list index 0 item 3405692655 not instance of str.

# ....................{ GO TO PLAID                     }....................
# Type-check anything in around 1µs (one millionth of a second) – including
# this list of one million 2-tuples of NumPy arrays.
>>> from beartype.door import is_bearable
>>> from numpy import array, ndarray
>>> data = [(array(i), array(i)) for i in range(1000000)]
>>> %time is_bearable(data, list[tuple[ndarray, ndarray]])
    CPU times: user 31 µs, sys: 2 µs, total: 33 µs
    Wall time: 36.7 µs
True

# ....................{ MAKE US DO IT                   }....................
# Don't know type hints? Do but wish you didn't? What if somebody else could
# write your type hints for you? @beartype: it's somebody. Let BeartypeAI™
# write your type hints for you. When you no longer care, call BeartypeAI™.
>>> from beartype.bite import infer_hint  # <----- caring begins

# What type hint describes the root state of a Pygments lexer, BeartypeAI™?
>>> from pygments.lexers import PythonLexer
>>> infer_hint(PythonLexer().tokens["root"])
list[
    tuple[str | pygments.token._TokenType[str], ...] |
    tuple[str | collections.abc.Callable[
        typing.Concatenate[object, object, ...], object], ...] |
    typing.Annotated[
        collections.abc.Collection[str],
        beartype.vale.IsInstance[pygments.lexer.include]]
]  # <---- caring ends

# ...all righty then. Guess I'll just take your word for that, BeartypeAI™.
```

Beartype brings Rust\_- and `C++`\_-inspired [zero-cost abstractions](<https://pypx.app/zero-cost abstraction_>)\_ into the lawless world of `dynamically-typed`\_ Python by [enforcing type safety at the granular level of functions and methods](<https://pypx.app/beartype ELI5_>)\_ against [type hints standardized by the Python community](<https://pypx.app/beartype PEPs_>)\_ in [O(1) non-amortized worst-case time with negligible constant factors](<https://pypx.app/beartype math_>)\_. If the prior sentence was unreadable jargon, [see our friendly and approachable FAQ for a human-readable synopsis](<https://pypx.app/beartype FAQ_>)\_.

Beartype is [portably implemented](<https://pypx.app/beartype codebase_>)\_ in [Python 3](https://pypx.app/Python_)\_, [continuously stress-tested](<https://pypx.app/beartype tests_>)\_ via `GitHub Actions`\_ **×** tox\_ **×** pytest\_ **×** Codecov\_, and [permissively distributed](<https://pypx.app/beartype license_>)\_ under the `MIT license`\_. Beartype has *no* runtime dependencies, [only one test-time dependency](https://pypx.app/pytest_)\_, and [only one documentation-time dependency](https://pypx.app/Sphinx_)\_. Beartype supports [all actively developed Python versions](<https://pypx.app/Python status_>)\_, [all Python package managers](<https://pypx.app/beartype install_>)\_, and [multiple platform-specific package managers](<https://pypx.app/beartype install_>)\_.

## **Details **

<dl>

<dt>Version</dt>
<dd>0.23.1</dd>

<dt>License</dt>
<dd>MIT</dd>

<dt>Python</dt>
<dd>&gt;=3.10</dd>

<dt>Maintainer</dt>
<dd>Cecil Curry &lt;leycec@gmail.com&gt;</dd></dl>

## **Links**

- [Docs](https://beartype.readthedocs.io)
- [Forums](https://github.com/beartype/beartype/discussions)
- [GitHub](https://github.com/beartype/beartype)
- [Issues](https://github.com/beartype/beartype/issues)
- [Releases](https://github.com/beartype/beartype/releases)
- [View on PyPI](https://pypi.org/project/beartype/)

## **Release Cadence **

**8**

releases in the past year

avg 34 days between releases

## **Maintainers **

- Cecil Curry · [leycec@gmail.com](mailto:leycec@gmail.com)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://pypx.app/#website","@type":"WebSite","description":"The Python Package Index, reimagined. Fast search, dependency insights, download trends, and changelogs — all in one place.","name":"pypx","url":"https://pypx.app/"},{"@id":"https://pypx.app/packages/beartype#webpage","@type":"WebPage","description":"Unbearably fast near-real-time pure-Python runtime-static type-checker.","name":"beartype","url":"https://pypx.app/packages/beartype","isPartOf":{"@id":"https://pypx.app/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://pypx.app/packages/beartype"]}]},{"@id":"https://pypx.app/packages/beartype#/schema/software-application/1","@type":"SoftwareApplication","applicationCategory":"DeveloperApplication","description":"Unbearably fast near-real-time pure-Python runtime-static type-checker.","license":"MIT","name":"beartype","softwareVersion":"0.23.1"}]}
```

## Sitemap

See the full [sitemap](https://pypx.app/sitemap.md) for all pages.
