From a5e0ef910f63c9a98b260cca4d17cfe3b5e9d46c Mon Sep 17 00:00:00 2001 From: James <50501825+Gobot1234@users.noreply.github.com> Date: Sun, 1 Nov 2020 14:23:02 +0000 Subject: [PATCH] Fixes for Python 3.9 (#140) Fix issue in logic for evaluating field types affecting python 3.9 --- .github/workflows/ci.yml | 2 +- src/betterproto/__init__.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 40a7f6f..204038d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,7 @@ jobs: - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v1 + uses: actions/setup-python@v2 with: python-version: ${{ matrix.python-version }} diff --git a/src/betterproto/__init__.py b/src/betterproto/__init__.py index b90dd05..e6daeb3 100644 --- a/src/betterproto/__init__.py +++ b/src/betterproto/__init__.py @@ -920,9 +920,9 @@ class Message(ABC): """ output: Dict[str, Any] = {} field_types = self._type_hints() + defaults = self._betterproto.default_gen for field_name, meta in self._betterproto.meta_by_field_name.items(): - field_type = field_types[field_name] - field_is_repeated = type(field_type) is type(typing.List) + field_is_repeated = defaults[field_name] is list value = getattr(self, field_name) cased_name = casing(field_name).rstrip("_") # type: ignore if meta.proto_type == TYPE_MESSAGE: @@ -988,7 +988,7 @@ class Message(ABC): output[cased_name] = b64encode(value).decode("utf8") elif meta.proto_type == TYPE_ENUM: if field_is_repeated: - enum_class: Type[Enum] = field_type.__args__[0] + enum_class: Type[Enum] = field_types[field_name].__args__[0] if isinstance(value, typing.Iterable) and not isinstance( value, str ): @@ -997,7 +997,7 @@ class Message(ABC): # transparently upgrade single value to repeated output[cased_name] = [enum_class(value).name] else: - enum_class: Type[Enum] = field_type # noqa + enum_class: Type[Enum] = field_types[field_name] # noqa output[cased_name] = enum_class(value).name else: output[cased_name] = value