def test_init(self) -> None: assert self.dict.wrapped_type == Dict assert hash(self.dict) with pytest.raises(ValueError): TypeAnnotation(TypeAnnotation(Dict)) with pytest.raises(ValueError): TypeAnnotation(str)
def manually_set_method(name: str) -> Method: if name == "create_tags": return Method( name="create_tags", arguments=[ Argument("DryRun", TypeAnnotation(bool), False), Argument( "Resources", TypeSubstript(TypeAnnotation(List), [TypeAnnotation(Any)]), True, ), Argument( "Tags", TypeSubstript(TypeAnnotation(List), [TypeAnnotation(Any)]), True, ), ], docstring="", return_type=TypeAnnotation(None), ) logger.warning(f"Unknown method: {name}") return Method(name=name, arguments=[], docstring="", return_type=TypeAnnotation(None))
def get_types(self) -> Set[FakeAnnotation]: types: Set[FakeAnnotation] = set() types.add(TypeAnnotation(Boto3Paginator)) for paginator in self.paginators: types.update(paginator.get_types()) for type_annotation in types: if isinstance(type_annotation, InternalImport): type_annotation.localize(self.service_name) return types
def get_types(self) -> Set[FakeAnnotation]: types: Set[FakeAnnotation] = set() types.add(TypeAnnotation(BaseClient)) for method in self.methods: types.update(method.get_types()) for type_annotation in types: if isinstance(type_annotation, InternalImport): type_annotation.localize(self.service_name) return types
class TestTypeAnnotation: def setup_method(self) -> None: self.dict = TypeAnnotation(Dict) def test_init(self) -> None: assert self.dict.wrapped_type == Dict assert hash(self.dict) with pytest.raises(ValueError): TypeAnnotation(TypeAnnotation(Dict)) with pytest.raises(ValueError): TypeAnnotation(str) def test_render(self) -> None: assert self.dict.render() == "Dict" def test_get_import_name(self) -> None: assert self.dict.get_import_name() == "Dict" self.dict.wrapped_type = str with pytest.raises(ValueError): self.dict.get_import_name() def test_get_import_record(self) -> None: assert self.dict.get_import_record().render( ) == "from typing import Dict" def test_copy(self) -> None: assert self.dict.copy().wrapped_type == Dict
class TestTypeAnnotation: def setup_method(self) -> None: self.dict = TypeAnnotation("Dict") def test_init(self) -> None: assert self.dict.get_import_name() == "Dict" assert hash(self.dict) with pytest.raises(ValueError): TypeAnnotation("str") def test_render(self) -> None: assert self.dict.render() == "Dict" def test_get_import_name(self) -> None: assert self.dict.get_import_name() == "Dict" def test_get_import_record(self) -> None: assert self.dict.get_import_record().render( ) == "from typing import Dict" def test_copy(self) -> None: assert self.dict.copy().get_import_name() == "Dict"
def get_types(self) -> Set[FakeAnnotation]: types: Set[FakeAnnotation] = set() types.add( ExternalImport( source="boto3.resources.base", name="ServiceResource", alias="Boto3ServiceResource", )) types.add(TypeAnnotation(ResourceCollection)) for method in self.methods: types.update(method.get_types()) for attribute in self.attributes: types.update(attribute.get_types()) for collection in self.collections: types.update(collection.get_types()) for sub_resource in self.sub_resources: types.update(sub_resource.get_types()) for type_annotation in types: if isinstance(type_annotation, InternalImport): type_annotation.localize(self.service_name) return types
class Type: """ Predefined FakeAnnotation instances. """ Union = TypeAnnotation(Union) Any = TypeAnnotation(Any) Dict = TypeAnnotation(Dict) List = TypeAnnotation(List) Optional = TypeAnnotation(Optional) Callable = TypeAnnotation(Callable) IO = TypeAnnotation(IO) overload = TypeAnnotation(overload) classmethod = TypeClass(classmethod) staticmethod = TypeClass(staticmethod) none = TypeConstant(None) str = TypeClass(str) Set = TypeAnnotation(Set) bool = TypeClass(bool) bytes = TypeClass(bytes) bytearray = TypeClass(bytearray) int = TypeClass(int) float = TypeClass(float) Ellipsis = TypeConstant(...) Generator = TypeAnnotation(Generator) Decimal = TypeClass(Decimal) Type = TypeAnnotation(TypingType) Iterator = TypeAnnotation(Iterator) ListAny = TypeSubscript(List, [Any]) DictStrAny = TypeSubscript(Dict, [str, Any]) IOBytes = TypeSubscript(IO, [bytes])
from datetime import datetime from typing import Callable, IO, List, Dict, Union, Any from mypy_boto3_builder.service_name import ServiceName from mypy_boto3_builder.type_annotations.fake_annotation import FakeAnnotation from mypy_boto3_builder.type_annotations.type_annotation import TypeAnnotation from mypy_boto3_builder.type_annotations.internal_import import InternalImport from mypy_boto3_builder.type_annotations.external_import import ExternalImport from mypy_boto3_builder.type_annotations.type_subscript import TypeSubstript TYPE_MAP: Dict[str, FakeAnnotation] = { "bytes": TypeAnnotation(bytes), "blob": TypeAnnotation(bytes), "boolean": TypeAnnotation(bool), "function": TypeSubstript( TypeAnnotation(Callable), [TypeAnnotation(...), TypeAnnotation(Any)]), "botocore or boto3 Client": ExternalImport(source="botocore.client", name="BaseClient"), "datetime": TypeAnnotation(datetime), "timestamp": TypeAnnotation(datetime), "dict": TypeSubstript( TypeAnnotation(Dict), [TypeAnnotation(str), TypeAnnotation(Any)]),
class Type: """ Predefined FakeAnnotation instances. """ Union = TypeAnnotation("Union") Any = TypeAnnotation("Any") Dict = TypeAnnotation("Dict") Mapping = TypeAnnotation("Mapping") List = TypeAnnotation("List") Sequence = TypeAnnotation("Sequence") Optional = TypeAnnotation("Optional") Callable = TypeAnnotation("Callable") IO = TypeAnnotation("IO") overload = TypeAnnotation("overload") none = TypeConstant(None) str = TypeClass(str) Set = TypeAnnotation("Set") bool = TypeClass(bool) bytes = TypeClass(bytes) bytearray = TypeClass(bytearray) int = TypeClass(int) float = TypeClass(float) Ellipsis = TypeConstant(...) Decimal = TypeClass(Decimal) Type = TypeAnnotation("Type") Iterator = TypeAnnotation("Iterator") AsyncIterator = TypeAnnotation("AsyncIterator") datetime = TypeClass(datetime) ListAny = TypeSubscript(List, [Any]) SequenceAny = TypeSubscript(Sequence, [Any]) MappingStrAny = TypeSubscript(Mapping, [str, Any]) DictStrAny = TypeSubscript(Dict, [str, Any]) DictStrStr = TypeSubscript(Dict, [str, str]) IOBytes = TypeSubscript(IO, [bytes]) RemoveArgument = RemoveArgument() @classmethod def get_optional(cls, wrapped: FakeAnnotation) -> FakeAnnotation: """ Get Optional type annotation. """ if (isinstance(wrapped, TypeSubscript) and isinstance(wrapped.parent, TypeAnnotation) and wrapped.parent.is_union()): result = wrapped.copy() result.add_child(cls.none) return result return TypeSubscript(cls.Optional, [wrapped])
def parse_return_type(meta: List[DocstringMeta]) -> FakeAnnotation: for docstring_meta in meta: if docstring_meta.args[0] == "rtype": return parse_type_from_str(docstring_meta.description) return TypeAnnotation(None)
def setup_method(self) -> None: self.dict = TypeAnnotation(Dict)
def test_init(self) -> None: assert self.dict.get_import_name() == "Dict" assert hash(self.dict) with pytest.raises(ValueError): TypeAnnotation("str")