Skip to content

protobuf: Annotate well_known_types.pyi #9323

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

Merged
merged 24 commits into from
Dec 5, 2022
Merged

Conversation

ikonst
Copy link
Contributor

@ikonst ikonst commented Dec 2, 2022

google.protobuf.struct_pb2.Struct and .List offer high-level methods which we are partially blind to, making things like list(list_value) fail.

The methods return either JSON primitive types (numbers are always floats!), Struct or List, and accept that along with plain sequences and mappings of the above. python/typing#182 comes handy in that we can now rely on recursive type aliases.

@ikonst ikonst marked this pull request as draft December 2, 2022 20:42
@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member

You may need to invent a fictional __iter__ method to get the type checkers to accept that ListValue is iterable 🙂 We do that in a few other places.

Type checkers aren't very good at understanding that classes with __getitem__ are also iterable.

@ikonst
Copy link
Contributor Author

ikonst commented Dec 2, 2022

Thanks, I was just wondering why it "passes" in mypy...

@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member

You'll need to add an entry to https://github.com/python/typeshed/blob/main/stubs/protobuf/%40tests/stubtest_allowlist.txt to get stubtest to shut up about the fictional __iter__ method.

@AlexWaygood
Copy link
Member

No idea what's going on with pytype, that might need some investigation.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@ikonst ikonst marked this pull request as ready for review December 2, 2022 22:11
@AlexWaygood
Copy link
Member

Ahh, I'm guessing this breaks pytype because you're importing from one of the files that we exclude from the pytype check: https://github.com/python/typeshed/blob/main/tests/pytype_exclude_list.txt

I guess if need be, we could maybe just add more files to that excludelist. Hopefully that will fix it.

@github-actions

This comment has been minimized.

@ikonst ikonst requested a review from AlexWaygood December 2, 2022 22:48
@AlexWaygood AlexWaygood changed the title Annotate google.protobuf.struct_pb2 protobuf: Annotate well_known_types.pyi Dec 5, 2022
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! A few comments:

ikonst and others added 7 commits December 5, 2022 13:03
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@github-actions
Copy link
Contributor

github-actions bot commented Dec 5, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

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 this pull request may close these issues.

2 participants