From ec542191dc3f32cc8093663436f02561583b9a72 Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 27 Aug 2024 12:33:34 -0400 Subject: [PATCH] Fix ConfigHandler generic --- setuptools/config/setupcfg.py | 17 ++++++++++------- setuptools/tests/config/test_setupcfg.py | 4 ++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/setuptools/config/setupcfg.py b/setuptools/config/setupcfg.py index 54469f74a3a..4fee109e262 100644 --- a/setuptools/config/setupcfg.py +++ b/setuptools/config/setupcfg.py @@ -27,7 +27,6 @@ List, Tuple, TypeVar, - Union, cast, ) @@ -53,7 +52,7 @@ while the second element of the tuple is the option value itself """ AllCommandOptions = Dict["str", SingleCommandOptions] # cmd name => its options -Target = TypeVar("Target", bound=Union["Distribution", "DistributionMetadata"]) +Target = TypeVar("Target", "Distribution", "DistributionMetadata") def read_configuration( @@ -96,7 +95,7 @@ def _apply( filepath: StrPath, other_files: Iterable[StrPath] = (), ignore_option_errors: bool = False, -) -> tuple[ConfigHandler, ...]: +) -> tuple[ConfigMetadataHandler, ConfigOptionsHandler]: """Read configuration from ``filepath`` and applies to the ``dist`` object.""" from setuptools.dist import _Distribution @@ -122,7 +121,7 @@ def _apply( return handlers -def _get_option(target_obj: Target, key: str): +def _get_option(target_obj: Distribution | DistributionMetadata, key: str): """ Given a target object and option key, get that option from the target object, either through a get_{key} method or @@ -134,10 +133,14 @@ def _get_option(target_obj: Target, key: str): return getter() -def configuration_to_dict(handlers: tuple[ConfigHandler, ...]) -> dict: +def configuration_to_dict( + handlers: Iterable[ + ConfigHandler[Distribution] | ConfigHandler[DistributionMetadata] + ], +) -> dict: """Returns configuration data gathered by given handlers as a dict. - :param list[ConfigHandler] handlers: Handlers list, + :param Iterable[ConfigHandler] handlers: Handlers list, usually from parse_configuration() :rtype: dict @@ -254,7 +257,7 @@ def __init__( ensure_discovered: expand.EnsurePackagesDiscovered, ): self.ignore_option_errors = ignore_option_errors - self.target_obj = target_obj + self.target_obj: Target = target_obj self.sections = dict(self._section_options(options)) self.set_options: list[str] = [] self.ensure_discovered = ensure_discovered diff --git a/setuptools/tests/config/test_setupcfg.py b/setuptools/tests/config/test_setupcfg.py index 4f0a7349f55..8d957981235 100644 --- a/setuptools/tests/config/test_setupcfg.py +++ b/setuptools/tests/config/test_setupcfg.py @@ -7,7 +7,7 @@ import pytest from packaging.requirements import InvalidRequirement -from setuptools.config.setupcfg import ConfigHandler, read_configuration +from setuptools.config.setupcfg import ConfigHandler, Target, read_configuration from setuptools.dist import Distribution, _Distribution from setuptools.warnings import SetuptoolsDeprecationWarning @@ -16,7 +16,7 @@ from distutils.errors import DistutilsFileError, DistutilsOptionError -class ErrConfigHandler(ConfigHandler): +class ErrConfigHandler(ConfigHandler[Target]): """Erroneous handler. Fails to implement required methods.""" section_prefix = "**err**"