Skip to content

Commit a9c2271

Browse files
committed
Use clashihg TypedDict logic for stubbed methods
1 parent ca5bdb8 commit a9c2271

File tree

6 files changed

+56
-33
lines changed

6 files changed

+56
-33
lines changed

mypy_boto3_builder/parsers/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from mypy_boto3_builder.type_annotations.type import Type
1919
from mypy_boto3_builder.type_annotations.type_subscript import TypeSubscript
2020
from mypy_boto3_builder.type_maps.service_stub_map import get_stub_method_map
21+
from mypy_boto3_builder.utils.strings import get_class_prefix
2122

2223

2324
def parse_client(service_name: ServiceName, shape_parser: ShapeParser) -> Client:
@@ -44,6 +45,11 @@ def parse_client(service_name: ServiceName, shape_parser: ShapeParser) -> Client
4445
shape_method_map = shape_parser.get_client_method_map()
4546
stub_method_map = get_stub_method_map(service_name, parent_name)
4647
method_map = {**shape_method_map, **stub_method_map}
48+
for method in stub_method_map.values():
49+
shape_parser.create_request_type_annotation(
50+
method=method,
51+
name=f"{parent_name}{get_class_prefix(method.name)}Request",
52+
)
4753

4854
result.methods.append(result.get_exceptions_property())
4955
result.methods.extend(method_map.values())

mypy_boto3_builder/parsers/resource_parser.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from mypy_boto3_builder.structures.resource_record import ResourceRecord
2020
from mypy_boto3_builder.type_annotations.internal_import import InternalImport
2121
from mypy_boto3_builder.type_maps.service_stub_map import get_stub_method_map
22+
from mypy_boto3_builder.utils.strings import get_class_prefix
2223

2324

2425
class ResourceParser:
@@ -58,6 +59,11 @@ def resource_model(self) -> ResourceModel:
5859
def _parse_method_map(self) -> dict[str, Method]:
5960
shape_method_map = self.shape_parser.get_resource_method_map(self.name)
6061
stub_method_map = get_stub_method_map(self.service_name, self.name)
62+
for method in stub_method_map.values():
63+
self.shape_parser.create_request_type_annotation(
64+
method=method,
65+
name=f"{self.name}{get_class_prefix(method.name)}Request",
66+
)
6167
return {**shape_method_map, **stub_method_map}
6268

6369
def _parse_attributes(self, collections: Iterable[Collection]) -> list[Attribute]:

mypy_boto3_builder/parsers/service_resource_parser.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from mypy_boto3_builder.structures.service_resource import ServiceResource
1717
from mypy_boto3_builder.type_annotations.internal_import import InternalImport
1818
from mypy_boto3_builder.type_maps.service_stub_map import get_stub_method_map
19+
from mypy_boto3_builder.utils.strings import get_class_prefix
1920

2021

2122
class ServiceResourceParser(ResourceParser):
@@ -52,6 +53,11 @@ def _get_meta_attribute(self, name: str) -> Attribute:
5253
def _parse_method_map(self) -> dict[str, Method]:
5354
shape_method_map = self.shape_parser.get_service_resource_method_map()
5455
stub_method_map = get_stub_method_map(self.service_name, self.name)
56+
for method in stub_method_map.values():
57+
self.shape_parser.create_request_type_annotation(
58+
method=method,
59+
name=f"{self.name}{get_class_prefix(method.name)}Request",
60+
)
5561
return {**shape_method_map, **stub_method_map}
5662

5763
def _get_resource_model(self) -> ResourceModel:

mypy_boto3_builder/parsers/shape_parser.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -353,37 +353,38 @@ def get_client_method_map(self) -> dict[str, Method]:
353353
),
354354
)
355355
if operation_model.input_shape:
356-
self._create_request_type_annotation(
356+
self.create_request_type_annotation(
357357
method=method,
358-
input_shape=operation_model.input_shape,
358+
name=operation_model.input_shape.name,
359359
optional_postfix="Request",
360360
)
361361
result[method.name] = method
362362
return result
363363

364-
def _create_request_type_annotation(
365-
self, method: Method, input_shape: Shape, optional_postfix: str
364+
def create_request_type_annotation(
365+
self, method: Method, name: str, optional_postfix: str = "Extra"
366366
) -> None:
367+
"""
368+
Create a request type annotation for the given method and input shape.
369+
"""
367370
method.create_request_type_annotation(
368-
self._get_non_clashing_typed_dict_name_for_shape(
369-
input_shape,
370-
optional_postfix="Request",
371+
self._get_non_clashing_typed_dict_name(
372+
name,
373+
optional_postfix=optional_postfix,
371374
)
372375
)
373376
if method.has_request_type_annotation():
374377
self._request_typed_dict_map.add(method.request_type_annotation)
375378

376-
def _get_non_clashing_typed_dict_name_for_shape(
377-
self, shape: Shape, optional_postfix: str
378-
) -> str:
379-
temp_typed_dict = TypeTypedDict(self._get_typed_dict_name(shape), ())
380-
return self._get_non_clashing_typed_dict_name(
379+
def _get_non_clashing_typed_dict_name(self, name: str, optional_postfix: str) -> str:
380+
temp_typed_dict = TypeTypedDict(get_type_def_name(name), ())
381+
return self._get_non_clashing_typed_dict_name_for_existing(
381382
temp_typed_dict,
382383
optional_postfix=optional_postfix,
383384
)
384385

385386
@staticmethod
386-
def _get_typed_dict_name(shape: Shape, postfix: str = "") -> str:
387+
def _get_typed_dict_name_for_shape(shape: Shape, postfix: str = "") -> str:
387388
return get_type_def_name(shape.name, postfix)
388389

389390
@staticmethod
@@ -478,7 +479,9 @@ def _parse_shape_structure(
478479
typed_dict = TypeTypedDict(typed_dict_name)
479480

480481
typed_dict_map = self._get_typed_dict_map(output=output, output_child=output_child)
481-
resource_typed_dict_name = self._get_typed_dict_name(shape, postfix=self.resource_name)
482+
resource_typed_dict_name = self._get_typed_dict_name_for_shape(
483+
shape, postfix=self.resource_name
484+
)
482485
found_typed_dict = typed_dict_map.get(typed_dict.name)
483486
found_resource_typed_dict = typed_dict_map.get(resource_typed_dict_name)
484487

@@ -550,7 +553,7 @@ def _parse_shape_list(
550553

551554
def _get_shape_type_name(self, shape: Shape) -> str:
552555
if isinstance(shape, StructureShape):
553-
return self._get_typed_dict_name(shape)
556+
return self._get_typed_dict_name_for_shape(shape)
554557

555558
if isinstance(shape, StringShape):
556559
return shape.name
@@ -740,9 +743,9 @@ def get_paginate_method(self, paginator_name: str) -> Method:
740743
type_ignore="override",
741744
)
742745
if operation_model.input_shape is not None:
743-
self._create_request_type_annotation(
746+
self.create_request_type_annotation(
744747
method=method,
745-
input_shape=operation_model.input_shape,
748+
name=operation_model.input_shape.name,
746749
optional_postfix="Paginate",
747750
)
748751
return method
@@ -792,9 +795,9 @@ def get_wait_method(self, waiter_name: str) -> Method:
792795
type_ignore="override",
793796
)
794797
if operation_model.input_shape is not None:
795-
self._create_request_type_annotation(
798+
self.create_request_type_annotation(
796799
method=method,
797-
input_shape=operation_model.input_shape,
800+
name=operation_model.input_shape.name,
798801
optional_postfix="Wait",
799802
)
800803
return method
@@ -1123,9 +1126,9 @@ def _get_resource_method(self, action_name: str, action_shape: ActionShape) -> M
11231126
),
11241127
)
11251128
if operation_model.input_shape is not None:
1126-
self._create_request_type_annotation(
1129+
self.create_request_type_annotation(
11271130
method=method,
1128-
input_shape=operation_model.input_shape,
1131+
name=operation_model.input_shape.name,
11291132
optional_postfix=f"{self.resource_name}{action_name}",
11301133
)
11311134
return method
@@ -1233,7 +1236,7 @@ def _get_typed_dict(
12331236
return typed_dict_map[name]
12341237
return None
12351238

1236-
def _get_non_clashing_typed_dict_name(
1239+
def _get_non_clashing_typed_dict_name_for_existing(
12371240
self,
12381241
typed_dict: TypeTypedDict,
12391242
postfix: str = "",
@@ -1264,7 +1267,10 @@ def _get_non_clashing_typed_dict_name(
12641267
f"Clashing typed dict name found: {new_typed_dict_name}",
12651268
tags=new_typed_dict_name,
12661269
)
1267-
return self._get_non_clashing_typed_dict_name(temp_typed_dict, postfix=optional_postfix)
1270+
return self._get_non_clashing_typed_dict_name_for_existing(
1271+
temp_typed_dict,
1272+
postfix=optional_postfix,
1273+
)
12681274

12691275
def fix_typed_dict_names(self) -> None:
12701276
"""
@@ -1284,7 +1290,7 @@ def fix_typed_dict_names(self) -> None:
12841290
continue
12851291

12861292
old_typed_dict_name = output_typed_dict.name
1287-
new_typed_dict_name = self._get_non_clashing_typed_dict_name(
1293+
new_typed_dict_name = self._get_non_clashing_typed_dict_name_for_existing(
12881294
output_typed_dict,
12891295
"Output",
12901296
)
@@ -1318,7 +1324,7 @@ def fix_typed_dict_names(self) -> None:
13181324
continue
13191325

13201326
old_typed_dict_name = response_typed_dict.name
1321-
new_typed_dict_name = self._get_non_clashing_typed_dict_name(
1327+
new_typed_dict_name = self._get_non_clashing_typed_dict_name_for_existing(
13221328
response_typed_dict,
13231329
postfix="Response",
13241330
)
@@ -1346,7 +1352,9 @@ def convert_input_arguments_to_unions(
13461352
if argument.type_annotation and is_type_parent(argument.type_annotation)
13471353
)
13481354
for input_typed_dict, output_typed_dict in self._fixed_typed_dict_map.items():
1349-
union_name = self._get_non_clashing_typed_dict_name(input_typed_dict, postfix="Union")
1355+
union_name = self._get_non_clashing_typed_dict_name_for_existing(
1356+
input_typed_dict, postfix="Union"
1357+
)
13501358
union_type_annotation = TypeUnion(
13511359
name=union_name,
13521360
children=(input_typed_dict, output_typed_dict),

mypy_boto3_builder/type_maps/service_stub_map/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from mypy_boto3_builder.service_name import ServiceName, ServiceNameCatalog
1212
from mypy_boto3_builder.structures.method import Method
1313
from mypy_boto3_builder.type_maps.service_stub_map import dynamodb, ec2, rds, s3
14-
from mypy_boto3_builder.utils.strings import get_type_def_name
1514

1615
ClassTypeMap = Mapping[str, Sequence[Method]]
1716
ServiceStubMap = Mapping[ServiceName, ClassTypeMap]
@@ -52,6 +51,4 @@ def get_stub_method_map(service_name: ServiceName, parent: str) -> dict[str, Met
5251
Get boto3 injected methods.
5352
"""
5453
methods = SERVICE_STUB_MAP.get(service_name, {}).get(parent, [])
55-
for method in methods:
56-
method.create_request_type_annotation(get_type_def_name(parent, method.name, "Request"))
5754
return {method.name: method.copy() for method in methods}

scripts/integration.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ def parse_args() -> CLINamespace:
271271
"--product",
272272
nargs="*",
273273
choices=product_names,
274-
default=[product_names[0]],
275-
help=f"Product to test. Default: {product_names[0]}",
274+
default=product_names,
275+
help="Product to test. Default: all",
276276
)
277277
parser.add_argument(
278278
"--python",
@@ -488,8 +488,8 @@ def main() -> None:
488488
logger.error(error)
489489
sys.exit(1)
490490

491-
# if TEMP_PATH.exists():
492-
# shutil.rmtree(TEMP_PATH)
491+
if TEMP_PATH.exists():
492+
shutil.rmtree(TEMP_PATH)
493493

494494

495495
if __name__ == "__main__":

0 commit comments

Comments
 (0)