You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot set min_length for optional string filed #804
Traceback (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "/path/report.py", line 13, in
author = msgspec.convert(element, type=Author)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Can only set min_length on a str, bytes, or collection type - type typing.Annotated[None | str, msgspec.Meta(min_length=10)] is invalid
We only support applying annotations directly to the type they apply to for clarity.
# This works, and makes it clear that the annotation applies to the `int`
Annotated[int, Meta(ge=1)] | None
# This doesn't work, but IMO it shouldn't. None isn't numeric and can't support >=
Annotated[int | None, Meta(ge=1)]
Since Optional[int] is the same as int | None, it's a slippery slope to applying annotations to a union and expecting them to apply to all union members. Annotated[int | str | None, Meta(ge=1)] for example. I'd rather just keep things simple and clear - annotations must be applied to the type directly, and not the union.
Activity
oek1ng commentedon Jan 24, 2025
Nonedoes not have a length, is that an issue? Try to useAnnonated[str, msgspec.Meta(min_length=10)] | NoneSiyet commentedon Apr 8, 2026
Resolved in the community fork msgspec-arise: issue #4, released in v0.20.2.
jcrist commentedon Apr 8, 2026
Not supporting this was intentional.
We only support applying annotations directly to the type they apply to for clarity.
Since
Optional[int]is the same asint | None, it's a slippery slope to applying annotations to a union and expecting them to apply to all union members.Annotated[int | str | None, Meta(ge=1)]for example. I'd rather just keep things simple and clear - annotations must be applied to the type directly, and not the union.Support constraints on Optional (union with None) types