Skip to content

Commit

Permalink
Remove calls to typing.cast with better type narrowing and definiti…
Browse files Browse the repository at this point in the history
…ons (#4375)
  • Loading branch information
Avasam authored Jun 17, 2024
1 parent 8ac08a0 commit 2299319
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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)(
Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/editable_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
}

Expand Down
17 changes: 9 additions & 8 deletions setuptools/config/_apply_pyprojecttoml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,8 +21,9 @@
TYPE_CHECKING,
Any,
Callable,
Dict,
Mapping,
Union,
cast,
)
from .._path import StrPath
from ..errors import RemovedConfigError
Expand All @@ -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]

Expand Down Expand Up @@ -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"]

Expand All @@ -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):
Expand Down
19 changes: 6 additions & 13 deletions setuptools/config/expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
Iterator,
Mapping,
TypeVar,
Union,
cast,
)
from pathlib import Path
from types import ModuleType
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions setuptools/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2299319

Please sign in to comment.