Skip to content

Commit

Permalink
Warn deprecated metadata fields in setup.cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Jan 20, 2025
1 parent 7792fac commit 6b6736c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 26 deletions.
29 changes: 25 additions & 4 deletions setuptools/config/setupcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from .. import _static
from .._path import StrPath
from ..errors import FileError, OptionError
from ..errors import FileError, OptionError, RemovedConfigError
from ..warnings import SetuptoolsDeprecationWarning
from . import expand

Expand Down Expand Up @@ -516,6 +516,25 @@ def config_handler(*args, **kwargs):

return config_handler

def _deprecated(self, field, func):
anchor = f"keyword-{field.replace('_', '-')}"
return self._deprecated_config_handler(
func,
f"Deprecated usage of `{field}` in `setup.cfg`.",
see_docs=f"references/keywords.html#{anchor}",
due_date=(2027, 1, 25), # introduced in 20 Jan 2025
)

def _removed(self, field, **kwargs):
def config_handler(*args, **kwargs):
raise RemovedConfigError(
f"Invalid use of `{field}` in `setup.cfg`.\nSee: "
"https://setuptools.pypa.io/en/latest/"
f"references/keywords.html#keyword-{field.replace('_', '-')}"
)

return config_handler


class ConfigMetadataHandler(ConfigHandler["DistributionMetadata"]):
section_prefix = 'metadata'
Expand Down Expand Up @@ -561,16 +580,18 @@ def parsers(self):
'maintainer_email': _static.Str,
'platforms': parse_list_static,
'keywords': parse_list_static,
'provides': parse_list_static,
'obsoletes': parse_list_static,
'provides': self._deprecated('provides', parse_list_static),
'obsoletes': self._deprecated('obsoletes', parse_list_static),
'requires': self._removed('requires'), # 2023-Nov-20
'classifiers': self._get_parser_compound(parse_file, parse_list_static),
'license': exclude_files_parser('license'),
'license_files': parse_list_static,
'description': parse_file,
'long_description': parse_file,
'long_description_content_type': _static.Str,
'version': self._parse_version, # Cannot be marked as dynamic
'url': _static.Str,
'url': self._deprecated('url', _static.Str),
'download_url': self._deprecated('download_url', _static.Str),
'project_urls': parse_dict_static,
}

Expand Down
80 changes: 58 additions & 22 deletions setuptools/tests/config/test_setupcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from setuptools.config.setupcfg import ConfigHandler, Target, read_configuration
from setuptools.dist import Distribution, _Distribution
from setuptools.errors import RemovedConfigError
from setuptools.warnings import SetuptoolsDeprecationWarning

from ..textwrap import DALS
Expand Down Expand Up @@ -136,19 +137,20 @@ def test_basic(self, tmpdir):
'license': 'BSD 3-Clause License',
}

with get_dist(tmpdir, meta_initial) as dist:
metadata = dist.metadata
with pytest.warns(SetuptoolsDeprecationWarning, match="Deprecated config"):
with get_dist(tmpdir, meta_initial) as dist:
metadata = dist.metadata

assert metadata.version == '10.1.1'
assert metadata.description == 'Some description'
assert metadata.long_description_content_type == 'text/something'
assert metadata.long_description == 'readme contents\nline2'
assert metadata.provides == ['package', 'package.sub']
assert metadata.license == 'BSD 3-Clause License'
assert metadata.name == 'fake_name'
assert metadata.keywords == ['one', 'two']
assert metadata.download_url == 'http://test.test.com/test/'
assert metadata.maintainer_email == '[email protected]'
assert metadata.version == '10.1.1'
assert metadata.description == 'Some description'
assert metadata.long_description_content_type == 'text/something'
assert metadata.long_description == 'readme contents\nline2'
assert metadata.provides == ['package', 'package.sub']
assert metadata.license == 'BSD 3-Clause License'
assert metadata.name == 'fake_name'
assert metadata.keywords == ['one', 'two']
assert metadata.download_url == 'http://test.test.com/test/'
assert metadata.maintainer_email == '[email protected]'

def test_license_cfg(self, tmpdir):
fake_env(
Expand Down Expand Up @@ -207,16 +209,17 @@ def test_aliases(self, tmpdir):
' Programming Language :: Python :: 3.5\n',
)

with get_dist(tmpdir) as dist:
metadata = dist.metadata
assert metadata.author_email == '[email protected]'
assert metadata.url == 'http://test.test.com/test/'
assert metadata.description == 'Short summary'
assert metadata.platforms == ['a', 'b']
assert metadata.classifiers == [
'Framework :: Django',
'Programming Language :: Python :: 3.5',
]
with pytest.warns(match='Deprecated usage of .url.'):
with get_dist(tmpdir) as dist:
metadata = dist.metadata
assert metadata.author_email == '[email protected]'
assert metadata.url == 'http://test.test.com/test/'
assert metadata.description == 'Short summary'
assert metadata.platforms == ['a', 'b']
assert metadata.classifiers == [
'Framework :: Django',
'Programming Language :: Python :: 3.5',
]

def test_multiline(self, tmpdir):
fake_env(
Expand Down Expand Up @@ -449,6 +452,39 @@ def test_make_option_lowercase(self, tmpdir):
assert metadata.name == 'foo'
assert metadata.description == 'Some description'

@pytest.mark.parametrize(
("field", "value"),
[
("provides", "setuptools"),
("obsoletes", "setuptools"),
("url", "www.setuptools.com.br"),
("download_url", "www.setuptools.com.br/42"),
],
)
def test_deprecated(self, tmpdir, field, value):
fake_env(
tmpdir,
f'[metadata]\nname = foo\ndescription = Desc\n{field} = {value}',
)
match = f"Deprecated usage of `{field}`"
with pytest.warns(SetuptoolsDeprecationWarning, match=match):
get_dist(tmpdir).__enter__()

@pytest.mark.parametrize(
("field", "value"),
[
("requires", "setuptools"),
],
)
def test_removed(self, tmpdir, field, value):
fake_env(
tmpdir,
f'[metadata]\nname = foo\ndescription = Desc\n{field} = {value}',
)
match = f"Invalid use of `{field}`"
with pytest.raises(RemovedConfigError, match=match):
get_dist(tmpdir).__enter__()


class TestOptions:
def test_basic(self, tmpdir):
Expand Down
1 change: 1 addition & 0 deletions setuptools/tests/test_core_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ def dist(self, request, monkeypatch, tmp_path):
yield setupcfg.apply_configuration(Distribution({}), config)

@pytest.mark.uses_network
@pytest.mark.filterwarnings('ignore:Deprecated config')
def test_equivalent_output(self, tmp_path, dist):
"""Ensure output from setuptools is equivalent to the one from `pypa/wheel`"""
# Generate a METADATA file using pypa/wheel for comparison
Expand Down

0 comments on commit 6b6736c

Please sign in to comment.