Skip to content

Commit

Permalink
Add warning for deprecated metadata fields
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Jan 20, 2025
1 parent fb7f3d3 commit 000b196
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
18 changes: 14 additions & 4 deletions docs/references/keywords.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,21 @@ extensions).
``url``
A string specifying the URL for the package homepage.

.. warning::
``url`` is deprecated as it generates the ``Home-page`` metadata.
Please use ``project_urls`` instead, with the relevant labels.
See :pep:`753`.

.. _keyword/download_url:

``download_url``
A string specifying the URL to download the package.

.. warning::
``download_url`` is deprecated as it generates the ``Download-URL`` metadata.
Please use ``project_urls`` instead, with the relevant labels.
See :pep:`753`.

.. _keyword/packages:

``packages``
Expand Down Expand Up @@ -257,14 +267,14 @@ extensions).

``requires``
.. warning::
``requires`` is superseded by ``install_requires`` and should not be used
anymore.
``requires`` is deprecated and superseded by ``install_requires``.
It should not be used anymore.

.. _keyword/obsoletes:

``obsoletes``
.. warning::
``obsoletes`` is currently ignored by ``pip``.
``obsoletes`` is deprecated and currently ignored by ``pip``.

List of strings describing packages which this package renders obsolete,
meaning that the two projects should not be installed at the same time.
Expand All @@ -283,7 +293,7 @@ extensions).

``provides``
.. warning::
``provides`` is currently ignored by ``pip``.
``provides`` is currently considered deprecated and is ignored by ``pip``.

List of strings describing package- and virtual package names contained
within this package.
Expand Down
23 changes: 23 additions & 0 deletions setuptools/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ def check_packages(dist, attr, value):
)


def _check_deprecated_metadata_field(
dist: Distribution, attr: str, value: object
) -> None:
if value:
SetuptoolsDeprecationWarning.emit(
f"Deprecated usage of `{attr}` in setuptools configuration.",
see_docs=f"references/keywords.html#keyword-{attr.replace('_', '-')}",
due_date=(2027, 1, 25),
# Warning initially introduced in 14 Jan 2025
)


if TYPE_CHECKING:
# Work around a mypy issue where type[T] can't be used as a base: https://github.com/python/mypy/issues/10962
from distutils.core import Distribution as _Distribution
Expand Down Expand Up @@ -294,6 +306,14 @@ class Distribution(_Distribution):
'extras_require': dict,
}

_DEPRECATED_FIELDS = (
"url",
"download_url",
"requires",
"provides",
"obsoletes",
)

# Used by build_py, editable_wheel and install_lib commands for legacy namespaces
namespace_packages: list[str] #: :meta private: DEPRECATED

Expand All @@ -318,6 +338,9 @@ def __init__(self, attrs: MutableMapping[str, Any] | None = None) -> None:
dist_attrs = {k: v for k, v in attrs.items() if k not in metadata_only}
_Distribution.__init__(self, dist_attrs)

for attr in self._DEPRECATED_FIELDS:
_check_deprecated_metadata_field(self, attr, dist_attrs.get(attr))

# Private API (setuptools-use only, not restricted to Distribution)
# Stores files that are referenced by the configuration and need to be in the
# sdist (e.g. `version = file: VERSION.txt`)
Expand Down
21 changes: 20 additions & 1 deletion setuptools/tests/test_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from setuptools import Distribution
from setuptools import Distribution, SetuptoolsDeprecationWarning
from setuptools.dist import check_package_data, check_specifier

from .test_easy_install import make_nspkg_sdist
Expand Down Expand Up @@ -276,3 +276,22 @@ def test_dist_default_name(tmp_path, dist_name, package_dir, package_files):
dist.set_defaults()
assert dist.py_modules or dist.packages
assert dist.get_name() == dist_name


@pytest.mark.parametrize(
("field", "value"),
[
("requires", ["setuptools"]),
("provides", ["setuptools"]),
("obsoletes", ["setuptools"]),
("url", "www.setuptools.com.br"),
("download_url", "www.setuptools.com.br/42"),
],
)
def test_deprecated_fields(tmpdir_cwd, field, value):
"""See discussion in https://github.com/pypa/setuptools/issues/4797"""
attrs = {"name": "test", "version": "0.42", field: value}
match = f"Deprecated usage of `{field}`"
with pytest.warns(SetuptoolsDeprecationWarning, match=match):
dist = Distribution(attrs)
assert getattr(dist.metadata, field, None) == value

0 comments on commit 000b196

Please sign in to comment.