def _get_type_literal(self) -> FakeAnnotation: if not self.literal_items: raise ValueError(f"Value is not literal: {self.raw}") items = [ TypeValue(self.service_name, self.prefix, i) for i in self.literal_items ] if all(i.is_literal_item() for i in items): item_constants = [ self._parse_constant(i.value or "") for i in items ] return TypeLiteral(*item_constants) item_types = [i.get_type() for i in items] if all([i is item_types[0] for i in item_types]): return item_types[0] return TypeSubscript(Type.Union, item_types)
def get_client_method(self) -> Method: """ Get `get_paginator` method for `Client`. """ return Method( name="get_paginator", decorators=[Type.overload], docstring=self.docstring, arguments=[ Argument("self", None), Argument( "operation_name", TypeLiteral(f"{self.name}Name", [self.operation_name]), ), ], return_type=ExternalImport( source=ImportString.parent() + ImportString(ServiceModuleName.paginator.value), name=self.name, ), )
def parse_boto3_stubs_package( session: Session, service_names: Iterable[ServiceName], package_data: type[BasePackageData]) -> Boto3StubsPackage: """ Parse data for boto3_stubs package. Arguments: session -- boto3 session. service_names -- All available service names. Returns: Boto3StubsPackage structure. """ result = Boto3StubsPackage(package_data, service_names=service_names) for service_name in result.service_names: result.service_packages.append( parse_fake_service_package(session, service_name, package_data)) init_arguments = [ Argument("region_name", TypeSubscript(Type.Optional, [Type.str]), Type.Ellipsis), Argument("api_version", TypeSubscript(Type.Optional, [Type.str]), Type.Ellipsis), Argument("use_ssl", TypeSubscript(Type.Optional, [Type.bool]), Type.Ellipsis), Argument( "verify", TypeSubscript(Type.Union, [Type.bool, Type.str, Type.none]), Type.Ellipsis, ), Argument("endpoint_url", TypeSubscript(Type.Optional, [Type.str]), Type.Ellipsis), Argument("aws_access_key_id", TypeSubscript(Type.Optional, [Type.str]), Type.Ellipsis), Argument("aws_secret_access_key", TypeSubscript(Type.Optional, [Type.str]), Type.Ellipsis), Argument("aws_session_token", TypeSubscript(Type.Optional, [Type.str]), Type.Ellipsis), Argument("config", TypeSubscript(Type.Optional, [TypeClass(Config)]), Type.Ellipsis), ] client_function_decorators: list[TypeAnnotation] = [] if len(result.service_packages) > 1: client_function_decorators.append(Type.overload) for service_package in result.service_packages: package_name = Boto3StubsPackageData.get_service_package_name( service_package.service_name) service_argument = Argument( "service_name", TypeLiteral( service_package.service_name.class_name + "Type", [service_package.service_name.boto3_name], ), ) client_function = Function( name="client", decorators=client_function_decorators, docstring="", arguments=[ service_argument, *init_arguments, ], return_type=ExternalImport( source=ImportString(package_name, ServiceModuleName.client.value), name=service_package.client.name, ), body_lines=["..."], ) result.init_functions.append(client_function) result.session_class.methods.append( Method( name="client", decorators=client_function_decorators, docstring="", arguments=[ Argument("self", None), service_argument, *init_arguments, ], return_type=ExternalImport( source=ImportString(package_name, ServiceModuleName.client.value), name=service_package.client.name, ), body_lines=["..."], )) service_resource_packages = [ i for i in result.service_packages if i.service_resource ] resource_function_decorators: list[TypeAnnotation] = [] if len(service_resource_packages) > 1: resource_function_decorators.append(Type.overload) for service_package in service_resource_packages: assert service_package.service_resource package_name = Boto3StubsPackageData.get_service_package_name( service_package.service_name) service_argument = Argument( "service_name", TypeLiteral( service_package.service_name.class_name + "Type", [service_package.service_name.boto3_name], ), ) resource_function = Function( name="resource", decorators=resource_function_decorators, docstring="", arguments=[ service_argument, *init_arguments, ], return_type=ExternalImport( source=ImportString(package_name, ServiceModuleName.service_resource.value), name=service_package.service_resource.name, ), body_lines=["..."], ) result.init_functions.append(resource_function) result.session_class.methods.append( Method( name="resource", decorators=resource_function_decorators, docstring="", arguments=[ Argument("self", None), service_argument, *init_arguments, ], return_type=ExternalImport( source=ImportString( package_name, ServiceModuleName.service_resource.value), name=service_package.service_resource.name, ), body_lines=["..."], )) return result
# FIXME: https://github.com/boto/botocore/issues/1807 ResourceStatusType: TypeLiteral = TypeLiteral( "ResourceStatusType", [ "CREATE_COMPLETE", "CREATE_FAILED", "CREATE_IN_PROGRESS", "DELETE_COMPLETE", "DELETE_FAILED", "DELETE_IN_PROGRESS", "DELETE_SKIPPED", "IMPORT_COMPLETE", "IMPORT_FAILED", "IMPORT_IN_PROGRESS", "IMPORT_ROLLBACK_COMPLETE", "IMPORT_ROLLBACK_FAILED", "IMPORT_ROLLBACK_IN_PROGRESS", "UPDATE_COMPLETE", "UPDATE_FAILED", "UPDATE_IN_PROGRESS", "ROLLBACK_COMPLETE", "ROLLBACK_FAILED", "ROLLBACK_IN_PROGRESS", "UPDATE_COMPLETE_CLEANUP_IN_PROGRESS", "UPDATE_ROLLBACK_COMPLETE", "UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS", "UPDATE_ROLLBACK_FAILED", "UPDATE_ROLLBACK_IN_PROGRESS", ], )
def setup_method(self) -> None: self.result = TypeLiteral("test", ("a", "b"))
def test_is_same(self) -> None: assert self.result.is_same(TypeLiteral("other", ["a", "b"])) assert not self.result.is_same(TypeLiteral("other", ["a", "b", "c"]))
class TestTypeLiteral: def setup_method(self) -> None: self.result = TypeLiteral("test", ("a", "b")) def test_init(self) -> None: assert self.result.children == {"a", "b"} assert hash(self.result) assert TypeLiteral("Type", ["a"]).name == "TypeType" assert TypeLiteral("__stringType", ["a", "b"]).name == "ABType" assert TypeLiteral("Protocol", ["a"]).name == "ProtocolType" assert TypeLiteral("Other", ["a"]).name == "Other" with pytest.raises(ValueError): TypeLiteral("test", []) def test_get_sort_key(self) -> None: assert self.result.get_sort_key() == "test" def test_render(self) -> None: assert self.result.render() == "test" assert TypeLiteral("test", ("a", "b")).render() == "test" assert TypeLiteral("test", ["a"]).render() == "Literal['a']" def test_render_children(self) -> None: assert self.result.render_children() == "'a', 'b'" def test_get_import_record(self) -> None: assert self.result.get_import_record().render( ) == "from .literals import test" assert (TypeLiteral( "test", ["a" ]).get_import_record().render() == "from typing import Literal") def test_add_child(self) -> None: with pytest.raises(ValueError): self.result.add_child(TypeLiteral("test", ("a", "b"))) def test_is_type(self) -> None: assert self.result.is_literal() assert not self.result.is_dict() assert not self.result.is_list() def test_copy(self) -> None: assert self.result.copy().children == self.result.children def test_is_same(self) -> None: assert self.result.is_same(TypeLiteral("other", ["a", "b"])) assert not self.result.is_same(TypeLiteral("other", ["a", "b", "c"]))
def test_add_child(self) -> None: with pytest.raises(ValueError): self.result.add_child(TypeLiteral("test", ("a", "b")))
def test_render(self) -> None: assert self.result.render() == "test" assert TypeLiteral("test", ("a", "b")).render() == "test" assert TypeLiteral("test", ["a"]).render() == "Literal['a']"
def parse_boto3_stubs_package( session: Session, service_names: List[ServiceName]) -> Boto3StubsPackage: """ Parse data for boto3_stubs package. Arguments: session -- boto3 session. service_names -- All available service names. Returns: Boto3StubsPackage structure. """ result = Boto3StubsPackage(service_names=service_names) for service_name in result.service_names: result.service_packages.append( parse_fake_service_package(session, service_name)) init_arguments = [ Argument("region_name", TypeSubscript(Type.Optional, [Type.str]), Type.none), Argument("api_version", TypeSubscript(Type.Optional, [Type.str]), Type.none), Argument("use_ssl", TypeSubscript(Type.Optional, [Type.bool]), Type.none), Argument("verify", TypeSubscript(Type.Union, [Type.bool, Type.str, Type.none]), Type.none), Argument("endpoint_url", TypeSubscript(Type.Optional, [Type.str]), Type.none), Argument("aws_access_key_id", TypeSubscript(Type.Optional, [Type.str]), Type.none), Argument("aws_secret_access_key", TypeSubscript(Type.Optional, [Type.str]), Type.none), Argument("aws_session_token", TypeSubscript(Type.Optional, [Type.str]), Type.none), Argument("config", TypeSubscript(Type.Optional, [TypeClass(Config)]), Type.none), ] for service_package in result.service_packages: client_function = Function( name="client", decorators=[Type.overload], docstring="", arguments=[ Argument("service_name", TypeLiteral(service_package.service_name.boto3_name)), *init_arguments, ], return_type=ExternalImport( source=ImportString(service_package.service_name.module_name, ServiceModuleName.client.value), name=service_package.client.name, ), body_lines=["..."], ) result.init_functions.append(client_function) result.session_class.methods.append( Method( name="client", decorators=[Type.overload], docstring="", arguments=[ Argument("self", None), Argument( "service_name", TypeLiteral(service_package.service_name.boto3_name)), *init_arguments, ], return_type=ExternalImport( source=ImportString( service_package.service_name.module_name, ServiceModuleName.client.value), name=service_package.client.name, ), body_lines=["..."], )) for service_package in result.service_packages: if service_package.service_resource: client_function = Function( name="resource", decorators=[Type.overload], docstring="", arguments=[ Argument( "service_name", TypeLiteral(service_package.service_name.boto3_name)), *init_arguments, ], return_type=ExternalImport( source=ImportString( service_package.service_name.module_name, ServiceModuleName.service_resource.value, ), name=service_package.service_resource.name, ), body_lines=["..."], ) result.init_functions.append(client_function) result.session_class.methods.append( Method( name="resource", decorators=[Type.overload], docstring="", arguments=[ Argument("self", None), Argument( "service_name", TypeLiteral( service_package.service_name.boto3_name)), *init_arguments, ], return_type=ExternalImport( source=ImportString( service_package.service_name.module_name, ServiceModuleName.service_resource.value, ), name=service_package.service_resource.name, ), body_lines=["..."], )) return result
def test_render(self) -> None: assert self.result.render() == "Literal['a', 'b']" with pytest.raises(ValueError): TypeLiteral().render()