Skip to content

Runtime validation with Pydantic

Pydantic is an amazing package that allows for runtime validation of objects and easy (de)serialization which would proof extremely useful for writing and reading results. Moreover, it an also validate function calls by working with type annotations. For example.

from pydantic import validate_call, PositiveFloat

@validate_call
def some_function(radius: PositiveFloat):

    # radii should be larger than zero, so we use the Pydantic type PositiveFloat
    return radius

some_function(1)
some_function(1.0)
# works fine

some_function(0)
some_function(-1)
# will raise a runtime error

This would be extremely useful, for example, for the geometry or vector field templates.

Edited by Koerber, Lukas (FWIN-C) - 108045
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information