CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-beartype

Unbearably fast near-real-time hybrid runtime-static type-checking in pure Python

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

exceptions.mddocs/

Exception Handling

Comprehensive exception and warning hierarchy for different types of type-checking failures and configuration errors. The beartype.roar module provides detailed exception classes for precise error handling and debugging of type violations.

Capabilities

Base Exception Classes

Root exception classes that all beartype errors inherit from.

class BeartypeException(Exception):
    """
    Base exception for all beartype-related errors.
    
    Root exception class from which all other beartype exceptions inherit.
    Provides common functionality for beartype error handling.
    """

class BeartypeHintViolation(BeartypeException):
    """
    Base class for type hint violations.
    
    Root class for all exceptions raised during runtime type checking
    when objects fail to satisfy their type hints.
    """

Runtime Call Violations

Exceptions raised during function or method calls when type checking fails.

class BeartypeCallHintViolation(BeartypeHintViolation):
    """
    Type hint violation during callable invocation.
    
    Raised when parameters or return values violate their type hints
    during function or method calls.
    """

class BeartypeCallHintParamViolation(BeartypeCallHintViolation):
    """
    Parameter type hint violation.
    
    Raised when a parameter value doesn't match its declared type hint
    during function invocation.
    """

class BeartypeCallHintReturnViolation(BeartypeCallHintViolation):
    """
    Return value type hint violation.
    
    Raised when a function's return value doesn't match its declared
    return type hint.
    """

Usage examples:

from beartype import beartype
from beartype.roar import (
    BeartypeCallHintParamViolation,
    BeartypeCallHintReturnViolation
)

@beartype
def divide(x: int, y: int) -> float:
    return x / y

try:
    divide("10", 2)  # String instead of int
except BeartypeCallHintParamViolation as e:
    print(f"Parameter error: {e}")

@beartype  
def get_name() -> str:
    return 42  # Should return string

try:
    get_name()
except BeartypeCallHintReturnViolation as e:
    print(f"Return type error: {e}")

Decorator and Configuration Exceptions

Exceptions related to decorator usage and configuration.

class BeartypeDecorException(BeartypeException):
    """
    Decorator-related exception.
    
    Base class for errors that occur during decorator application
    or wrapper function generation.
    """

class BeartypeDecorHintException(BeartypeDecorException):
    """
    Type hint decoration exception.
    
    Raised when there are issues with type hints during decorator
    application, such as invalid or unsupported type hints.
    """

class BeartypeConfException(BeartypeException):
    """
    Configuration-related exception.
    
    Raised when BeartypeConf objects are created with invalid
    parameters or used incorrectly.
    """

class BeartypeConfParamException(BeartypeConfException):
    """
    Configuration parameter exception.
    
    Raised when invalid parameters are passed to BeartypeConf
    constructor or configuration methods.
    """

Usage examples:

from beartype import beartype, BeartypeConf
from beartype.roar import BeartypeConfParamException

try:
    # Invalid configuration parameter
    conf = BeartypeConf(strategy="invalid_strategy")
except BeartypeConfParamException as e:
    print(f"Configuration error: {e}")

# Invalid type hint usage
try:
    @beartype
    def bad_function(x: "invalid_forward_ref") -> int:
        return x
except BeartypeDecorHintException as e:
    print(f"Type hint error: {e}")

Import Hook Exceptions

Exceptions related to the import hook system in beartype.claw.

class BeartypeClawException(BeartypeException):
    """
    Import hook exception.
    
    Base class for errors in the beartype.claw import hook system.
    """

class BeartypeClawHookException(BeartypeClawException):
    """
    Hook installation exception.
    
    Raised when there are errors installing or managing import hooks.
    """

class BeartypeClawImportException(BeartypeClawException):
    """
    Import processing exception.
    
    Raised when errors occur while processing imports through
    beartype import hooks.
    """

DOOR API Exceptions

Exceptions related to the object-oriented type introspection API.

class BeartypeDoorException(BeartypeException):
    """
    DOOR API exception.
    
    Base class for errors in the beartype.door object-oriented API.
    """

class BeartypeDoorHintViolation(BeartypeHintViolation):
    """
    DOOR type hint violation.
    
    Raised by DOOR API functions when objects fail type validation.
    """

class BeartypeDoorPepException(BeartypeDoorException):
    """
    PEP compliance exception in DOOR API.
    
    Raised when type hints don't conform to expected PEP standards
    in DOOR API operations.
    """

Usage examples:

from beartype.door import die_if_unbearable, is_bearable
from beartype.roar import BeartypeDoorHintViolation
from typing import List

try:
    die_if_unbearable("not a list", List[int])
except BeartypeDoorHintViolation as e:
    print(f"DOOR validation error: {e}")

# Check type compatibility safely
if not is_bearable(42, str):
    print("Type mismatch detected")

Validator Exceptions

Exceptions related to custom validators in beartype.vale.

class BeartypeValeException(BeartypeException):
    """
    Validator exception.
    
    Base class for errors in the beartype.vale validator system.
    """

class BeartypeValeValidationException(BeartypeValeException):
    """
    Validation failure exception.
    
    Raised when custom validators fail to validate objects.
    """

class BeartypeValeSubscriptionException(BeartypeValeException):
    """
    Validator subscription exception.
    
    Raised when validator factories are subscripted with invalid parameters.
    """

Usage examples:

from beartype import beartype
from beartype.vale import Is
from beartype.roar import BeartypeValeValidationException
from typing import Annotated

PositiveInt = Annotated[int, Is[lambda x: x > 0]]

@beartype
def process_positive(value: PositiveInt) -> int:
    return value * 2

try:
    process_positive(-5)
except BeartypeValeValidationException as e:
    print(f"Validator error: {e}")

Warning Hierarchy

Non-fatal warnings for various beartype operations.

class BeartypeWarning(UserWarning):
    """
    Base warning class for beartype.
    
    Root warning class for all beartype-related warnings.
    """

class BeartypeDecorHintWarning(BeartypeWarning):
    """
    Type hint decoration warning.
    
    Emitted for non-fatal issues with type hints during decoration.
    """

class BeartypeClawWarning(BeartypeWarning):
    """
    Import hook warning.
    
    Emitted for non-fatal issues in the import hook system.
    """

class BeartypeConfWarning(BeartypeWarning):
    """
    Configuration warning.
    
    Emitted for non-fatal configuration issues.
    """

PEP-Specific Exceptions

Exceptions for specific PEP standard violations.

class BeartypePepException(BeartypeException):
    """Base class for PEP standard violations."""

class BeartypeDecorHintPep484Exception(BeartypeDecorHintException):
    """PEP 484 (typing) standard violation."""

class BeartypeDecorHintPep585Exception(BeartypeDecorHintException):
    """PEP 585 (generic aliases) standard violation."""

class BeartypeDecorHintPep593Exception(BeartypeDecorHintException):
    """PEP 593 (Annotated) standard violation."""

class BeartypeDecorHintPep604Exception(BeartypeDecorHintException):
    """PEP 604 (union operator |) standard violation."""

Exception Handling Patterns

Specific Error Handling

from beartype import beartype
from beartype.roar import (
    BeartypeCallHintParamViolation,
    BeartypeCallHintReturnViolation,
    BeartypeDecorHintException
)

@beartype
def robust_function(x: int, y: str) -> float:
    return float(len(y) + x)

def safe_call():
    try:
        return robust_function("not int", "valid string")
    except BeartypeCallHintParamViolation as e:
        print(f"Invalid parameter: {e}")
        return None
    except BeartypeCallHintReturnViolation as e:
        print(f"Invalid return value: {e}")
        return None

Generic Error Handling

from beartype import beartype
from beartype.roar import BeartypeException, BeartypeHintViolation

@beartype
def risky_operation(data: list[int]) -> int:
    return sum(data)

def safe_operation(data):
    try:
        return risky_operation(data)
    except BeartypeHintViolation as e:
        # Handle type hint violations specifically
        print(f"Type violation: {e}")
        return 0
    except BeartypeException as e:
        # Handle other beartype errors
        print(f"Beartype error: {e}")
        return 0

Warning Handling

import warnings
from beartype.roar import BeartypeWarning

# Filter beartype warnings
warnings.filterwarnings("ignore", category=BeartypeWarning)

# Or handle them specifically
def warning_handler(message, category, filename, lineno, file=None, line=None):
    if issubclass(category, BeartypeWarning):
        print(f"Beartype warning: {message}")

warnings.showwarning = warning_handler

Install with Tessl CLI

npx tessl i tessl/pypi-beartype

docs

core-type-checking.md

door-api.md

exceptions.md

import-hooks.md

index.md

typing-compatibility.md

validators.md

tile.json