flake8-requirements
flake8-requirements copied to clipboard
Use build.util.project_wheel_metadata to get the package dependencies/requirements
It looks like there is finally a standard supported way to get the package dependencies/requirements via PEP517.
https://github.com/pypa/build/issues/181
From the end of the discussion on that issue, something like this should work:
import tempfile
from importlib.metadata import PathDistribution
from pathlib import Path
import pep517.wrappers
from build import ProjectBuilder
from packaging.requirements import Requirement
# replace "." with the package directory
builder = ProjectBuilder(".", runner=pep517.wrappers.quiet_subprocess_runner)
with tempfile.TemporaryDirectory() as tmpdir:
distribution = PathDistribution(Path(builder.metadata_path(tmpdir)))
requires = [Requirement(i) for i in distribution.requires]
for x in requires:
print(type(x))
print(x)
This should work for any package/project that uses a PEP517 compatible build backend, eg. setuptools, flit, poetry, whey, etc. I've only tested it myself on https://github.com/pypa/sampleproject which uses setuptools.
EDIT: https://pypa-build.readthedocs.io/en/latest/api.html#build.util.project_wheel_metadata is now available and should be sufficient for this feature.
@wwuck What are your thoughts on shipping this as its own PyPI package, to:
a) make it easily available accessible b) provide a stable home until such time as an easily invokable access method is provided by build? c) discourage copy/pasta/variations/inconsistencies over time d) as a sandbox/experimental area to validate, prove out and provide examples for adding additional conveniences / Quality of Life stuff to build or other PyCA projects
It looks like there is finally a standard supported way to get the package dependencies/requirements via PEP517.
What are your thoughts on shipping this as its own PyPI package, to
The same objections are on my side. It does not look like a standard way of getting package dependencies. It's more like "it can be done". Anyway, nice work. So, I think that I will wait until there is an actual API for getting dependencies in build or pep517 packages. From my side I do not care where it will be, but I do care about potential "API" breakages.
I'm somewhat in agreement @Arkq, except its hard to know if a "standard API" will ever appear given previous comments from some of the PyPA people. In any case, I'm guessing it would just be some sort of wrapper around the example from https://github.com/pypa/build/issues/181.
At least that bug has now been re-opened so there is hope that something more "standard" may appear sooner rather than later.
@Arkq It’s finally arrived?
https://github.com/pypa/build/issues/181#issuecomment-921789130