Skip to content

Commit

Permalink
Add Requires-Dist to PKG-INFO
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Aug 23, 2023
1 parent ed0757e commit 807ab8a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
55 changes: 52 additions & 3 deletions setuptools/_core_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

from distutils.util import rfc822_escape

from . import _normalization
from ._deprecation_warning import SetuptoolsDeprecationWarning
from .extern.packaging.markers import Marker
from .extern.packaging.requirements import Requirement
from .extern.packaging.version import Version
from .warnings import SetuptoolsDeprecationWarning

Expand Down Expand Up @@ -175,14 +179,59 @@ def write_field(key, value):
# PEP 566
if self.long_description_content_type:
write_field('Description-Content-Type', self.long_description_content_type)
if self.provides_extras:
for extra in self.provides_extras:
write_field('Provides-Extra', extra)

self._write_list(file, 'License-File', self.license_files or [])
_write_requirements(self, file)

long_description = self.get_long_description()
if long_description:
file.write("\n%s" % long_description)
if not long_description.endswith("\n"):
file.write("\n")


def _write_requirements(self, file):
for req in self.install_requires:
file.write(f"Requires-Dist: {req}\n")

processed_extras = {}
for augmented_extra, reqs in self.extras_require.items():
# Historically, setuptools allows "augmented extras": `<extra>:<condition>`
unsafe_extra, _, condition = augmented_extra.partition(":")
unsafe_extra = unsafe_extra.strip()
extra = _normalization.safe_extra(unsafe_extra)

if extra:
_write_provides_extra(file, processed_extras, extra, unsafe_extra)
for req in reqs:
r = _include_extra(req, extra, condition.strip())
file.write(f"Requires-Dist: {r}\n")

return processed_extras


def _include_extra(req: str, extra: str, condition: str) -> Requirement:
r = Requirement(req)
parts = (
f"({r.marker})" if r.marker else None,
f"({condition})" if condition else None,
f"extra == {extra!r}" if extra else None,
)
r.marker = Marker(" and ".join(x for x in parts if x))
return r


def _write_provides_extra(file, processed_extras, safe, unsafe):
previous = processed_extras.get(safe)
if previous == unsafe:
msg = f"""Ambiguity during "extra" normalization for dependencies.\n\n
********************************************************************
{previous!r} and {unsafe!r} normalize to the same value:\n
{safe!r}\n
In future versions, setuptools might halt the build process.
********************************************************************\n\n
"""
warnings.warn(msg, SetuptoolsDeprecationWarning, stacklevel=3)
else:
processed_extras[safe] = unsafe
file.write(f"Provides-Extra: {safe}\n")
4 changes: 4 additions & 0 deletions setuptools/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ class Distribution(_Distribution):
'provides_extras': OrderedSet,
'license_file': lambda: None,
'license_files': lambda: None,
'install_requires': list,
'extras_require': dict,
}

_patched_dist = None
Expand Down Expand Up @@ -389,6 +391,8 @@ def _finalize_requires(self):
self.metadata.python_requires = self.python_requires

self._normalize_requires()
self.metadata.install_requires = self.install_requires
self.metadata.extras_require = self.extras_require

if self.extras_require:
for extra in self.extras_require.keys():
Expand Down

0 comments on commit 807ab8a

Please sign in to comment.