From 786e51a75053a0bd17336f50606cdf52eeb4a4d2 Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 20 Aug 2024 13:11:55 -0400 Subject: [PATCH] Allows using `dict` as an ordered type in `setuptools.dist.check_requirements` --- newsfragments/4575.feature.rst | 1 + setuptools/command/_requirestxt.py | 3 +-- setuptools/command/egg_info.py | 5 +---- setuptools/dist.py | 11 ++++++++--- setuptools/tests/test_core_metadata.py | 1 - setuptools/tests/test_dist.py | 10 ++-------- 6 files changed, 13 insertions(+), 18 deletions(-) create mode 100644 newsfragments/4575.feature.rst diff --git a/newsfragments/4575.feature.rst b/newsfragments/4575.feature.rst new file mode 100644 index 00000000000..64ab49830f0 --- /dev/null +++ b/newsfragments/4575.feature.rst @@ -0,0 +1 @@ +Allowed using `dict` as an ordered type in ``setuptools.dist.check_requirements`` -- by :user:`Avasam` diff --git a/setuptools/command/_requirestxt.py b/setuptools/command/_requirestxt.py index b87476d6f4e..d426f5dffb0 100644 --- a/setuptools/command/_requirestxt.py +++ b/setuptools/command/_requirestxt.py @@ -18,12 +18,11 @@ from packaging.requirements import Requirement from .. import _reqs +from .._reqs import _StrOrIter # dict can work as an ordered set _T = TypeVar("_T") _Ordered = Dict[_T, None] -_ordered = dict -_StrOrIter = _reqs._StrOrIter def _prepare( diff --git a/setuptools/command/egg_info.py b/setuptools/command/egg_info.py index 09255a32400..35b155a2329 100644 --- a/setuptools/command/egg_info.py +++ b/setuptools/command/egg_info.py @@ -2,7 +2,6 @@ Create a distribution's .egg-info directory and contents""" -import collections import functools import os import re @@ -211,11 +210,9 @@ def save_version_info(self, filename): build tag. Install build keys in a deterministic order to avoid arbitrary reordering on subsequent builds. """ - egg_info = collections.OrderedDict() # follow the order these keys would have been added # when PYTHONHASHSEED=0 - egg_info['tag_build'] = self.tags() - egg_info['tag_date'] = 0 + egg_info = dict(tag_build=self.tags(), tag_date=0) edit_config(filename, dict(egg_info=egg_info)) def finalize_options(self): diff --git a/setuptools/dist.py b/setuptools/dist.py index 68f877decd0..f03d47da0a4 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -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, MutableMapping, NoReturn, overload from more_itertools import partition, unique_everseen from packaging.markers import InvalidMarker, Marker @@ -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 @@ -138,11 +139,15 @@ 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) -> 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)): + if isinstance(value, set): raise TypeError("Unordered types are not allowed") except (TypeError, ValueError) as error: tmpl = ( diff --git a/setuptools/tests/test_core_metadata.py b/setuptools/tests/test_core_metadata.py index 34828ac750d..51d4a10810f 100644 --- a/setuptools/tests/test_core_metadata.py +++ b/setuptools/tests/test_core_metadata.py @@ -310,7 +310,6 @@ def test_parity_with_metadata_from_pypa_wheel(tmp_path): python_requires=">=3.8", install_requires=""" packaging==23.2 - ordered-set==3.1.1 more-itertools==8.8.0; extra == "other" jaraco.text==3.7.0 importlib-resources==5.10.2; python_version<"3.8" diff --git a/setuptools/tests/test_dist.py b/setuptools/tests/test_dist.py index 597785b519e..6c86bebbb71 100644 --- a/setuptools/tests/test_dist.py +++ b/setuptools/tests/test_dist.py @@ -1,4 +1,3 @@ -import collections import os import re import urllib.parse @@ -72,15 +71,10 @@ def sdist_with_index(distname, version): def test_provides_extras_deterministic_order(): - extras = collections.OrderedDict() - extras['a'] = ['foo'] - extras['b'] = ['bar'] - attrs = dict(extras_require=extras) + attrs = dict(extras_require=dict(a=['foo'], b=['bar'])) dist = Distribution(attrs) assert list(dist.metadata.provides_extras) == ['a', 'b'] - attrs['extras_require'] = collections.OrderedDict( - reversed(list(attrs['extras_require'].items())) - ) + attrs['extras_require'] = dict(reversed(attrs['extras_require'].items())) dist = Distribution(attrs) assert list(dist.metadata.provides_extras) == ['b', 'a']