Skip to content

Commit

Permalink
Improve logging for union replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
vemel committed Jan 29, 2025
1 parent 9007f4c commit 3788f2d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
19 changes: 11 additions & 8 deletions mypy_boto3_builder/parsers/shape_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.argument import Argument
from mypy_boto3_builder.structures.attribute import Attribute
from mypy_boto3_builder.structures.class_record import ClassRecord
from mypy_boto3_builder.structures.method import Method
from mypy_boto3_builder.type_annotations.external_import import ExternalImport
from mypy_boto3_builder.type_annotations.fake_annotation import FakeAnnotation
Expand Down Expand Up @@ -1314,16 +1315,18 @@ def fix_typed_dict_names(self) -> None:

self._response_typed_dict_map.rename(response_typed_dict, new_typed_dict_name)

def convert_input_arguments_to_unions(self, methods: Iterable[Method]) -> None:
def convert_input_arguments_to_unions(
self, methods: Iterable[tuple[ClassRecord, Method]]
) -> None:
"""
Accept both input and output shapes in method arguments.
mypy does not compare TypedDicts, so we need to accept both input and output shapes.
https://github.com/youtype/mypy_boto3_builder/issues/209
"""
method_arguments = tuple(
(method, argument)
for method in methods
(parent_class, method, argument)
for parent_class, method in methods
for argument in method.arguments
if argument.type_annotation and argument.type_annotation in self._fixed_typed_dict_map
)
Expand All @@ -1334,15 +1337,15 @@ def convert_input_arguments_to_unions(self, methods: Iterable[Method]) -> None:
children=(input_typed_dict, output_typed_dict),
)
matching_method_arguments = (
(method, argument)
for method, argument in method_arguments
(parent_class, method, argument)
for parent_class, method, argument in method_arguments
if argument.type_annotation == input_typed_dict
)
for method, argument in matching_method_arguments:
for parent_class, method, argument in matching_method_arguments:
self.logger.debug(
f"Adding output shape to {method.name}"
f"Adding output shape to {parent_class.name}.{method.name}"
f" {argument.name} argument:"
f" {input_typed_dict.name} | {output_typed_dict.name}",
tags=f"{method.name} {argument.name}",
tags=(parent_class.name, method.name, argument.name),
)
argument.type_annotation = union_type_annotation
20 changes: 13 additions & 7 deletions mypy_boto3_builder/structures/packages/service_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from mypy_boto3_builder.import_helpers.import_record_group import ImportRecordGroup
from mypy_boto3_builder.package_data import BasePackageData
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.class_record import ClassRecord
from mypy_boto3_builder.structures.client import Client
from mypy_boto3_builder.structures.function import Function
from mypy_boto3_builder.structures.method import Method
Expand Down Expand Up @@ -382,16 +383,21 @@ def calculate_install_requires(self) -> None:
)
)

def iterate_methods(self) -> Generator[Method]:
def iterate_methods(self) -> Generator[tuple[ClassRecord, Method]]:
"""
Iterate over all methods.
Iterate over all parent, method pairs.
"""
yield from self.client.methods
for method in self.client.methods:
yield self.client, method
if self.service_resource:
yield from self.service_resource.methods
for method in self.service_resource.methods:
yield self.service_resource, method
for resource in self.service_resource.sub_resources:
yield from resource.methods
for method in resource.methods:
yield resource, method
for waiter in self.waiters:
yield from waiter.methods
for method in waiter.methods:
yield waiter, method
for paginator in self.paginators:
yield from paginator.methods
for method in paginator.methods:
yield paginator, method

0 comments on commit 3788f2d

Please sign in to comment.