danielgtaylor / python-betterproto

Clean, modern, Python 3.6+ code generator & library for Protobuf 3 and async gRPC
MIT License
1.5k stars 209 forks source link

Fix support for optional syntax with 3.9 #590

Closed benedikt-bartscher closed 1 month ago

benedikt-bartscher commented 1 month ago

Summary

Add support for future annotations

What is the feature request for?

The core library

The Problem

Currently future annotations are not supported. All custom type annotations are rendered as strings.

The Ideal Solution

Add a flag to enable future annotations

The Current Solution

No response

Gobot1234 commented 1 month ago

I don't see much point in enabling this when PEP-649 is just round the corner

benedikt-bartscher commented 1 month ago

using typing.310 and pydantic_dataclasses generated this:

@dataclass(eq=False, repr=False)
class Message(betterproto.Message):
    selection_request: "SelectionRequest" | None = betterproto.message_field(
        1, optional=True, group="message"
    )
    machine_event: "MachineEvent" | None = betterproto.message_field(
        2, optional=True, group="message"
    )

    @model_validator(mode="after")
    def check_oneof(cls, values):
        return cls._validate_field_groups(values)

pyright

/home/***/gen/hello/v1/__init__.py
  /home/***/gen/hello/v1/__init__.py:41:24 - error: Union syntax cannot be used with string operand; use quotes around entire expression (reportGeneralTypeIssues)
  /home/***/gen/hello/v1/__init__.py:44:20 - error: Union syntax cannot be used with string operand; use quotes around entire expression (reportGeneralTypeIssues)
Gobot1234 commented 1 month ago

Ah well that's just a bug with the processing. In the meantime, you can generate the imports differently. See the Configuration typing imports section of README