Skip to content

Syntax error output if a field is named 'from' #73

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

Closed
gaggle opened this issue May 24, 2019 · 3 comments · Fixed by #78
Closed

Syntax error output if a field is named 'from' #73

gaggle opened this issue May 24, 2019 · 3 comments · Fixed by #78

Comments

@gaggle
Copy link

gaggle commented May 24, 2019

This tool generates .pyi files with syntax error when parsing a proto-file with a field named from. E.g. foo.proto:

message FooRequest {
	int64 from = 5;
}

Running mypy on a project with this kind of autogenerated files results in syntax error:

$ mypy
package/schemas/foo_pb2.pyi:356: error: invalid syntax

This is because the foo_pb2.pyi file has this in it:

class FooRequest(google___protobuf___message___Message):
    from = ... # type: int

The from = ... is invalid syntax, and so we have a problem.

Just to say I've set up my mypy to ignore these files, mypy.ini:

[mypy]

[mypy-package.schemas.*]
ignore_errors = True

But this doesn't ignore syntax errors. As per python/mypy#6897 mypy can't ignore syntax errors. It would be favorable if this project did not generate syntax-invalid output.

I'm using versions:

$ python --version Python 3.6.7
mypy==0.701
mypy-protobuf==1.10

And here is a gist with the entire contents of the .proto file, the generated .py file (which does not have syntax errors and the problematic and unexcludable .pyi file:
https://gist.github.com/gaggle/8aef6102d314936c9eb33724194682b0

@gaggle gaggle changed the title Syntax error output using field named from Syntax error output if a field is named 'from' May 24, 2019
@nipunn1313
Copy link
Owner

Great bugreport! Thanks.

I don't know offhand how we're supposed to type a variable with a reserved keyword name. Will look into it w/ mypy docs / folks. Thanks for reporting!

@gvanrossum
Copy link
Contributor

It looks like the .py file generator sidesteps the issue by not generating Python code but some meta thing. So at runtime you'd have to write getattr(thing, 'from'). (And add # noqa :-)

Since there can't be any code that writes thing.from, maybe the simplest thing would be to just not emit the field at all? Or perhaps emit it with a # in front of it?

@ikonst
Copy link

ikonst commented May 24, 2019

I'd not emit them. See also https://developers.google.com/protocol-buffers/docs/reference/python-generated#keyword-conflicts

nipunn1313 added a commit that referenced this issue May 27, 2019
Added testing for this behavior.
Fixes #73. See #73 for details.
nipunn1313 added a commit that referenced this issue May 27, 2019
Added testing for this behavior.
Fixes #73. See #73 for details.
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

Successfully merging a pull request may close this issue.

4 participants