Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set min_length for optional string filed #804

Open
RoTorEx opened this issue Jan 21, 2025 · 1 comment
Open

Cannot set min_length for optional string filed #804

RoTorEx opened this issue Jan 21, 2025 · 1 comment

Comments

@RoTorEx
Copy link

RoTorEx commented Jan 21, 2025

Description

Error while creating an object with optional filed with min_length meta.

from typing import Annotated

import msgspec


class Author(msgspec.Struct):
    name: Annotated[str, msgspec.Meta(min_length=1)]
    biography: Annotated[None | str, msgspec.Meta(min_length=10)]


element = {"name": "Me", "biography": None}

author = msgspec.convert(element, type=Author)

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


msgspec version 0.19.0

@oek1ng
Copy link

oek1ng commented Jan 24, 2025

None does not have a length, is that an issue? Try to use Annonated[str, msgspec.Meta(min_length=10)] | None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants