Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some type fixes #1138

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions backend/src/hatchling/builders/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

from hatchling.builders.config import BuilderConfig
from hatchling.builders.plugin.interface import BuilderInterface
from hatchling.plugin.manager import PluginManager


class AppBuilderConfig(BuilderConfig):
class AppBuilderConfig(BuilderConfig[PluginManager]):
SUPPORTED_VERSIONS = ('3.11', '3.10', '3.9', '3.8', '3.7')

def __init__(self, *args: Any, **kwargs: Any) -> None:
Expand Down Expand Up @@ -79,7 +80,7 @@ def pyapp_version(self) -> str:
return self.__pyapp_version


class AppBuilder(BuilderInterface):
class AppBuilder(BuilderInterface[AppBuilderConfig, PluginManager]):
"""
Build applications
"""
Expand Down
15 changes: 10 additions & 5 deletions backend/src/hatchling/builders/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@

import os
from contextlib import contextmanager
from typing import TYPE_CHECKING, Any, Generator, TypeVar
from typing import TYPE_CHECKING, Any, Generic, TypeVar

import pathspec

from hatchling.builders.constants import DEFAULT_BUILD_DIRECTORY, EXCLUDED_DIRECTORIES, BuildEnvVars
from hatchling.builders.utils import normalize_inclusion_map, normalize_relative_directory, normalize_relative_path
from hatchling.metadata.utils import normalize_project_name
from hatchling.plugin.manager import PluginManagerBound
from hatchling.utils.fs import locate_file

if TYPE_CHECKING:
from typing import Generator

from typing_extensions import Self

from hatchling.builders.plugin.interface import BuilderInterface


class BuilderConfig:
class BuilderConfig(Generic[PluginManagerBound]):
def __init__(
self,
builder: BuilderInterface,
builder: BuilderInterface[Self, PluginManagerBound],
root: str,
plugin_name: str,
build_config: dict[str, Any],
Expand Down Expand Up @@ -69,7 +74,7 @@ def __init__(
self.__require_runtime_features: list[str] | None = None

@property
def builder(self) -> BuilderInterface:
def builder(self) -> BuilderInterface[Self, PluginManagerBound]:
return self.__builder

@property
Expand Down Expand Up @@ -927,4 +932,4 @@ def env_var_enabled(env_var: str, *, default: bool = False) -> bool:
return default


BuilderConfigBound = TypeVar('BuilderConfigBound', bound=BuilderConfig)
BuilderConfigBound = TypeVar('BuilderConfigBound', bound=BuilderConfig[Any])
12 changes: 8 additions & 4 deletions backend/src/hatchling/builders/hooks/plugin/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@
from typing import TYPE_CHECKING, Any, Generic, cast

from hatchling.builders.config import BuilderConfigBound
from hatchling.plugin.manager import PluginManagerBound

if TYPE_CHECKING:
from hatchling.bridge.app import Application
from hatchling.metadata.core import ProjectMetadata


class BuildHookInterface(Generic[BuilderConfigBound]): # no cov
class BuildHookInterface(Generic[BuilderConfigBound, PluginManagerBound]): # no cov
"""
Example usage:

```python tab="plugin.py"
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
from hatchling.plugin.manager import PluginManager

from .builder import SpecialBuilderConfig

class SpecialBuildHook(BuildHookInterface):

class SpecialBuildHook(BuildHookInterface[SpecialBuilderConfig, PluginManager]):
PLUGIN_NAME = 'special'
...
```
Expand All @@ -42,7 +46,7 @@ def __init__(
root: str,
config: dict[str, Any],
build_config: BuilderConfigBound,
metadata: ProjectMetadata,
metadata: ProjectMetadata[PluginManagerBound],
directory: str,
target_name: str,
app: Application | None = None,
Expand Down Expand Up @@ -87,7 +91,7 @@ def config(self) -> dict[str, Any]:
return self.__config

@property
def metadata(self) -> ProjectMetadata:
def metadata(self) -> ProjectMetadata[PluginManagerBound]:
# Undocumented for now
return self.__metadata

Expand Down
25 changes: 17 additions & 8 deletions backend/src/hatchling/builders/plugin/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,20 @@ class BuilderInterface(ABC, Generic[BuilderConfigBound, PluginManagerBound]):
Example usage:

```python tab="plugin.py"
from hatchling.builders.config import BuilderConfig
from hatchling.builders.plugin.interface import BuilderInterface
from hatchling.plugin.manager import PluginManager


class SpecialBuilder(BuilderInterface):
class SpecialBuilderConfig(BuilderConfig[PluginManager]): ...


class SpecialBuilder(BuilderInterface[SpecialBuilderConfig, PluginManager]):
PLUGIN_NAME = 'special'

def get_config_class(self) -> type[SpecialBuilderConfig]:
return SpecialBuilderConfig

...
```

Expand All @@ -45,7 +54,7 @@ class SpecialBuilder(BuilderInterface):


@hookimpl
def hatch_register_builder():
def hatch_register_builder() -> type[SpecialBuilder]:
return SpecialBuilder
```
"""
Expand All @@ -58,7 +67,7 @@ def __init__(
root: str,
plugin_manager: PluginManagerBound | None = None,
config: dict[str, Any] | None = None,
metadata: ProjectMetadata | None = None,
metadata: ProjectMetadata[PluginManagerBound] | None = None,
app: Application | None = None,
) -> None:
self.__root = root
Expand Down Expand Up @@ -272,12 +281,12 @@ def plugin_manager(self) -> PluginManagerBound:
if self.__plugin_manager is None:
from hatchling.plugin.manager import PluginManager

self.__plugin_manager = PluginManager()
self.__plugin_manager = cast(PluginManagerBound, PluginManager())

return self.__plugin_manager

@property
def metadata(self) -> ProjectMetadata:
def metadata(self) -> ProjectMetadata[PluginManagerBound]:
if self.__metadata is None:
from hatchling.metadata.core import ProjectMetadata

Expand Down Expand Up @@ -389,7 +398,7 @@ def get_version_api(self) -> dict[str, Callable]:
Each callable must have the following signature:

```python
def ...(build_dir: str, build_data: dict) -> str:
def ...(build_dir: str, **build_data: dict) -> str:
```

The return value must be the absolute path to the built artifact.
Expand Down Expand Up @@ -418,11 +427,11 @@ def clean(self, directory: str, versions: list[str]) -> None:
"""

@classmethod
def get_config_class(cls) -> type[BuilderConfig]:
def get_config_class(cls) -> type[BuilderConfigBound]:
"""
Must return a subclass of [BuilderConfig](../utilities.md#hatchling.builders.config.BuilderConfig).
"""
return BuilderConfig
return cast(type[BuilderConfigBound], BuilderConfig)

@staticmethod
def normalize_file_name_component(file_name: str) -> str:
Expand Down
5 changes: 3 additions & 2 deletions backend/src/hatchling/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
replace_file,
)
from hatchling.metadata.spec import DEFAULT_METADATA_VERSION, get_core_metadata_constructors
from hatchling.plugin.manager import PluginManager
from hatchling.utils.constants import DEFAULT_BUILD_SCRIPT, DEFAULT_CONFIG_FILE

if TYPE_CHECKING:
Expand Down Expand Up @@ -85,7 +86,7 @@ def __exit__(
self.fd.close()


class SdistBuilderConfig(BuilderConfig):
class SdistBuilderConfig(BuilderConfig[PluginManager]):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)

Expand Down Expand Up @@ -140,7 +141,7 @@ def support_legacy(self) -> bool:
return self.__support_legacy


class SdistBuilder(BuilderInterface):
class SdistBuilder(BuilderInterface[SdistBuilderConfig, PluginManager]):
"""
Build an archive of the source files
"""
Expand Down
2 changes: 1 addition & 1 deletion backend/src/hatchling/builders/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def safe_walk(path: str) -> Iterable[tuple[str, list[str], list[str]]]:
yield root, dirs, files


def get_known_python_major_versions() -> map:
def get_known_python_major_versions() -> map[str]:
return map(str, sorted((2, 3)))


Expand Down
5 changes: 3 additions & 2 deletions backend/src/hatchling/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
set_zip_info_mode,
)
from hatchling.metadata.spec import DEFAULT_METADATA_VERSION, get_core_metadata_constructors
from hatchling.plugin.manager import PluginManager

if TYPE_CHECKING:
from types import TracebackType
Expand Down Expand Up @@ -158,7 +159,7 @@ def __exit__(
self.fd.close()


class WheelBuilderConfig(BuilderConfig):
class WheelBuilderConfig(BuilderConfig[PluginManager]):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)

Expand Down Expand Up @@ -376,7 +377,7 @@ def get_raw_fs_path_name(directory: str, name: str) -> str: # noqa: ARG004
return name


class WheelBuilder(BuilderInterface):
class WheelBuilder(BuilderInterface[WheelBuilderConfig, PluginManager]):
"""
Build a binary distribution (.whl file)
"""
Expand Down
10 changes: 5 additions & 5 deletions backend/src/hatchling/metadata/spec.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Callable
from typing import TYPE_CHECKING, Any, Callable

if TYPE_CHECKING:
from hatchling.metadata.core import ProjectMetadata
Expand All @@ -20,7 +20,7 @@ def get_core_metadata_constructors() -> dict[str, Callable]:
}


def construct_metadata_file_1_2(metadata: ProjectMetadata, extra_dependencies: tuple[str] | None = None) -> str:
def construct_metadata_file_1_2(metadata: ProjectMetadata[Any], extra_dependencies: tuple[str] | None = None) -> str:
"""
https://peps.python.org/pep-0345/
"""
Expand Down Expand Up @@ -79,7 +79,7 @@ def construct_metadata_file_1_2(metadata: ProjectMetadata, extra_dependencies: t
return metadata_file


def construct_metadata_file_2_1(metadata: ProjectMetadata, extra_dependencies: tuple[str] | None = None) -> str:
def construct_metadata_file_2_1(metadata: ProjectMetadata[Any], extra_dependencies: tuple[str] | None = None) -> str:
"""
https://peps.python.org/pep-0566/
"""
Expand Down Expand Up @@ -161,7 +161,7 @@ def construct_metadata_file_2_1(metadata: ProjectMetadata, extra_dependencies: t
return metadata_file


def construct_metadata_file_2_2(metadata: ProjectMetadata, extra_dependencies: tuple[str] | None = None) -> str:
def construct_metadata_file_2_2(metadata: ProjectMetadata[Any], extra_dependencies: tuple[str] | None = None) -> str:
"""
https://peps.python.org/pep-0643/
"""
Expand Down Expand Up @@ -243,7 +243,7 @@ def construct_metadata_file_2_2(metadata: ProjectMetadata, extra_dependencies: t
return metadata_file


def construct_metadata_file_2_3(metadata: ProjectMetadata, extra_dependencies: tuple[str] | None = None) -> str:
def construct_metadata_file_2_3(metadata: ProjectMetadata[Any], extra_dependencies: tuple[str] | None = None) -> str:
"""
https://peps.python.org/pep-0639/
"""
Expand Down