From 25a71b6795a8836d703d4e01a066dfb9ee16ef5c Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 21 May 2024 14:01:26 -0400 Subject: [PATCH] Remove calls to typing.cast with better type narrowing --- mypy.ini | 1 + setuptools/command/editable_wheel.py | 2 +- setuptools/config/_apply_pyprojecttoml.py | 16 +++++++++++----- setuptools/config/expand.py | 20 +++++++------------- setuptools/dist.py | 1 + 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/mypy.ini b/mypy.ini index 146a0e19293..3c717421a8a 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 b8ed84750a6..939d097a530 100644 --- a/setuptools/command/editable_wheel.py +++ b/setuptools/command/editable_wheel.py @@ -519,7 +519,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 3626282a791..d381a294c7e 100644 --- a/setuptools/config/_apply_pyprojecttoml.py +++ b/setuptools/config/_apply_pyprojecttoml.py @@ -10,7 +10,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,6 +21,7 @@ Callable, Dict, List, + Mapping, Optional, Set, Tuple, @@ -39,7 +39,7 @@ from setuptools.dist import Distribution # noqa EMPTY: Mapping = MappingProxyType({}) # Immutable dict-like -_DictOrStr = Union[dict, str] +_LongDescriptionValue = Union[str, Mapping[str, str]] _CorrespFn = Callable[["Distribution", Any, StrPath], None] _Correspondence = Union[str, _CorrespFn] @@ -153,15 +153,17 @@ def _guess_content_type(file: str) -> Optional[str]: 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: _LongDescriptionValue, root_dir: StrPath +): from setuptools.config import expand if isinstance(val, str): - file: Union[str, list] = val + file: Union[str, tuple[()]] = val text = expand.read_files(file, root_dir) ctype = _guess_content_type(val) 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"] @@ -171,6 +173,10 @@ def _long_description(dist: "Distribution", val: _DictOrStr, root_dir: StrPath): _set_config(dist, "long_description_content_type", ctype) if file: + # Due to bad type narrowing, mypy sees file as `str | tuple[Any, ...]` + # and flipping the condition mypy sees val as `str | None`. + # Pyright gets both w/o issue. Can't use `tuple[()]()` until Python 3.9. So casting instead. + # https://github.com/python/mypy/issues/17273 dist._referenced_files.add(cast(str, file)) diff --git a/setuptools/config/expand.py b/setuptools/config/expand.py index 0d8d58add82..adf01ab436c 100644 --- a/setuptools/config/expand.py +++ b/setuptools/config/expand.py @@ -39,7 +39,6 @@ Tuple, TypeVar, Union, - cast, ) from pathlib import Path from types import ModuleType @@ -119,7 +118,7 @@ def glob_relative( return expanded_values -def read_files(filepaths: Union[str, bytes, Iterable[StrPath]], root_dir=None) -> str: +def read_files(filepaths: Union[StrPath, Iterable[StrPath]], root_dir=None) -> str: """Return the content of the files concatenated using ``\n`` as str This function is sandboxed and won't reach anything outside ``root_dir`` @@ -339,18 +338,13 @@ def version(value: Union[Callable, Iterable[Union[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 03f6c0398ba..bc5ec5ddd5c 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -272,6 +272,7 @@ class Distribution(_Distribution): } _patched_dist = None + namespace_packages: List[str] def patch_missing_pkg_info(self, attrs): # Fake up a replacement for the data that would normally come from