Unbearably fast near-real-time hybrid runtime-static type-checking in pure Python
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Automatic decoration of packages and modules using PEP 302/451-compliant import hooks. The beartype.claw module provides powerful mechanisms to automatically apply beartype decoration to entire packages or the complete Python ecosystem without manually decorating individual functions.
Apply beartype to all importable packages in the Python ecosystem.
def beartype_all(conf=None):
"""
Apply beartype to all importable packages.
Automatically decorates all annotated functions in all packages
imported after this call.
Parameters:
- conf: BeartypeConf - Optional configuration (defaults to BeartypeConf())
Returns:
None
"""Usage examples:
from beartype.claw import beartype_all
from beartype import BeartypeConf, BeartypeStrategy
# Apply to all packages with default configuration
beartype_all()
# Apply with custom configuration
beartype_all(conf=BeartypeConf(
strategy=BeartypeStrategy.On,
is_debug=True
))
# Now all annotated functions in any imported package are type-checked
import some_package # All functions automatically decoratedApply beartype to a specific named package.
def beartype_package(package_name, conf=None):
"""
Apply beartype to a specific package by name.
Parameters:
- package_name: str - Name of package to decorate
- conf: BeartypeConf - Optional configuration
Returns:
None
"""Usage examples:
from beartype.claw import beartype_package
# Decorate specific package
beartype_package("requests")
beartype_package("numpy")
# With configuration
beartype_package("pandas", conf=BeartypeConf(strategy=BeartypeStrategy.O1))
# Import happens after decoration setup
import requests # All annotated functions in requests are type-checkedApply beartype to multiple specific packages at once.
def beartype_packages(*package_names, conf=None):
"""
Apply beartype to multiple packages by name.
Parameters:
- *package_names: str - Names of packages to decorate
- conf: BeartypeConf - Optional configuration
Returns:
None
"""Usage examples:
from beartype.claw import beartype_packages
# Decorate multiple packages
beartype_packages("requests", "urllib3", "httpx")
# With configuration
beartype_packages(
"numpy", "pandas", "scipy",
conf=BeartypeConf(strategy=BeartypeStrategy.Ologn)
)Apply beartype to the calling package automatically.
def beartype_this_package(conf=None):
"""
Apply beartype to the package from which this function is called.
Automatically decorates all annotated functions in the calling package.
Typically called from a package's __init__.py file.
Parameters:
- conf: BeartypeConf - Optional configuration
Returns:
None
"""Usage examples:
# In your package's __init__.py file:
from beartype.claw import beartype_this_package
# Apply beartype to all modules in current package
beartype_this_package()
# With custom configuration
beartype_this_package(conf=BeartypeConf(
strategy=BeartypeStrategy.On,
is_debug=True
))
# All annotated functions in this package are now type-checked
def my_function(x: int) -> str:
return str(x) # Automatically type-checkedTemporarily apply beartype within a specific scope using a context manager.
class beartyping:
"""
Context manager for temporary beartype application.
Applies beartype configuration within the context and restores
previous state upon exit.
"""
def __init__(self, conf=None):
"""
Initialize context manager.
Parameters:
- conf: BeartypeConf - Optional configuration
"""
def __enter__(self):
"""Enter context with beartype active."""
def __exit__(self, exc_type, exc_val, exc_tb):
"""Exit context and restore previous state."""Usage examples:
from beartype.claw import beartyping
from beartype import BeartypeConf, BeartypeStrategy
# Temporary beartype application
with beartyping():
import some_module # Type-checked within this scope
# Type checking no longer applied to new imports
# With custom configuration
with beartyping(conf=BeartypeConf(strategy=BeartypeStrategy.On)):
import another_module # Thoroughly type-checked
def temp_function(x: int) -> int:
return x * 2 # Type-checked within contextCombining different import hook approaches for fine-grained control:
from beartype.claw import beartype_all, beartype_package
from beartype import BeartypeConf, BeartypeStrategy
# Different strategies for different packages
beartype_package("critical_package", conf=BeartypeConf(
strategy=BeartypeStrategy.On, # Thorough checking
is_debug=True
))
beartype_package("performance_package", conf=BeartypeConf(
strategy=BeartypeStrategy.O1 # Fast checking
))
# Catch-all for remaining packages
beartype_all(conf=BeartypeConf(
strategy=BeartypeStrategy.Ologn, # Balanced approach
violation_type=UserWarning # Warnings instead of errors
))All import hook functions accept BeartypeConf objects for customization:
from beartype.claw import beartype_this_package
from beartype import BeartypeConf, BeartypeStrategy
# Performance-optimized configuration
fast_conf = BeartypeConf(
strategy=BeartypeStrategy.O1,
is_debug=False
)
# Development configuration with thorough checking
dev_conf = BeartypeConf(
strategy=BeartypeStrategy.On,
is_debug=True,
is_color=True
)
# Production configuration with warning-based violations
prod_conf = BeartypeConf(
strategy=BeartypeStrategy.Ologn,
violation_type=UserWarning
)
# Apply based on environment
import os
if os.getenv("ENV") == "development":
beartype_this_package(conf=dev_conf)
elif os.getenv("ENV") == "production":
beartype_this_package(conf=prod_conf)
else:
beartype_this_package(conf=fast_conf)Call Early: Import hook functions should be called as early as possible, preferably in your package's __init__.py
Before Imports: Set up import hooks before importing the packages you want to decorate
Configuration Consistency: Use consistent BeartypeConf objects across related packages
Environment Awareness: Consider different configurations for development vs. production environments
Performance Monitoring: Use appropriate strategies based on performance requirements
Install with Tessl CLI
npx tessl i tessl/pypi-beartype