Skip to content
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

service: Fix mypy errors with types-protobuf 4.24 #362

Merged
merged 4 commits into from
Aug 24, 2023

Conversation

bkeryan
Copy link
Collaborator

@bkeryan bkeryan commented Aug 22, 2023

What does this Pull Request accomplish?

Fix these mypy errors:

ni_measurementlink_service\measurement\service.py:438: error: Item "_EnumTypeWrapper[Any]" of "Union[type[Enum], _EnumTypeWrapper[Any]]" has no attribute "__iter__" (not iterable)  [union-attr]
ni_measurementlink_service\measurement\service.py:440: error: Item "_EnumTypeWrapper[Any]" of "Union[type[Enum], _EnumTypeWrapper[Any]]" has no attribute "__iter__" (not iterable)  [union-attr]

This code has separate cases for EnumTypeWrapper and Enum, and these errors occur when mypy type-checks the Enum case using the _EnumTypeWrapper[_V] type.

EnumTypeWrapper is not a subclass of type, but types-protobuf 4.24 now pretends that _EnumTypeWrapper[_V] inherits from type: https://www.github.com/python/typeshed/pull/10203

This seems to affect the type narrowing in _enum_to_annotations_value. isinstance(enum_type, type) previously rejected EnumTypeWrapper, but now accepts it. and issubclass(enum_type, Enum) should still reject EnumTypeWrapper, but apparently it doesn't. This may be related to the limitations described in https://www.github.com/python/mypy/issues/15653

Anyway, isinstance(enum_type, EnumMeta) works at run time and with mypy.

Related improvements:

  • Use TypeGuard to simplify type narrowing.
  • Avoid importing typing_extensions at run time.

Why should this Pull Request be merged?

Updating poetry.lock causes mypy errors.

What testing has been done?

Ran mypy and pytest.

ni_measurementlink_service\measurement\service.py:438: error: Item "_EnumTypeWrapper[Any]" of "Union[type[Enum], _EnumTypeWrapper[Any]]" has no attribute "__iter__" (not iterable)  [union-attr]
ni_measurementlink_service\measurement\service.py:440: error: Item "_EnumTypeWrapper[Any]" of "Union[type[Enum], _EnumTypeWrapper[Any]]" has no attribute "__iter__" (not iterable)  [union-attr]

`EnumTypeWrapper` is not a subclass of `type`, but types-protobuf 4.24
now pretends that `_EnumTypeWrapper[_V]` inherits from `type`:
https://www.github.com/python/typeshed/pull/10203

This affected the type narrowing in `_enum_to_annotations_value`.
`isinstance(enum_type, type)` previously rejected `EnumTypeWrapper`, but
now it accepts `EnumTypeWrapper` (when type checking, it is a subclass of
`_EnumTypeWrapper[_V]`).
Change the return value of _is_protobuf_enum() to TypeGuard[_EnumTypeWrapper]
so that mypy can use this function for type narrowing.
@github-actions
Copy link

Test Results

     12 files  ±0       12 suites  ±0   1m 57s ⏱️ -6s
   182 tests ±0     157 ✔️ ±0    25 💤 ±0  0 ±0 
2 172 runs  ±0  1 872 ✔️ ±0  300 💤 ±0  0 ±0 

Results for commit 9a016fb. ± Comparison against base commit 7a4ce48.

@bkeryan bkeryan merged commit f54b16e into main Aug 24, 2023
17 checks passed
@bkeryan bkeryan deleted the users/bkeryan/types-protobuf-4.24 branch August 24, 2023 15:58
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.

3 participants