79458925

Date: 2025-02-22 01:15:37
Score: 1
Natty:
Report link

I hit

400 Can't have direct dependency: hadolint-py@ git+https://github.com/AleksaC/hadolint-py.git ; extra ==
         "precommit". See https://packaging.python.org/specifications/core-metadata for more information.
           The server could not comply with the request since it is either malformed or otherwise incorrect.

when trying to move my requirements.in into the [project.optional-dependencies] section of pyproject.toml.

To resolve the issue, my first approach was to keep the git+https dependency in requirements.in, while moving everything else to pyproject.toml. A more elaborate fix I was able to develop was by the following to the [build-system] section of pyproject.toml:

build-backend = 'pypi_compatible_build'
backend-path = ['']

And adding this pypi_compatible_build.py file:

"""
Avoid git URLs breaking PyPI uploads.
Similar problem to:
https://stackoverflow.com/questions/54887301/how-can-i-use-git-repos-as-dependencies-for-my-pypi-package
"""

from io import StringIO
from typing import TextIO

import setuptools
from packaging.metadata import Metadata
from setuptools._core_metadata import _write_requirements  # type: ignore[import-not-found]
from setuptools.build_meta import *  # noqa: F403


def write_pypi_compatible_requirements(self: Metadata, final_file: TextIO) -> None:
    """Mark requirements with URLs as external."""
    initial_file = StringIO()
    _write_requirements(self, initial_file)
    initial_file.seek(0)
    for initial_line in initial_file:
        final_line = initial_line
        metadata = Metadata.from_email(initial_line, validate=False)
        if metadata.requires_dist and metadata.requires_dist[0].url:
            final_line = initial_line.replace('Requires-Dist:', 'Requires-External:')
        final_file.write(final_line)


setuptools._core_metadata._write_requirements = write_pypi_compatible_requirements

References:

Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: cov