def process_service_paginator( session: Session, service_name: ServiceName, service_output_path: Path, import_record_renderer: ImportRecordRenderer, ) -> Optional[ServicePaginator]: logger.debug(f"Parsing {service_name} ServicePaginator") service_paginator = parse_service_paginator(session, service_name) if service_paginator is None or not service_paginator.paginators: logger.debug(f"Skipping {service_name} ServicePaginator") return None module_output_path = service_output_path / "paginator.py" logger.debug( f"Writing {service_name.boto3_name} ServicePaginator to {NicePath(module_output_path)}" ) write_service_paginator( service_paginator, module_output_path, import_record_renderer, include_doc=service_name.is_with_docs(), ) return service_paginator
def test_generate_stubs( self, process_botocore_stubs_mock: MagicMock, process_boto3_stubs_mock: MagicMock, process_master_mock: MagicMock, process_service_mock: MagicMock, ) -> None: with tempfile.TemporaryDirectory() as output_dir: namespace = Namespace( log_level=0, output_path=Path(output_dir), service_names=["s3"], build_version="1.2.3.post4", installed=False, skip_master=False, skip_services=False, builder_version="1.2.3", generate_docs=False, list_services=False, ) session_mock = MagicMock() generate_stubs(namespace, service_names=[ServiceName("s3", "S3")], session=session_mock) process_botocore_stubs_mock.assert_called() process_boto3_stubs_mock.assert_called() process_master_mock.assert_called() process_service_mock.assert_called()
def test_get_selected_service_names(self) -> None: assert [ i.name for i in get_selected_service_names( ["s3", "ec2"], [ServiceName("ec2", "EC2"), ServiceName("other", "Other")]) ] == ["ec2"] assert [ i.name for i in get_selected_service_names( ["all", "ec2"], [ServiceName("ec2", "EC2"), ServiceName("other", "Other")]) ] == ["ec2", "other"] assert get_selected_service_names(["s3", "ec2"], []) == [] with patch.object(BotocoreChangelog, "get_updated_service_names", lambda x, y: ["ecs"]): assert [ i.name for i in get_selected_service_names( ["updated", "ec2"], [ ServiceName("ec2", "EC2"), ServiceName("ecs", "ECS"), ServiceName("other", "Other"), ], ) ] == ["ec2", "ecs"]
def collection(self) -> Collection: return Collection( name="name", attribute_name="attribute", parent_name="Parent", service_name=ServiceName("s3", "S3"), type_annotation=TypeClass(ServiceName), object_class_name="object", )
def test_init(self) -> None: service_name = ServiceName("my-service", "MyService") assert service_name.name == "my-service" assert service_name.import_name == "my_service" assert service_name.class_name == "MyService" assert service_name.boto3_version == "latest" assert service_name.boto3_name == "my-service" assert service_name.extras_name == "my-service" assert ( service_name.boto3_doc_link == "https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/my-service.html#MyService" )
def test_init(self) -> None: service_name = ServiceName("my-service", "MyService") self.assertEqual(service_name.name, "my-service") self.assertEqual(service_name.import_name, "my_service") self.assertEqual(service_name.class_name, "MyService") self.assertEqual(service_name.boto3_version, "latest") self.assertEqual(service_name.boto3_name, "my-service") self.assertEqual(service_name.module_name, "mypy_boto3_my_service") self.assertEqual(service_name.pypi_name, "mypy-boto3-my-service") self.assertEqual(service_name.extras_name, "my-service") self.assertEqual( service_name.doc_link, "https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/my-service.html#MyService", )
def parse_collections( resource: Boto3ServiceResource, ) -> Generator[Collection, None, None]: for collection in resource.meta.resource_model.collections: yield Collection( name=collection.name, docstring=clean_doc(getdoc(collection)), type=InternalImport( name=collection.name, service_name=ServiceName(resource.meta.service_name), ), methods=list( parse_methods( get_instance_public_methods( getattr(resource, collection.name)))), )
def service_package(self) -> ServicePackage: service_name = ServiceName("service", "Service") return ServicePackage( Boto3StubsPackageData, service_name=service_name, client=Client("Client", service_name, "base"), service_resource=ServiceResource("ServiceResource", service_name, "base"), waiters=[Waiter("waiter", "waiter", service_name)], paginators=[ Paginator("Paginator", "Paginator", "paginate", service_name) ], typed_dicts=[TypeTypedDict("MyTypedDict", [])], literals=[TypeLiteral("MyLiteral", ["value"])], helper_functions=["helper_function"], )
def test_process_service_docs( self, PackageWriterMock: MagicMock, parse_service_package_mock: MagicMock, ) -> None: session_mock = MagicMock() service_name_mock = MagicMock() result = process_service_docs( session_mock, service_name_mock, Path("my_path"), [ServiceName("ec2", "EC2")], ) PackageWriterMock().write_service_docs.assert_called() parse_service_package_mock.assert_called_with(session_mock, service_name_mock, Boto3StubsPackageData) assert result == parse_service_package_mock()
def process_service_client( session: Session, service_name: ServiceName, service_output_path: Path, import_record_renderer: ImportRecordRenderer, ) -> Client: logger.debug(f"Parsing {service_name} Client") client = parse_client(session, service_name) module_output_path = service_output_path / "client.py" logger.debug( f"Writing Client for {service_name.boto3_name} to {NicePath(module_output_path)}" ) write_client( client, module_output_path, import_record_renderer, include_doc=service_name.is_with_docs(), ) return client
def test_process_service( self, PackageWriterMock: MagicMock, parse_service_package_mock: MagicMock, ) -> None: session_mock = MagicMock() service_name_mock = MagicMock() parse_service_package_mock().typed_dicts = [MagicMock()] result = process_service( session_mock, service_name_mock, Path("my_path"), True, [ServiceName("ec2", "EC2")], version="1.2.3", ) PackageWriterMock().write_service_package.assert_called() parse_service_package_mock.assert_called_with(session_mock, service_name_mock, Boto3StubsPackageData) assert result == parse_service_package_mock()
def get_cli_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser("mypy_boto3_builder", description="Builder for mypy-boto3.") parser.add_argument("-d", "--debug", action="store_true", help="Show debug messages") parser.add_argument("-v", "--version", action="store_true", help="Show package version") parser.add_argument("-f", "--format", action="store_true", help="Format output with black") parser.add_argument("--skip-master", action="store_true", help="Whether to skip master module") parser.add_argument("--skip-services", action="store_true", help="Whether to skip service modules") parser.add_argument("output_path", metavar="OUTPUT_PATH", help="Output path", type=get_absolute_path) parser.add_argument( "-s", "--services", dest="service_names", nargs="*", metavar="SERVICE_NAME", help="List of AWS services, by default ", type=get_service_name, default=ServiceName.items(), ) return parser
def process_service_resource( session: Session, service_name: ServiceName, service_output_path: Path, import_record_renderer: ImportRecordRenderer, ) -> Optional[ServiceResource]: logger.debug(f"Parsing {service_name} ServiceResource") service_resource = parse_service_resource(session, service_name) if service_resource is None: logger.debug(f"Skipping {service_name} ServiceResource") return None module_output_path = service_output_path / "service_resource.py" logger.debug( f"Writing ServiceResource for {service_name.boto3_name} to {NicePath(module_output_path)}" ) write_service_resource( service_resource, module_output_path, import_record_renderer, include_doc=service_name.is_with_docs(), ) return service_resource
def test_special_name(self) -> None: assert ServiceName("lambda", "MyService").import_name == "lambda_"
def get_service_name(name: str) -> ServiceName: try: return ServiceName(name) except ValueError: raise argparse.ArgumentTypeError(f"Unknown service {name}")
def test_special_name(self) -> None: self.assertEqual( ServiceName("lambda", "MyService").import_name, "lambda_") self.assertEqual( ServiceName("lambda", "MyService").module_name, "mypy_boto3_lambda")
def test_is_essential(self) -> None: assert not ServiceName("my-service", "MyService").is_essential() assert ServiceName("lambda", "MyService").is_essential()
def test_special_name(self) -> None: assert ServiceName("lambda", "MyService").import_name == "lambda_" assert ServiceName("lambda", "MyService").module_name == "mypy_boto3_lambda"
def test_is_essential(self) -> None: self.assertFalse(ServiceName("my-service", "MyService").is_essential()) self.assertTrue(ServiceName("lambda", "MyService").is_essential())