Skip to content

Support python 3.10 optional syntax #193

@peku33

Description

@peku33

Python 3.10 introduced str | None syntax which should be equivalent to Optional[str]. At the moment such syntax isn't properly recognized by pure_protobuf.

Following declaration will raise error:

@dataclass(kw_only=True)
class Bar(BaseMessage):
    foo: Annotated[str | None, Field(1)]

with

UnsupportedAnnotationError: type annotation `str | None` is not supported

This is because get_origin in pure_protobuf.helpers._typing.extract_optional checks for Union only, while this syntax has UnionType from types origin. The rest is the same.

This probably could be easily fixed by changing is comparison to in (Union, UnionType), however special care must be taken, because (same as NoneType) UnionType was introduced in 3.10.

Thank you.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions