Skip to content

Commit 2299319

Browse files
authored
Remove calls to typing.cast with better type narrowing and definitions (#4375)
1 parent 8ac08a0 commit 2299319

File tree

5 files changed

+19
-22
lines changed

5 files changed

+19
-22
lines changed

mypy.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
python_version = 3.8
55
strict = False
66
warn_unused_ignores = True
7+
warn_redundant_casts = True
78
# required to support namespace packages: https://github.com/python/mypy/issues/14057
89
explicit_package_bases = True
910
exclude = (?x)(

setuptools/command/editable_wheel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ def template_vars(self) -> tuple[str, str, dict[str, str], dict[str, list[str]]]
515515
)
516516

517517
legacy_namespaces = {
518-
cast(str, pkg): find_package_path(pkg, roots, self.dist.src_root or "")
518+
pkg: find_package_path(pkg, roots, self.dist.src_root or "")
519519
for pkg in self.dist.namespace_packages or []
520520
}
521521

setuptools/config/_apply_pyprojecttoml.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import logging
1414
import os
15-
from collections.abc import Mapping
1615
from email.headerregistry import Address
1716
from functools import partial, reduce
1817
from inspect import cleandoc
@@ -22,8 +21,9 @@
2221
TYPE_CHECKING,
2322
Any,
2423
Callable,
24+
Dict,
25+
Mapping,
2526
Union,
26-
cast,
2727
)
2828
from .._path import StrPath
2929
from ..errors import RemovedConfigError
@@ -35,7 +35,7 @@
3535
from setuptools.dist import Distribution # noqa
3636

3737
EMPTY: Mapping = MappingProxyType({}) # Immutable dict-like
38-
_DictOrStr = Union[dict, str]
38+
_ProjectReadmeValue = Union[str, Dict[str, str]]
3939
_CorrespFn = Callable[["Distribution", Any, StrPath], None]
4040
_Correspondence = Union[str, _CorrespFn]
4141

@@ -149,15 +149,16 @@ def _guess_content_type(file: str) -> str | None:
149149
raise ValueError(f"Undefined content type for {file}, {msg}")
150150

151151

152-
def _long_description(dist: Distribution, val: _DictOrStr, root_dir: StrPath):
152+
def _long_description(dist: Distribution, val: _ProjectReadmeValue, root_dir: StrPath):
153153
from setuptools.config import expand
154154

155+
file: str | tuple[()]
155156
if isinstance(val, str):
156-
file: str | list = val
157+
file = val
157158
text = expand.read_files(file, root_dir)
158-
ctype = _guess_content_type(val)
159+
ctype = _guess_content_type(file)
159160
else:
160-
file = val.get("file") or []
161+
file = val.get("file") or ()
161162
text = val.get("text") or expand.read_files(file, root_dir)
162163
ctype = val["content-type"]
163164

@@ -167,7 +168,7 @@ def _long_description(dist: Distribution, val: _DictOrStr, root_dir: StrPath):
167168
_set_config(dist, "long_description_content_type", ctype)
168169

169170
if file:
170-
dist._referenced_files.add(cast(str, file))
171+
dist._referenced_files.add(file)
171172

172173

173174
def _license(dist: Distribution, val: dict, root_dir: StrPath):

setuptools/config/expand.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
Iterator,
3737
Mapping,
3838
TypeVar,
39-
Union,
40-
cast,
4139
)
4240
from pathlib import Path
4341
from types import ModuleType
@@ -326,18 +324,13 @@ def version(value: Callable | Iterable[str | int] | str) -> str:
326324
"""When getting the version directly from an attribute,
327325
it should be normalised to string.
328326
"""
329-
if callable(value):
330-
value = value()
327+
_value = value() if callable(value) else value
331328

332-
value = cast(Iterable[Union[str, int]], value)
333-
334-
if not isinstance(value, str):
335-
if hasattr(value, '__iter__'):
336-
value = '.'.join(map(str, value))
337-
else:
338-
value = '%s' % value
339-
340-
return value
329+
if isinstance(_value, str):
330+
return _value
331+
if hasattr(_value, '__iter__'):
332+
return '.'.join(map(str, _value))
333+
return '%s' % _value
341334

342335

343336
def canonic_package_data(package_data: dict) -> dict:

setuptools/dist.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ class Distribution(_Distribution):
272272
}
273273

274274
_patched_dist = None
275+
# Used by build_py, editable_wheel and install_lib commands for legacy namespaces
276+
namespace_packages: list[str] #: :meta private: DEPRECATED
275277

276278
def patch_missing_pkg_info(self, attrs):
277279
# Fake up a replacement for the data that would normally come from

0 commit comments

Comments
 (0)