Skip to content

Commit

Permalink
Type sequence checks in setuptools/dist.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Aug 20, 2024
1 parent 477f713 commit d7db4a0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
1 change: 1 addition & 0 deletions newsfragments/4578.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a `TypeError` when a ``Distribution``'s old included attribute was a `tuple` -- by :user:`Avasam`
1 change: 1 addition & 0 deletions newsfragments/4578.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Made errors when parsing ``Distribution`` data more explicit about the expected type (``tuple[str, ...] | list[str]``) -- by :user:`Avasam`
48 changes: 34 additions & 14 deletions setuptools/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sys
from glob import iglob
from pathlib import Path
from typing import TYPE_CHECKING, MutableMapping
from typing import TYPE_CHECKING, List, MutableMapping, NoReturn, Tuple, Union, overload

from more_itertools import partition, unique_everseen
from packaging.markers import InvalidMarker, Marker
Expand All @@ -21,6 +21,7 @@
command as _, # noqa: F401 # imported for side-effects
)
from ._importlib import metadata
from ._reqs import _StrOrIter
from .config import pyprojecttoml, setupcfg
from .discovery import ConfigDiscovery
from .monkey import get_unpatched
Expand All @@ -36,9 +37,22 @@
from distutils.fancy_getopt import translate_longopt
from distutils.util import strtobool

if TYPE_CHECKING:
from typing_extensions import TypeAlias

__all__ = ['Distribution']

sequence = tuple, list
"""
Supported iterable types that are known to be:
- ordered (which `set` isn't)
- not match a str (which `Sequence[str]` does)
- not imply a nested type (like `dict`)
for use with `isinstance`.
"""
_Sequence: TypeAlias = Union[Tuple[str, ...], List[str]]
# This is how stringifying _Sequence would look in Python 3.10
_requence_type_repr = "tuple[str, ...] | list[str]"


def check_importable(dist, attr, value):
Expand All @@ -51,7 +65,7 @@ def check_importable(dist, attr, value):
) from e


def assert_string_list(dist, attr, value):
def assert_string_list(dist, attr: str, value: _Sequence) -> None:
"""Verify that value is a string list"""
try:
# verify that value is a list or tuple to exclude unordered
Expand All @@ -61,7 +75,7 @@ def assert_string_list(dist, attr, value):
assert ''.join(value) != value
except (TypeError, ValueError, AttributeError, AssertionError) as e:
raise DistutilsSetupError(
"%r must be a list of strings (got %r)" % (attr, value)
f"{attr!r} must be of type <{_requence_type_repr}> (got {value!r})"
) from e


Expand Down Expand Up @@ -138,18 +152,22 @@ def invalid_unless_false(dist, attr, value):
raise DistutilsSetupError(f"{attr} is invalid.")


def check_requirements(dist, attr, value):
@overload
def check_requirements(dist, attr: str, value: set | dict) -> NoReturn: ...
@overload
def check_requirements(dist, attr: str, value: _StrOrIter) -> None: ...
def check_requirements(dist, attr: str, value: _StrOrIter) -> None:
"""Verify that install_requires is a valid requirements list"""
try:
list(_reqs.parse(value))
if isinstance(value, (dict, set)):
raise TypeError("Unordered types are not allowed")
except (TypeError, ValueError) as error:
tmpl = (
"{attr!r} must be a string or list of strings "
"containing valid project/version requirement specifiers; {error}"
f"{attr!r} must be a string or iterable of strings "
f"containing valid project/version requirement specifiers; {error}"
)
raise DistutilsSetupError(tmpl.format(attr=attr, error=error)) from error
raise DistutilsSetupError(tmpl) from error


def check_specifier(dist, attr, value):
Expand Down Expand Up @@ -765,11 +783,11 @@ def has_contents_for(self, package):

return False

def _exclude_misc(self, name, value):
def _exclude_misc(self, name: str, value: _Sequence) -> None:
"""Handle 'exclude()' for list/tuple attrs without a special handler"""
if not isinstance(value, sequence):
raise DistutilsSetupError(
"%s: setting must be a list or tuple (%r)" % (name, value)
f"{name}: setting must be of type <{_requence_type_repr}> (got {value!r})"
)
try:
old = getattr(self, name)
Expand All @@ -782,11 +800,13 @@ def _exclude_misc(self, name, value):
elif old:
setattr(self, name, [item for item in old if item not in value])

def _include_misc(self, name, value):
def _include_misc(self, name: str, value: _Sequence) -> None:
"""Handle 'include()' for list/tuple attrs without a special handler"""

if not isinstance(value, sequence):
raise DistutilsSetupError("%s: setting must be a list (%r)" % (name, value))
raise DistutilsSetupError(
f"{name}: setting must be of type <{_requence_type_repr}> (got {value!r})"
)
try:
old = getattr(self, name)
except AttributeError as e:
Expand All @@ -799,7 +819,7 @@ def _include_misc(self, name, value):
)
else:
new = [item for item in value if item not in old]
setattr(self, name, old + new)
setattr(self, name, list(old) + new)

def exclude(self, **attrs):
"""Remove items from distribution that are named in keyword arguments
Expand All @@ -824,10 +844,10 @@ def exclude(self, **attrs):
else:
self._exclude_misc(k, v)

def _exclude_packages(self, packages):
def _exclude_packages(self, packages: _Sequence) -> None:
if not isinstance(packages, sequence):
raise DistutilsSetupError(
"packages: setting must be a list or tuple (%r)" % (packages,)
f"packages: setting must be of type <{_requence_type_repr}> (got {packages!r})"
)
list(map(self.exclude_package, packages))

Expand Down
8 changes: 4 additions & 4 deletions setuptools/tests/test_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def test_provides_extras_deterministic_order():
'hello': '*.msg',
},
(
"\"values of 'package_data' dict\" "
"must be a list of strings (got '*.msg')"
"\"values of 'package_data' dict\" must be of type <tuple[str, ...] | list[str]>"
" (got '*.msg')"
),
),
# Invalid value type (generators are single use)
Expand All @@ -128,8 +128,8 @@ def test_provides_extras_deterministic_order():
'hello': (x for x in "generator"),
},
(
"\"values of 'package_data' dict\" must be a list of strings "
"(got <generator object"
"\"values of 'package_data' dict\" must be of type <tuple[str, ...] | list[str]>"
" (got <generator object"
),
),
)
Expand Down

0 comments on commit d7db4a0

Please sign in to comment.