Skip to content

8451/labrea-type-validation

Repository files navigation

Labrea Type Validation

Type validation for labrea using pydantic

lifecycle PyPI Downloads pre-commit Coverage docs

Installation

labrea-type-validation is available for install via pip.

pip install labrea-type-validation

Alternatively, you can install the latest development version from GitHub.

pip install git+https://github.com/8451/labrea-type-validation@develop

Usage

When creating an Option with labrea, specify the expected type using either of the following syntaxes:

from labrea import Option

X = Option("X", type=int)
Y = Option[int]("Y")

Either literal types or type hints can be used, such as List[int] or Union[str, List[str]].

To enable type validation, simply import labrea_type_validation and call the enable function:

import labrea_type_validation

X({"X": "1"})  # No error

labrea_type_validation.enable()

X({"X": "1"})
# ...
# TypeError: Expected type <class 'int'> but got <class 'str'> ('1')
#
# The above exception was the direct cause of the following exception:
#
# Traceback (most recent call last):
# ...
# labrea.exceptions.EvaluationError: Originating in Option('X') | Error during evaluation

Type validation can also be used in a with statement as a context manager using enabled.

with labrea_type_validation.enabled():
    X({"X": "1"})
# ...
# TypeError: Expected type <class 'int'> but got <class 'str'> ('1')
#
# The above exception was the direct cause of the following exception:
#
# Traceback (most recent call last):
# ...
# labrea.exceptions.EvaluationError: Originating in Option('X') | Error during evaluation

Alternatively, the LABREA_TYPE_VALIDATION_ENABLED environment variable can be set to TRUE.

Multithreaded Applications

Type validation is based on the labrea.runtime module. For this reason, type validation is enabled for the current thread and any threads spawned from it. If you are using a multithreaded application, ensure that type validation is enabled in the main thread before spawning any new threads.

Contributing

If you would like to contribute to labrea-type-validation, please read the Contributing Guide.

Changelog

A summary of recent updates to labrea-type-validation can be found in the Changelog.

Maintainers

Maintainer Email
Austin Warner [email protected]
Michael Stoepel [email protected]

Links

About

Type validation for labrea using pydantic

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages