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

Fix 'typing' import collisions.

Open scott-hendricks opened this issue 1 year ago • 0 comments

Summary

The Kubernetes protobuf library contains a top level List object, which collided with the typing.List import in the top of each file. This resulted in the following error at import time, input sanitized:

Traceback (most recent call last):
...
    from ...k8s.io.api.core import v1 as __k8_s_io_api_core_v1__
  File "/.../k8s/io/api/core/v1/__init__.py", line 16, in <module>
    from ....apimachinery.pkg.apis.meta import v1 as ___apimachinery_pkg_apis_meta_v1__
  File "/.../k8s/io/apimachinery/pkg/apis/meta/v1/__init__.py", line 797, in <module>
    class ObjectMeta(betterproto.Message):
  File "/.../k8s/io/apimachinery/pkg/apis/meta/v1/__init__.py", line 931, in ObjectMeta
    owner_references: List["OwnerReference"] = betterproto.message_field(13)
                      ~~~~^^^^^^^^^^^^^^^^^^
TypeError: type 'List' is not subscriptable

This change makes the typing import a static import, and the fields keep the typing.[Object] qualifier, rather than having the from typing import [Object] import at the top of the file.

This results in usable libraries, where previously we were not able to import them.

Checklist

  • [x] If code changes were made then they have been tested.
    • [ ] I have updated the documentation to reflect the changes.
  • [x] This PR fixes an issue.
  • [ ] This PR adds something new (e.g. new method or parameters).
    • [ ] This change has an associated test.
  • [ ] This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • [ ] This PR is not a code change (e.g. documentation, README, ...)

Test Results

❯ poe test                     
Poe => pytest
============================================ test session starts ============================================
platform darwin -- Python 3.11.7, pytest-6.2.5, py-1.11.0, pluggy-1.2.0
rootdir: /.../python-betterproto, configfile: pytest.ini
plugins: cov-2.12.1, asyncio-0.12.0, mock-3.11.1
collected 699 items                                                                                         

tests/test_casing.py ........................................................................         [ 10%]
tests/test_deprecated.py ....                                                                         [ 10%]
tests/test_enum.py .................                                                                  [ 13%]
tests/test_features.py .....................                                                          [ 16%]
tests/test_get_ref_type.py ....................................................                       [ 23%]
tests/test_inputs.py .....X......X..x...................x...........X.........X......X..x............ [ 35%]
.......x...........X........X....x....................x.................x....x....................x.. [ 49%]
                                                                                                      [ 49%]
tests/test_mapmessage.py .                                                                            [ 49%]
tests/test_pickling.py .....                                                                          [ 50%]
tests/test_streams.py ............................                                                    [ 54%]
tests/test_struct.py .                                                                                [ 54%]
tests/test_timestamp.py ....                                                                          [ 55%]
tests/test_version.py .                                                                               [ 55%]
tests/grpc/test_grpclib_client.py ..............                                                      [ 57%]
tests/grpc/test_stream_stream.py .....                                                                [ 58%]
tests/inputs/bool/test_bool.py ...                                                                    [ 58%]
tests/inputs/casing/test_casing.py ...                                                                [ 58%]
tests/inputs/casing_inner_class/test_casing_inner_class.py ..                                         [ 59%]
tests/inputs/enum/test_enum.py ...........                                                            [ 60%]
tests/inputs/example_service/test_example_service.py .                                                [ 60%]
tests/inputs/google_impl_behavior_equivalence/test_google_impl_behavior_equivalence.py ....           [ 61%]
tests/inputs/googletypes_request/test_googletypes_request.py ...........                              [ 63%]
tests/inputs/googletypes_response/test_googletypes_response.py .........xxxxxxxxx                     [ 65%]
tests/inputs/googletypes_response_embedded/test_googletypes_response_embedded.py .                    [ 65%]
tests/inputs/import_service_input_message/test_import_service_input_message.py ...                    [ 66%]
tests/inputs/nestedtwice/test_nestedtwice.py ......                                                   [ 67%]
tests/inputs/oneof/test_oneof.py ....x                                                                [ 67%]
tests/inputs/oneof_default_value_serialization/test_oneof_default_value_serialization.py ...          [ 68%]
tests/inputs/oneof_enum/test_oneof_enum.py ...                                                        [ 68%]
tests/inputs/proto3_field_presence/test_proto3_field_presence.py ..                                   [ 68%]
tests/inputs/proto3_field_presence_oneof/test_proto3_field_presence_oneof.py .                        [ 69%]
tests/inputs/regression_387/test_regression_387.py .                                                  [ 69%]
tests/inputs/regression_414/test_regression_414.py .                                                  [ 69%]
tests/inputs/repeated_duration_timestamp/test_repeated_duration_timestamp.py .                        [ 69%]
tests/inputs/service_uppercase/test_service.py .                                                      [ 69%]
tests/inputs/timestamp_dict_encode/test_timestamp_dict_encode.py .................................... [ 74%]
..................................................................................................... [ 89%]
...........................................................................                           [100%]

================================ 673 passed, 19 xfailed, 7 xpassed in 5.73s =================================

scott-hendricks avatar Jun 07 '24 19:06 scott-hendricks