Skip to content

Commit

Permalink
Rewrite all tupels to sets for lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
vemel committed Oct 25, 2024
1 parent 8e4655b commit 9175ac1
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 56 deletions.
4 changes: 2 additions & 2 deletions mypy_boto3_builder/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
CLIENT: Final[str] = "Client"

# keys to mark as NotRequired for output TypeDicts
NOT_REQUIRED_OUTPUT_KEYS: Final[tuple[str, ...]] = (
NOT_REQUIRED_OUTPUT_KEYS: Final[set[str]] = {
"NextToken",
"nextToken",
"Contents",
"Item",
"CommonPrefixes",
)
}

# python versions supported by output stubs
SUPPORTED_PY_VERSIONS: Final[set[tuple[int, int]]] = {
Expand Down
4 changes: 2 additions & 2 deletions mypy_boto3_builder/import_helpers/import_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class ImportString:
"""

_BUILTINS: Final[str] = "builtins"
_THIRD_PARTY: Final[tuple[str, ...]] = (
_THIRD_PARTY: Final[set[str]] = {
"boto3",
"botocore",
"aioboto3",
"aiobotocore",
"s3transfer",
"awscrt",
)
}

def __init__(self, parent: str, *parts: str) -> None:
all_parts = (parent, *parts)
Expand Down
12 changes: 6 additions & 6 deletions mypy_boto3_builder/postprocessors/aiobotocore.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ class AioBotocorePostprocessor(BasePostprocessor):
Postprocessor for aiobotocore classes and methods.
"""

COMMON_COLLECTION_METHOD_NAMES: Final[tuple[str, ...]] = (
_COMMON_COLLECTION_METHOD_NAMES: Final[set[str]] = {
"__iter__",
"__aiter__",
"all",
"pages",
"filter",
"limit",
"page_size",
)
NOT_ASYNC_METHOD_NAMES: Final[tuple[str, ...]] = (
}
_NOT_ASYNC_METHOD_NAMES: Final[set[str]] = {
"exceptions",
"get_waiter",
"get_paginator",
"can_paginate",
)
}

EXTERNAL_IMPORTS_MAP: Final[Mapping[ExternalImport, ExternalImport]] = {
ExternalImport.from_class(StreamingBody): ExternalImport(
Expand Down Expand Up @@ -98,14 +98,14 @@ def process_package(self) -> None:

def _make_async_client(self) -> None:
for method in self.package.client.methods:
if method.name in self.NOT_ASYNC_METHOD_NAMES:
if method.name in self._NOT_ASYNC_METHOD_NAMES:
continue
method.is_async = True

@classmethod
def _make_async_collection(cls, collection: Collection) -> None:
for method in collection.methods:
if method.name not in cls.COMMON_COLLECTION_METHOD_NAMES:
if method.name not in cls._COMMON_COLLECTION_METHOD_NAMES:
method.is_async = True

pages_method = collection.get_method("pages")
Expand Down
11 changes: 8 additions & 3 deletions mypy_boto3_builder/structures/argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import copy
from collections.abc import Iterator
from typing import Literal, Self
from typing import Final, Literal, Self

from mypy_boto3_builder.type_annotations.fake_annotation import FakeAnnotation
from mypy_boto3_builder.type_annotations.type_constant import TypeConstant
Expand All @@ -21,6 +21,11 @@ class Argument:
prefix -- Used for starargs.
"""

SELF_NAME: Final[str] = "self"
CLS_NAME: Final[str] = "cls"
KW_NAME: Final[str] = "*"
FIRST_NAMES: Final[set[str]] = {SELF_NAME, CLS_NAME}

def __init__(
self,
name: str,
Expand Down Expand Up @@ -48,14 +53,14 @@ def self(cls: type[Self]) -> Self:
"""
Create `self` argument.
"""
return cls(name="self", type_annotation=None)
return cls(name=cls.SELF_NAME, type_annotation=None)

@classmethod
def kwflag(cls: type[Self]) -> Self:
"""
Create `*` keywords separator.
"""
return cls(name="*", type_annotation=None)
return cls(name=cls.KW_NAME, type_annotation=None)

def is_kwflag(self) -> bool:
"""
Expand Down
6 changes: 3 additions & 3 deletions mypy_boto3_builder/structures/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class Client(ClassRecord):
Service Client.
"""

OWN_METHOD_NAMES: Final[tuple[str, ...]] = (
_OWN_METHOD_NAMES: Final[set[str]] = {
"get_waiter",
"get_paginator",
"exceptions",
)
}

def __init__(self, name: str, service_name: ServiceName, boto3_client: BaseClient) -> None:
super().__init__(name=name)
Expand Down Expand Up @@ -99,7 +99,7 @@ def own_methods(self) -> Iterator[Method]:
Get a list of auto-generated methods.
"""
for method in self.methods:
if method.name not in self.OWN_METHOD_NAMES:
if method.name not in self._OWN_METHOD_NAMES:
yield method

def get_exceptions_property(self) -> Method:
Expand Down
4 changes: 1 addition & 3 deletions mypy_boto3_builder/structures/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from mypy_boto3_builder.structures.argument import Argument
from mypy_boto3_builder.structures.function import Function

FIRST_ARGUMENT_NAMES = ("self", "cls")


class Method(Function):
"""
Expand All @@ -22,7 +20,7 @@ def get_self_argument(self) -> Argument | None:
if not self.arguments:
return None
first_argument = self.arguments[0]
if first_argument.name in FIRST_ARGUMENT_NAMES:
if first_argument.name in Argument.FIRST_NAMES:
return first_argument

return None
Expand Down
64 changes: 32 additions & 32 deletions mypy_boto3_builder/type_annotations/type_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,43 @@ class TypeAnnotation(FakeAnnotation):
wrapped_type -- Original type annotation as a string.
"""

_typing: Final[ImportString] = ImportString("typing")
_typing_extensions: Final[ImportString] = ImportString("typing_extensions")
_TYPING: Final[ImportString] = ImportString("typing")
_TYPING_EXTENSIONS: Final[ImportString] = ImportString("typing_extensions")

# Set of supported type annotations. value is default import module
SUPPORTED_TYPES: Final[Mapping[str, ImportString]] = {
"Union": _typing, # typing.Union
"Any": _typing, # typing.Any
"Dict": _typing, # typing.Dict
"List": _typing, # typing.List
"Set": _typing, # typing.Set
"Optional": _typing, # typing.Optional
"IO": _typing, # typing.IO
"overload": _typing, # typing.overload
"Type": _typing, # typing.Type
"NoReturn": _typing, # typing.NoReturn
"TypedDict": _typing, # typing_extensions.TypedDict / typing.TypedDict
"Literal": _typing, # typing_extensions.Literal / typing.Literal
"Mapping": _typing, # typing.Mapping
"Sequence": _typing, # typing.Sequence
"Callable": _typing, # typing.Callable
"Iterator": _typing, # typing.Iterator
"Awaitable": _typing, # typing.Awaitable
"AsyncIterator": _typing, # typing.AsyncIterator
"NotRequired": _typing, # typing_extensions.NotRequired / typing.NotRequired
"Unpack": _typing, # typing_extensions.Unpack / typing.Unpack
_SUPPORTED_TYPES: Final[Mapping[str, ImportString]] = {
"Union": _TYPING, # typing.Union
"Any": _TYPING, # typing.Any
"Dict": _TYPING, # typing.Dict
"List": _TYPING, # typing.List
"Set": _TYPING, # typing.Set
"Optional": _TYPING, # typing.Optional
"IO": _TYPING, # typing.IO
"overload": _TYPING, # typing.overload
"Type": _TYPING, # typing.Type
"NoReturn": _TYPING, # typing.NoReturn
"TypedDict": _TYPING, # typing_extensions.TypedDict / typing.TypedDict
"Literal": _TYPING, # typing_extensions.Literal / typing.Literal
"Mapping": _TYPING, # typing.Mapping
"Sequence": _TYPING, # typing.Sequence
"Callable": _TYPING, # typing.Callable
"Iterator": _TYPING, # typing.Iterator
"Awaitable": _TYPING, # typing.Awaitable
"AsyncIterator": _TYPING, # typing.AsyncIterator
"NotRequired": _TYPING, # typing_extensions.NotRequired / typing.NotRequired
"Unpack": _TYPING, # typing_extensions.Unpack / typing.Unpack
}

# Set of fallback type annotations
FALLBACK: Final[Mapping[str, tuple[tuple[int, int] | None, ImportString]]] = {
"NotRequired": ((3, 12), _typing_extensions),
"TypedDict": ((3, 12), _typing_extensions),
"Literal": ((3, 12), _typing_extensions),
"Unpack": ((3, 12), _typing_extensions),
_FALLBACK: Final[Mapping[str, tuple[tuple[int, int] | None, ImportString]]] = {
"NotRequired": ((3, 12), _TYPING_EXTENSIONS),
"TypedDict": ((3, 12), _TYPING_EXTENSIONS),
"Literal": ((3, 12), _TYPING_EXTENSIONS),
"Unpack": ((3, 12), _TYPING_EXTENSIONS),
}

def __init__(self, wrapped_type: str) -> None:
if wrapped_type not in self.SUPPORTED_TYPES:
if wrapped_type not in self._SUPPORTED_TYPES:
raise TypeAnnotationError(f"Cannot wrap {wrapped_type}")

self._wrapped_type: str = wrapped_type
Expand All @@ -80,11 +80,11 @@ def _get_import_records(self) -> set[ImportRecord]:
Create a safe Import Record for annotation.
"""
name = self.get_import_name()
source = self.SUPPORTED_TYPES[name]
if name not in self.FALLBACK:
source = self._SUPPORTED_TYPES[name]
if name not in self._FALLBACK:
return {ImportRecord(source=source, name=name)}

fallback_min_version, fallback_source = self.FALLBACK[name]
fallback_min_version, fallback_source = self._FALLBACK[name]

return {
ImportRecord(
Expand Down
6 changes: 3 additions & 3 deletions mypy_boto3_builder/type_annotations/type_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TypeUnion(TypeSubscript, TypeDefSortable):
Wrapper for name Union type annotations, like `MyUnion = Union[str, int]`.
"""

MIN_CHILDREN: Final[int] = 2
_MIN_CHILDREN: Final[int] = 2

def __init__(
self,
Expand All @@ -34,8 +34,8 @@ def __init__(
self.name = name
self.parent = Type.Union
self.children: list[FakeAnnotation] = list(children)
if len(self.children) < self.MIN_CHILDREN:
raise TypeAnnotationError(f"Union must have at least {self.MIN_CHILDREN} children")
if len(self.children) < self._MIN_CHILDREN:
raise TypeAnnotationError(f"Union must have at least {self._MIN_CHILDREN} children")
self._stringify = stringify

def is_stringified(self) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions mypy_boto3_builder/writers/package_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class PackageWriter:
cleanup -- Whether to remove unknown files
"""

PY_EXTENSIONS: Final[tuple[str, ...]] = (".py", ".pyi")
_PY_EXTENSIONS: Final[set[str]] = {".py", ".pyi"}

def __init__(self, output_path: Path, generate_setup: bool, cleanup: bool) -> None:
self.output_path = output_path
Expand Down Expand Up @@ -225,7 +225,7 @@ def _format_output(self, package: Package, paths: Sequence[Path]) -> None:
)
existing_paths = [path for path in paths if path.exists()]
format_python_paths = [
path for path in existing_paths if path.suffix.lower() in self.PY_EXTENSIONS
path for path in existing_paths if path.suffix.lower() in self._PY_EXTENSIONS
]
if format_python_paths:
ruff_formatter.format_python(format_python_paths)
Expand Down

0 comments on commit 9175ac1

Please sign in to comment.