Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion doc/comparisons.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ In practice, most programmers simply skip validation or implement it only partia

## Runtime checking

Python 3 type annotations allow users to specify types for attributes and function returns, but these types are not normally checked at runtime, and so they do not have the same role of validating user input or programmer error as the type declarations in Params, Traits, Traitlets, pydantic, and attr. They also are limited to the type, so they cannot enforce constraints on range ('state' must be in the list ['Alabama', 'Alaska',...]). Thus even if type hinting is used, programmers still need to write code to actually validate the inputs to functions and methods, which is the role of packages like Param and Traitlets. Note that Pydantic focuses on [generating valid outputs at runtime](https://github.com/samuelcolvin/pydantic/issues/578) rather than detecting invalid inputs, so it may or may not be suitable for the same types of applications as the other libraries discussed here.
Python type annotations let you declare the types of attributes, function arguments, and return values, but Python itself does not enforce them at runtime. Static type checkers such as mypy and pyright verify them before the code runs, and `dataclasses` only uses them to generate code, so a dataclass field annotated as `int` will happily accept a string. Enforcing annotations at runtime requires an additional library such as [Beartype](https://beartype.readthedocs.io) or [Typeguard](https://typeguard.readthedocs.io), which check argument and return values when a decorated function is called. Param, by contrast, always validates at runtime, both when an object is instantiated and on every later assignment to a parameter, and with [`param.Model`](user_guide/Typing) those parameters can themselves be declared using type annotations.

Another difference is that Param has built-in value-level checking, not just type checking, such as requiring that a `state` parameter be one of `['Alabama', 'Alaska', ...]` or that a number fall within given bounds. With type annotations there are several ways to express such a constraint:

- List the allowed values with `Literal['Alabama', 'Alaska', ...]`, which static type checkers verify and runtime checkers such as Beartype and Typeguard also enforce.
- Attach a predicate with `Annotated`, as in Beartype's `Annotated[str, Is[lambda s: s in states]]`. Only Beartype enforces this predicate; static type checkers treat the annotation as a plain `str`.
- Define a dedicated `State` type whose `__instancecheck__` evaluates the predicate, as done by [phantom-types](https://phantom-types.readthedocs.io/en/main/pages/implementation.html).

Pydantic also supports value constraints, e.g. via `Field(ge=1, le=10)` or `Annotated` validators, but even for a plain type annotation it behaves differently from Param: in its default "lax" mode it [coerces compatible inputs](https://docs.pydantic.dev/latest/concepts/conversion_table/) to the declared type (for example the string `"1"` to the integer `1`) instead of rejecting them, which suits data parsing use cases. Pydantic's strict mode disables this coercion.


## Generality and ease of integration with your project
Expand Down