python-betterproto icon indicating copy to clipboard operation
python-betterproto copied to clipboard

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

Open MicaelJarniac opened this issue 2 years ago • 2 comments

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.

MicaelJarniac avatar May 09 '23 16:05 MicaelJarniac

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

Gobot1234 avatar May 09 '23 17:05 Gobot1234

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]".

MicaelJarniac avatar May 09 '23 17:05 MicaelJarniac

This works now on b7

Gobot1234 avatar Aug 14 '24 22:08 Gobot1234