I've been trying to define "-1 or > 0" and I got very close with this:
from typing import Union, Literal
from pydantic import PositiveInt
from pydantic.fields import Field
from pydantic_settings import BaseSettings
class MyClass(BaseSettings):
item: Union[Literal[-1], PositiveInt] = Field(union_mode=“left_to_right”, default=-1)
I believe the type definition alone works as desired (need to do a little more testing), but the pydantic_settings from an environment variable aspect does not.
The problem is that this is using pydantic settings and applying this from an environment variable, which are always 'string' type at the point they're matched against the type definition here. So it's technically Literal["-1"]
and this fails.
Otherwise, this appears to express what I want without a custom validator...
ge
meansgreater or equals to
andle
meansless than or equals to
. So if you want a custom range, you need to change these params accordingly. For exampleField(None, ge=-1, le=168)
– Surculose