Skip to content

Commit

Permalink
Remove most of boto3 imports
Browse files Browse the repository at this point in the history
  • Loading branch information
vemel committed Nov 21, 2024
1 parent ac3f823 commit c240234
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
3 changes: 1 addition & 2 deletions mypy_boto3_builder/parsers/service_resource_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
Copyright 2024 Vlad Emelianov
"""

from boto3.resources.base import ResourceMeta
from boto3.resources.model import ResourceModel

from mypy_boto3_builder.constants import SERVICE_RESOURCE
Expand Down Expand Up @@ -143,7 +142,7 @@ def _parse_methods(self) -> list[Method]:
def _get_resource_meta_class(self) -> ClassRecord:
return ClassRecord(
name=f"{self.service_name.class_name}ResourceMeta",
bases=[ExternalImport.from_class(ResourceMeta)],
bases=[ExternalImport(ImportString("boto3", "resources", "base"), "ResourceMeta")],
attributes=[
Attribute(
"client",
Expand Down
15 changes: 8 additions & 7 deletions mypy_boto3_builder/postprocessors/aiobotocore.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
from collections.abc import Iterable, Iterator, Mapping
from typing import Final

from boto3.dynamodb.table import BatchWriter, TableResource
from boto3.resources.base import ServiceResource
from boto3.resources.collection import ResourceCollection
from botocore.client import BaseClient
from botocore.config import Config
from botocore.eventstream import EventStream
Expand Down Expand Up @@ -77,22 +74,26 @@ class AioBotocorePostprocessor(BasePostprocessor):
ImportString("aiobotocore", "client"),
"AioBaseClient",
),
ExternalImport.from_class(ServiceResource): ExternalImport(
ExternalImport(
ImportString("boto3", "resources", "base"), "ServiceResource"
): ExternalImport(
ImportString("aioboto3", "resources", "base"),
"AIOBoto3ServiceResource",
safe=True,
),
ExternalImport.from_class(ResourceCollection): ExternalImport(
ExternalImport(
ImportString("boto3", "resources", "collection"), "ResourceCollection"
): ExternalImport(
ImportString("aioboto3", "resources", "collection"),
"AIOResourceCollection",
safe=True,
),
ExternalImport.from_class(BatchWriter): ExternalImport(
ExternalImport(ImportString("boto3", "dynamodb", "table"), "BatchWriter"): ExternalImport(
ImportString("aioboto3", "dynamodb", "table"),
"BatchWriter",
safe=True,
),
ExternalImport.from_class(TableResource): ExternalImport(
ExternalImport(ImportString("boto3", "dynamodb", "table"), "TableResource"): ExternalImport(
ImportString("aioboto3", "dynamodb", "table"),
"CustomTableResource",
safe=True,
Expand Down
10 changes: 7 additions & 3 deletions mypy_boto3_builder/structures/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

from collections.abc import Iterator

from boto3.resources.collection import ResourceCollection

from mypy_boto3_builder.constants import SERVICE_RESOURCE
from mypy_boto3_builder.import_helpers.import_string import ImportString
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.class_record import ClassRecord
from mypy_boto3_builder.type_annotations.external_import import ExternalImport
Expand All @@ -33,7 +32,12 @@ def __init__(
super().__init__(
name=name,
use_alias=True,
bases=[ExternalImport.from_class(ResourceCollection)],
bases=[
ExternalImport(
ImportString("boto3", "resources", "collection"),
"ResourceCollection",
)
],
)
self.service_name = service_name
self.attribute_name = attribute_name
Expand Down
5 changes: 2 additions & 3 deletions mypy_boto3_builder/structures/resource_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from collections.abc import Iterator
from typing import TYPE_CHECKING

from boto3.resources.base import ServiceResource as Boto3ServiceResource

from mypy_boto3_builder.import_helpers.import_string import ImportString
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.class_record import ClassRecord
from mypy_boto3_builder.type_annotations.external_import import ExternalImport
Expand All @@ -26,7 +25,7 @@ class ResourceRecord(ClassRecord):
def __init__(self, name: str, service_name: ServiceName) -> None:
super().__init__(
name=name,
bases=[ExternalImport.from_class(Boto3ServiceResource)],
bases=[ExternalImport(ImportString("boto3", "resources", "base"), "ServiceResource")],
use_alias=True,
)
self.service_name: ServiceName = service_name
Expand Down
7 changes: 2 additions & 5 deletions mypy_boto3_builder/structures/service_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
from collections.abc import Iterator
from typing import TYPE_CHECKING

from boto3.resources.base import ResourceMeta
from boto3.resources.base import ServiceResource as Boto3ServiceResource

from mypy_boto3_builder.enums.service_module_name import ServiceModuleName
from mypy_boto3_builder.exceptions import StructureError
from mypy_boto3_builder.import_helpers.import_string import ImportString
Expand Down Expand Up @@ -39,7 +36,7 @@ def __init__(
self.resource_meta_class = self._get_resource_meta_class(service_name)
super().__init__(
name=name,
bases=[ExternalImport.from_class(Boto3ServiceResource)],
bases=[ExternalImport(ImportString("boto3", "resources", "base"), "ServiceResource")],
)
self.service_name = service_name
self.collections: list[Collection] = []
Expand Down Expand Up @@ -68,7 +65,7 @@ def get_class_name(service_name: ServiceName) -> str:
def _get_resource_meta_class(self, service_name: ServiceName) -> ClassRecord:
return ClassRecord(
name=f"{service_name.class_name}ResourceMeta",
bases=[ExternalImport.from_class(ResourceMeta)],
bases=[ExternalImport(ImportString("boto3", "resources", "base"), "ResourceMeta")],
attributes=[
Attribute(
"client",
Expand Down
4 changes: 2 additions & 2 deletions mypy_boto3_builder/type_maps/service_stub_map/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
Copyright 2024 Vlad Emelianov
"""

from boto3.s3.transfer import TransferConfig
from botocore.client import BaseClient

from mypy_boto3_builder.import_helpers.import_string import ImportString
from mypy_boto3_builder.structures.argument import Argument
from mypy_boto3_builder.structures.method import Method
from mypy_boto3_builder.type_annotations.external_import import ExternalImport
Expand All @@ -25,7 +25,7 @@

config_arg = Argument(
"Config",
get_optional(ExternalImport.from_class(TransferConfig)),
get_optional(ExternalImport(ImportString("boto3", "s3", "transfer"), "TransferConfig")),
Type.Ellipsis,
)

Expand Down

0 comments on commit c240234

Please sign in to comment.