danielgtaylor / python-betterproto

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

Support `list` as well as `typing.List` (among others) #485

Closed MicaelJarniac closed 1 month ago

MicaelJarniac commented 1 year ago

I'm on Python 3.9.9, using betterproto==2.0.0b5.

On the generated code, it uses typing.List, and sometimes does List["FooBar"] if FooBar hasn't already been defined. This does work.

I do, however, use Ruff for linting and fixing common issues, and it turns typing.List["FooBar"] into list["FooBar"], since I'm already on a Python version that supports it.

This, though, causes errors with betterproto: TypeError: 'str' object is not callable

I can tell Ruff to not do this fix, but I think it'd be great if betterproto supported it correctly.

Generating code using typing.List is fine I guess, as it supports more Python versions, but also having support for the new list behavior would be great, as in newer versions, typing.List is deprecated.

I only mentioned list, but I suspect it'll be a similar thing for other types.

Gobot1234 commented 1 year ago

Currently you can make this work at runtime if you replace list["Foo"] with "list[Foo]" which is what I do

MicaelJarniac commented 1 year ago

Currently you can make this work at runtime if you replace list["Foo"] with "list[Foo]" which is what I do

I suspect a from __future__ import annotations would work too, then?

This is another thing I was thinking about suggesting, automatically doing this import, so it can use list[Foo] instead of list["Foo"] or "list[Foo]".

Gobot1234 commented 1 month ago

This works now on b7