Skip to content

Commit

Permalink
Add automated install_requires build
Browse files Browse the repository at this point in the history
  • Loading branch information
vemel committed Dec 26, 2024
1 parent c503619 commit 1ef7938
Show file tree
Hide file tree
Showing 29 changed files with 366 additions and 177 deletions.
9 changes: 7 additions & 2 deletions mypy_boto3_builder/generators/aioboto3_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
)
from mypy_boto3_builder.postprocessors.aioboto3 import AioBoto3Postprocessor
from mypy_boto3_builder.structures.package import Package
from mypy_boto3_builder.structures.service_package import ServicePackage
from mypy_boto3_builder.structures.packages.service_package import ServicePackage
from mypy_boto3_builder.writers.package_writer import PackageWriter


Expand Down Expand Up @@ -104,7 +104,7 @@ def generate_stubs_lite(self) -> Package | None:
package,
template_path=TemplatePath.types_aioboto3,
static_files_path=self._get_static_files_path(),
exclude_template_names=["session.pyi.jinja2"],
exclude_template_names=("session.pyi.jinja2",),
)
return package

Expand Down Expand Up @@ -192,4 +192,9 @@ def generate_custom_stubs(self) -> Package:
)

self._generate_full_stubs_services(package)
self.setup_package_writer.write_package(
package,
template_path=TemplatePath.types_aioboto3_custom,
include_template_names=("setup.py.jinja2",),
)
return package
11 changes: 8 additions & 3 deletions mypy_boto3_builder/generators/aiobotocore_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from mypy_boto3_builder.parsers.parse_wrapper_package import parse_types_aiobotocore_package
from mypy_boto3_builder.postprocessors.aiobotocore import AioBotocorePostprocessor
from mypy_boto3_builder.structures.package import Package
from mypy_boto3_builder.structures.service_package import ServicePackage
from mypy_boto3_builder.structures.packages.service_package import ServicePackage


class AioBotocoreGenerator(BaseGenerator):
Expand Down Expand Up @@ -95,7 +95,7 @@ def generate_stubs_lite(self) -> Package | None:
package,
template_path=TemplatePath.types_aiobotocore,
static_files_path=self._get_static_files_path(),
exclude_template_names=["session.pyi.jinja2"],
exclude_template_names=("session.pyi.jinja2",),
)
return package

Expand Down Expand Up @@ -152,11 +152,11 @@ def generate_full_stubs(self) -> Package | None:
"*.pyi",
)
package.set_description(package.get_short_description("All-in-one type annotations"))
self._generate_full_stubs_services(package)
self.package_writer.write_package(
package,
template_path=TemplatePath.types_aiobotocore_full,
)
self._generate_full_stubs_services(package)
return package

def generate_custom_stubs(self) -> Package:
Expand Down Expand Up @@ -184,4 +184,9 @@ def generate_custom_stubs(self) -> Package:
)

self._generate_full_stubs_services(package)
self.setup_package_writer.write_package(
package,
template_path=TemplatePath.types_aiobotocore_custom,
include_template_names=("setup.py.jinja2",),
)
return package
8 changes: 7 additions & 1 deletion mypy_boto3_builder/generators/base_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.package import Package
from mypy_boto3_builder.structures.package_extra import PackageExtra
from mypy_boto3_builder.structures.service_package import ServicePackage
from mypy_boto3_builder.structures.packages.service_package import ServicePackage
from mypy_boto3_builder.utils.github import download_and_extract
from mypy_boto3_builder.utils.package_builder import PackageBuilder
from mypy_boto3_builder.utils.pypi_manager import PyPIManager
Expand Down Expand Up @@ -70,6 +70,11 @@ def __init__(
generate_package=self.is_package(),
cleanup=cleanup,
)
self.setup_package_writer = PackageWriter(
output_path=self.output_path,
generate_package=self.is_package(),
cleanup=False,
)

def is_package(self) -> bool:
"""
Expand Down Expand Up @@ -205,6 +210,7 @@ def _generate_full_stubs_services(self, package: Package) -> None:
package=service_package,
templates_path=self.service_template_path,
)
package.install_requires.update(service_package.install_requires)

@abstractmethod
def generate_docs(self) -> None:
Expand Down
13 changes: 9 additions & 4 deletions mypy_boto3_builder/generators/boto3_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
from mypy_boto3_builder.parsers.mypy_boto3_package import parse_mypy_boto3_package
from mypy_boto3_builder.parsers.parse_wrapper_package import parse_types_boto3_package
from mypy_boto3_builder.postprocessors.botocore import BotocorePostprocessor
from mypy_boto3_builder.structures.mypy_boto3_package import MypyBoto3Package
from mypy_boto3_builder.structures.package import Package
from mypy_boto3_builder.structures.service_package import ServicePackage
from mypy_boto3_builder.structures.types_boto3_package import TypesBoto3Package
from mypy_boto3_builder.structures.packages.mypy_boto3_package import MypyBoto3Package
from mypy_boto3_builder.structures.packages.service_package import ServicePackage
from mypy_boto3_builder.structures.packages.types_boto3_package import TypesBoto3Package


class Boto3Generator(BaseGenerator):
Expand Down Expand Up @@ -192,12 +192,12 @@ def generate_full_stubs(self) -> Package | None:
"*.pyi",
)
package.set_description(package.get_short_description("All-in-one type annotations"))
self._generate_full_stubs_services(package)
self.package_writer.write_package(
package=package,
template_path=TemplatePath.types_boto3_full,
static_files_path=None,
)
self._generate_full_stubs_services(package)
return package

def generate_custom_stubs(self) -> Package:
Expand Down Expand Up @@ -225,4 +225,9 @@ def generate_custom_stubs(self) -> Package:
)

self._generate_full_stubs_services(package)
self.setup_package_writer.write_package(
package,
template_path=TemplatePath.types_boto3_custom,
include_template_names=("setup.py.jinja2",),
)
return package
9 changes: 7 additions & 2 deletions mypy_boto3_builder/generators/types_boto3_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from mypy_boto3_builder.parsers.parse_wrapper_package import parse_types_boto3_package
from mypy_boto3_builder.postprocessors.botocore import BotocorePostprocessor
from mypy_boto3_builder.structures.package import Package
from mypy_boto3_builder.structures.service_package import ServicePackage
from mypy_boto3_builder.structures.packages.service_package import ServicePackage


class TypesBoto3Generator(BaseGenerator):
Expand Down Expand Up @@ -155,12 +155,12 @@ def generate_full_stubs(self) -> Package | None:
"*.pyi",
)
package.set_description(package.get_short_description("All-in-one type annotations"))
self._generate_full_stubs_services(package)
self.package_writer.write_package(
package=package,
template_path=TemplatePath.types_boto3_full,
static_files_path=None,
)
self._generate_full_stubs_services(package)
return package

def generate_custom_stubs(self) -> Package:
Expand Down Expand Up @@ -188,4 +188,9 @@ def generate_custom_stubs(self) -> Package:
)

self._generate_full_stubs_services(package)
self.setup_package_writer.write_package(
package,
template_path=TemplatePath.types_boto3_custom,
include_template_names=("setup.py.jinja2",),
)
return package
3 changes: 3 additions & 0 deletions mypy_boto3_builder/package_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class TypesBoto3FullPackageData(TypesBoto3PackageData):
name: str = ""
pypi_name: str = TypesBoto3PackageData.pypi_full_name
is_conda_forge_supported: bool = False
install_requires: tuple[str, ...] = ()

def get_service_pypi_name(self, service_name: ServiceName) -> str:
"""
Expand Down Expand Up @@ -212,6 +213,7 @@ class TypesAioBotocoreFullPackageData(TypesAioBotocorePackageData):
name: str = ""
pypi_name: str = TypesAioBotocorePackageData.pypi_full_name
is_conda_forge_supported: bool = False
install_requires: tuple[str, ...] = ()

def get_service_pypi_name(self, service_name: ServiceName) -> str:
"""
Expand Down Expand Up @@ -282,6 +284,7 @@ class Boto3StubsFullPackageData(Boto3StubsPackageData):
name: str = ""
pypi_name: str = Boto3StubsPackageData.pypi_full_name
is_conda_forge_supported: bool = False
install_requires: tuple[str, ...] = ()

def get_service_pypi_name(self, service_name: ServiceName) -> str:
"""
Expand Down
2 changes: 1 addition & 1 deletion mypy_boto3_builder/parsers/fake_service_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from mypy_boto3_builder.parsers.shape_parser import ShapeParser
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.client import Client
from mypy_boto3_builder.structures.packages.service_package import ServicePackage
from mypy_boto3_builder.structures.paginator import Paginator
from mypy_boto3_builder.structures.service_package import ServicePackage
from mypy_boto3_builder.structures.service_resource import ServiceResource
from mypy_boto3_builder.structures.waiter import Waiter
from mypy_boto3_builder.utils.strings import xform_name
Expand Down
2 changes: 1 addition & 1 deletion mypy_boto3_builder/parsers/mypy_boto3_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from mypy_boto3_builder.parsers.fake_service_package import parse_fake_service_package
from mypy_boto3_builder.parsers.resource_loader import ResourceLoader
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.mypy_boto3_package import MypyBoto3Package
from mypy_boto3_builder.structures.packages.mypy_boto3_package import MypyBoto3Package
from mypy_boto3_builder.type_annotations.type_literal import TypeLiteral


Expand Down
6 changes: 3 additions & 3 deletions mypy_boto3_builder/parsers/parse_wrapper_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from mypy_boto3_builder.parsers.wrapper_package_parser import WrapperPackageParser
from mypy_boto3_builder.postprocessors.aio_imports import replace_imports_with_aio
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.types_aioboto3_package import TypesAioBoto3Package
from mypy_boto3_builder.structures.types_aiobotocore_package import TypesAioBotocorePackage
from mypy_boto3_builder.structures.types_boto3_package import TypesBoto3Package
from mypy_boto3_builder.structures.packages.types_aioboto3_package import TypesAioBoto3Package
from mypy_boto3_builder.structures.packages.types_aiobotocore_package import TypesAioBotocorePackage
from mypy_boto3_builder.structures.packages.types_boto3_package import TypesBoto3Package
from mypy_boto3_builder.type_annotations.external_import import ExternalImport
from mypy_boto3_builder.type_annotations.internal_import import InternalImport
from mypy_boto3_builder.type_annotations.type_subscript import TypeSubscript
Expand Down
13 changes: 12 additions & 1 deletion mypy_boto3_builder/parsers/service_package_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
from mypy_boto3_builder.parsers.shape_parser import ShapeParser
from mypy_boto3_builder.service_name import ServiceName, ServiceNameCatalog
from mypy_boto3_builder.structures.method import Method
from mypy_boto3_builder.structures.packages.service_package import ServicePackage
from mypy_boto3_builder.structures.paginator import Paginator
from mypy_boto3_builder.structures.service_package import ServicePackage
from mypy_boto3_builder.structures.waiter import Waiter
from mypy_boto3_builder.type_annotations.type import Type
from mypy_boto3_builder.type_annotations.type_def_sortable import TypeDefSortable
from mypy_boto3_builder.type_maps.typed_dicts import CloudwatchEventTypeDef
from mypy_boto3_builder.utils.install_requires import InstallRequiresItem
from mypy_boto3_builder.utils.strings import xform_name
from mypy_boto3_builder.utils.type_checks import is_union
from mypy_boto3_builder.utils.type_def_sorter import TypeDefSorter
Expand Down Expand Up @@ -92,6 +93,16 @@ def parse(self) -> ServicePackage:
result.literals = result.extract_literals()
result.validate()

result.install_requires.clear()
typing_extensions_version = result.get_typing_extensions_fallback_version()
if typing_extensions_version:
result.install_requires.add(
InstallRequiresItem(
"typing-extensions",
drop_python_version=".".join(str(i) for i in typing_extensions_version),
)
)

return result

def fix_unions_for_py39(self, type_defs: Iterable[TypeDefSortable]) -> None:
Expand Down
2 changes: 1 addition & 1 deletion mypy_boto3_builder/parsers/wrapper_package_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from mypy_boto3_builder.structures.argument import Argument
from mypy_boto3_builder.structures.function import Function
from mypy_boto3_builder.structures.method import Method
from mypy_boto3_builder.structures.wrapper_package import WrapperPackage
from mypy_boto3_builder.structures.packages.wrapper_package import WrapperPackage
from mypy_boto3_builder.type_annotations.external_import import ExternalImport
from mypy_boto3_builder.type_annotations.type import Type
from mypy_boto3_builder.type_annotations.type_literal import TypeLiteral
Expand Down
2 changes: 1 addition & 1 deletion mypy_boto3_builder/postprocessors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from mypy_boto3_builder.logger import get_logger
from mypy_boto3_builder.parsers.resource_loader import ResourceLoader
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.service_package import ServicePackage
from mypy_boto3_builder.structures.packages.service_package import ServicePackage
from mypy_boto3_builder.type_annotations.type import Type
from mypy_boto3_builder.type_annotations.type_literal import TypeLiteral
from mypy_boto3_builder.type_annotations.type_subscript import TypeSubscript
Expand Down
2 changes: 2 additions & 0 deletions mypy_boto3_builder/structures/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from mypy_boto3_builder.package_data import BasePackageData
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.package_url import PackageURL
from mypy_boto3_builder.utils.install_requires import InstallRequires
from mypy_boto3_builder.utils.version import (
get_max_build_version,
get_min_build_version,
Expand Down Expand Up @@ -44,6 +45,7 @@ def __init__(
self.logger = get_logger()
self.url = PackageURL(self.pypi_name, self.data)
self.extras: list[PackageExtra] = []
self.install_requires = InstallRequires(self.data.install_requires)

@property
def library_version(self) -> str:
Expand Down
3 changes: 3 additions & 0 deletions mypy_boto3_builder/structures/packages/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Package wrappers for generated products.
"""
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from mypy_boto3_builder.package_data import MypyBoto3PackageData
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.package import Package
from mypy_boto3_builder.structures.service_package import ServicePackage
from mypy_boto3_builder.structures.packages.service_package import ServicePackage

if TYPE_CHECKING:
from mypy_boto3_builder.type_annotations.type_literal import TypeLiteral
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,21 @@ def mark_safe_typed_dicts(self) -> None:
if any(attribute.name in unsafe_keys for attribute in type_def.children):
type_def.is_safe_as_class = False
continue

def get_typing_extensions_fallback_version(self) -> tuple[int, ...] | None:
"""
Get max Python version that has fallbacks to typing-extensions.
"""
min_versions: set[tuple[int, ...]] = set()
for import_record_group in (
self.get_literals_required_import_records(),
self.get_client_required_import_records(),
self.get_service_resource_required_import_records(),
self.get_type_defs_required_import_records(),
):
for import_record in import_record_group.records:
if import_record.min_version:
min_versions.add(import_record.min_version)
if not min_versions:
return None
return max(min_versions)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Copyright 2024 Vlad Emelianov
"""

from mypy_boto3_builder.structures.wrapper_package import WrapperPackage
from mypy_boto3_builder.structures.packages.wrapper_package import WrapperPackage


class TypesAioBoto3Package(WrapperPackage):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Copyright 2024 Vlad Emelianov
"""

from mypy_boto3_builder.structures.wrapper_package import WrapperPackage
from mypy_boto3_builder.structures.packages.wrapper_package import WrapperPackage


class TypesAioBotocorePackage(WrapperPackage):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Copyright 2024 Vlad Emelianov
"""

from mypy_boto3_builder.structures.wrapper_package import WrapperPackage
from mypy_boto3_builder.structures.packages.wrapper_package import WrapperPackage


class TypesBoto3Package(WrapperPackage):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
from mypy_boto3_builder.service_name import ServiceName
from mypy_boto3_builder.structures.class_record import ClassRecord
from mypy_boto3_builder.structures.package import Package
from mypy_boto3_builder.utils.install_requires import InstallRequires
from mypy_boto3_builder.utils.version import get_builder_version

if TYPE_CHECKING:
from mypy_boto3_builder.structures.function import Function
from mypy_boto3_builder.structures.service_package import ServicePackage
from mypy_boto3_builder.structures.packages.service_package import ServicePackage
from mypy_boto3_builder.type_annotations.type_literal import TypeLiteral


Expand All @@ -42,6 +43,7 @@ def __init__(
{self.data.name: ("py.typed", "*.pyi", "*/*.pyi")} if self.data.name else {}
)
self._description = self.get_short_description()
self.install_requires = InstallRequires(self.data.install_requires)

@property
def description(self) -> str:
Expand Down
6 changes: 5 additions & 1 deletion mypy_boto3_builder/templates/common/service/setup.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ setup(
'Source': '{{ builder_repo_url }}',
'Tracker': '{{ builder_repo_url }}/issues',
},
{% if package.install_requires %}
install_requires=[
"typing-extensions>=4.1.0; python_version<\"3.12\"",
{% for item in package.install_requires.iterate_items() %}
'{{ item.render() }}'{{ '' if loop.last else ', ' }}
{% endfor -%}
],
{% endif %}
zip_safe=False,
)
Loading

0 comments on commit 1ef7938

Please sign in to comment.