pytest-beartype: Pytest plugin type-checking tests, fixtures, and/or your codebase with beartype: the unbearably fast near-real-time pure-Python runtime-static type-checker. · Pythie
Pytest plugin type-checking tests, fixtures, and/or your codebase with beartype: the unbearably fast near-real-time pure-Python runtime-static type-checker.
Maintained.Last release 2 months ago, commits in 7 of the last 26 weeks. Weaker on release cadence.
pytest-beartype supports your favourite Python package managers! It better.
Via uv, the newest upstart to emerge victorious in the battle of wills:
uv add pytest-beartype # <-- by the power of ultraviolet radiation
Via pip, the once-great venerable master now fallen on hard times:
pip install pytest-beartype # <-- sometimes the old ways are still okay
pytest-beartype: QA without doing anything.
Usage
pytest-beartype does nothing by default. This plugin only type-checks your
external packages, your pytest tests, and your pytest fixtures when you
tell it to – for your safety and the safety of the code you test:
Would you like to know more? Continue reading for details that may bore you.
Features
pytest-beartype configurably type-checks any combination of pytest tests,
pytest fixtures, and one or more arbitrary Python packages through
plugin-specific options either temporarily passed to the pytest
commandor permanently set from within project-specific
pyproject.toml and pytest.ini configuration
files:
Would you like to know more? No? Oh. Okay... wait. What? Really? You really
would like to know more? You should be bored out of your mind already! How can
you still be reading this? How can I even still be typing this!? 😮
pytest-beartype: Let's get this QA party started.
Type-check Tests
By default, pytest-beartype type-checks no tests inside your test suite.
Configure pytest-beartype to type-check all your tests (including all
parameters and fixtures passed to tests) inside your test suite:
By passing the --beartype-tests option to the pytest
command:
By setting the beartype_tests = true option in your
pytest.ini file:
beartype_tests = true
pytest-beartype: QA is made of this.
Type-check Fixtures
By default, pytest-beartype type-checks no fixtures inside your test suite.
Configure pytest-beartype to type-check all your fixtures (including all
parameters passed to and values returned from those fixtures as well as other
fixtures required by those fixtures) inside your test suite:
By passing the --beartype-test-fixtures option to the pytest
command:
pytest --beartype-test-fixtures
By setting the beartype_test_fixtures = true option in your
pyproject.toml file:
By setting the beartype_test_fixtures = true option in your
pytest.ini file:
beartype_test_fixtures = true
Note: Enabling this option does not configure pytest-beartype to
type-check fixtures defined outside your test suite (e.g., either standard
fixtures defined by pytest itself or non-standard fixtures defined by
third-party pytest plugins). The only fixtures that are type-checked are
those defined inside your test suite. Why? Safety. Namely, yours. External
fixtures are unlikely to expect or tolerate runtime type-checking. Attempting
to do so would likely fail on popular third-party plugins.
pytest-beartype: Who is pytest to disagree?
Type-check Packages
By default, pytest-beartype type-checks no packages outside your test suite.
If your packages are already internally type-checked by @beartype, no
problem. Your packages are internally type-checked by @beartype, aren't
they!? Uh oh. 😫
Maybe... not. Maybe you have justifiable usability or efficiency concerns.
Maybe you prefer to only conditionally type-check your packages by @beartype
while running tests and only while running tests. Maybe this is you. Fear not,
fellow QA person! pytest-beartype is here to type-check your dreams.
Configure pytest-beartype to either...
Type-check a Single Package
Type-check a single package (e.g., yours) while running tests:
By passing the --beartype-packages={package_name} option to the pytest
command:
pytest --beartype-packages=muh_package_name
By setting the beartype_packages = ["{package_name}"] option in your
pyproject.toml file:
pytest-beartype: Because code's already too complicated, too.
Type-check All Packages
Type-check all packages transitively imported anywhere while running tests.
Type-check your entire app stack at test time! Only the brave, the foolhardy,
and the desperate need apply:
By passing the --beartype-packages="*" option to the pytest
command.
pytest --beartype-packages='*'
CAUTION: The "*" character should typically be single- or
double-quoted to prevent your shell from erroneously expanding that as a
pathname glob.
By setting the beartype_packages = ["*"] option in your pyproject.toml
file:
By setting the beartype_packages = * option in your pytest.ini
file:
beartype_packages = *
CAUTION: You may need to omit problematic packages by also passing the
--beartype-skip-packages option to the pytest command
command or setting the beartype_skip_packages option in your
pyproject.toml or pytest.ini files. See
below for further commentary that will bore you into oblivion.
Avoid Type-checking Some Packages
By default, pytest-beartype type-checks all subpackages and submodules of
packages explicitly listed by passing the --beartype-packages option to the
pytest command command or setting the beartype_packages
option in your pyproject.toml or pytest.ini
files.
Ideally, that's fine. Ideally, your packages are all type-checkable in entirety.
But what if that's not fine? This is the real world over here. What if you're
type-checking all packages in your app stack with --beartype-packages="*",
for example, and one or more of those packages (or their subpackages or
submodules) fail type-checking? What then, huh?
Configure pytest-beartype to not type-check one or more of these
(sub)packages or (sub)modules while running tests:
By passing the
--beartype-skip-packages="{first_package_name},...,{last_package_name}"
option to the pytest command.