diff --git a/mypy.ini b/mypy.ini index c5b13942c4..a63ccf787c 100644 --- a/mypy.ini +++ b/mypy.ini @@ -4,6 +4,7 @@ python_version = 3.8 strict = False warn_unused_ignores = True +warn_redundant_casts = True # required to support namespace packages: https://github.com/python/mypy/issues/14057 explicit_package_bases = True exclude = (?x)( diff --git a/setuptools/command/editable_wheel.py b/setuptools/command/editable_wheel.py index 55d477eebf..ae31bb4c79 100644 --- a/setuptools/command/editable_wheel.py +++ b/setuptools/command/editable_wheel.py @@ -515,7 +515,7 @@ def template_vars(self) -> tuple[str, str, dict[str, str], dict[str, list[str]]] ) legacy_namespaces = { - cast(str, pkg): find_package_path(pkg, roots, self.dist.src_root or "") + pkg: find_package_path(pkg, roots, self.dist.src_root or "") for pkg in self.dist.namespace_packages or [] } diff --git a/setuptools/config/_apply_pyprojecttoml.py b/setuptools/config/_apply_pyprojecttoml.py index c7e25b755f..5a8700051e 100644 --- a/setuptools/config/_apply_pyprojecttoml.py +++ b/setuptools/config/_apply_pyprojecttoml.py @@ -12,7 +12,6 @@ import logging import os -from collections.abc import Mapping from email.headerregistry import Address from functools import partial, reduce from inspect import cleandoc @@ -22,8 +21,9 @@ TYPE_CHECKING, Any, Callable, + Dict, + Mapping, Union, - cast, ) from .._path import StrPath from ..errors import RemovedConfigError @@ -35,7 +35,7 @@ from setuptools.dist import Distribution # noqa EMPTY: Mapping = MappingProxyType({}) # Immutable dict-like -_DictOrStr = Union[dict, str] +_ProjectReadmeValue = Union[str, Dict[str, str]] _CorrespFn = Callable[["Distribution", Any, StrPath], None] _Correspondence = Union[str, _CorrespFn] @@ -149,15 +149,16 @@ def _guess_content_type(file: str) -> str | None: raise ValueError(f"Undefined content type for {file}, {msg}") -def _long_description(dist: Distribution, val: _DictOrStr, root_dir: StrPath): +def _long_description(dist: Distribution, val: _ProjectReadmeValue, root_dir: StrPath): from setuptools.config import expand + file: str | tuple[()] if isinstance(val, str): - file: str | list = val + file = val text = expand.read_files(file, root_dir) - ctype = _guess_content_type(val) + ctype = _guess_content_type(file) else: - file = val.get("file") or [] + file = val.get("file") or () text = val.get("text") or expand.read_files(file, root_dir) ctype = val["content-type"] @@ -167,7 +168,7 @@ def _long_description(dist: Distribution, val: _DictOrStr, root_dir: StrPath): _set_config(dist, "long_description_content_type", ctype) if file: - dist._referenced_files.add(cast(str, file)) + dist._referenced_files.add(file) def _license(dist: Distribution, val: dict, root_dir: StrPath): diff --git a/setuptools/config/expand.py b/setuptools/config/expand.py index 7140dc8ed8..6ea6cf6d0e 100644 --- a/setuptools/config/expand.py +++ b/setuptools/config/expand.py @@ -36,8 +36,6 @@ Iterator, Mapping, TypeVar, - Union, - cast, ) from pathlib import Path from types import ModuleType @@ -326,18 +324,13 @@ def version(value: Callable | Iterable[str | int] | str) -> str: """When getting the version directly from an attribute, it should be normalised to string. """ - if callable(value): - value = value() + _value = value() if callable(value) else value - value = cast(Iterable[Union[str, int]], value) - - if not isinstance(value, str): - if hasattr(value, '__iter__'): - value = '.'.join(map(str, value)) - else: - value = '%s' % value - - return value + if isinstance(_value, str): + return _value + if hasattr(_value, '__iter__'): + return '.'.join(map(str, _value)) + return '%s' % _value def canonic_package_data(package_data: dict) -> dict: diff --git a/setuptools/dist.py b/setuptools/dist.py index 30cdfdb10b..43762960ba 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -272,6 +272,8 @@ class Distribution(_Distribution): } _patched_dist = None + # Used by build_py, editable_wheel and install_lib commands for legacy namespaces + namespace_packages: list[str] #: :meta private: DEPRECATED def patch_missing_pkg_info(self, attrs): # Fake up a replacement for the data that would normally come from