{"id":4821,"library":"type-enforced","title":"Type Enforced","description":"Type-enforced is a pure Python library that provides runtime type enforcement for Python type annotations. It uses a decorator-based approach to validate type hints in functions, methods, and classes, ensuring data integrity at runtime. The library is currently at version 2.3.0 and maintains an active release cadence with frequent updates and bug fixes.","status":"active","version":"2.3.0","language":"en","source_language":"en","source_url":"https://github.com/connor-makowski/type_enforced","tags":["type-checking","runtime","annotations","validation","decorator"],"install":[{"cmd":"pip install type-enforced","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Requires Python 3.11 or higher. Older versions of type-enforced supported older Python versions (e.g., <=1.9.0 for 3.9, <=1.10.2 for 3.10).","package":"python","optional":false}],"imports":[{"symbol":"Enforcer","correct":"from type_enforced import Enforcer"}],"quickstart":{"code":"from type_enforced import Enforcer\n\n@Enforcer()\ndef greet(name: str, age: int) -> str:\n    return f\"Hello, {name}! You are {age} years old.\"\n\nprint(greet(\"Alice\", 30))\n\ntry:\n    greet(\"Bob\", \"twenty\") # This will raise a TypeError\nexcept TypeError as e:\n    print(f\"Caught expected error: {e}\")","lang":"python","description":"Demonstrates basic usage of the `@Enforcer` decorator on a function. Type mismatches at runtime will raise a `TypeError`."},"warnings":[{"fix":"Upgrade Python to 3.11+; replace list-based unions with standard `|` or `typing.Union`; ensure dict type hints specify both key and value types.","message":"Version 2.0.0 introduced significant breaking changes. Python 3.10 support was dropped (requiring >=3.11). List-based union types (e.g., `[int, float]`) are no longer supported; use `int | float` or `typing.Union[int, float]` instead. Dictionary type hints now strictly require both key and value types (e.g., `dict[str, int]`).","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Upgrade Python to 3.10+ (for `type_enforced>=1.10.0` but <2.0.0) or 3.11+ (for `type_enforced>=2.0.0`), or pin `type_enforced` to a compatible older version.","message":"Version 1.10.0 dropped support for Python 3.9. Users on Python 3.9 or older must use `type_enforced<=1.9.0` for full compatibility.","severity":"breaking","affected_versions":">=1.10.0"},{"fix":"Define all forward-referenced types before their first use in a decorated function, or ensure they are accessible in the module's global scope.","message":"When using forward references in type annotations (e.g., `def my_func(arg: 'MyClass')`), the type_enforced library lazily evaluates these types on the first call. Ensure the referenced class/type is defined and resolvable in the scope *before* the first execution of the decorated function/method to avoid resolution issues.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Explicitly define the exact type if subclass matching is not desired, or be aware of the default behavior.","message":"By default since version 2.1.0, `type_enforced` checks for subclasses when validating types. If you define `arg: ParentClass`, passing an instance of `ChildClass(ParentClass)` will pass validation. If strict exact type matching is required, this default behavior might be unexpected.","severity":"gotcha","affected_versions":">=2.1.0"},{"fix":"Be mindful of these parameters, especially in production environments, to ensure type enforcement is active and exceptions are raised as expected.","message":"The `@Enforcer` decorator has `enabled` and `strict` parameters (both `True` by default). Setting `enabled=False` disables all type checking for that function/class. Setting `strict=False` will print a warning to the console on type mismatch instead of raising a `TypeError`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}