PEP 722: Fix reference implementation (gh-3284)

This commit is contained in:
Paul Moore 2023-08-08 23:28:16 +01:00 committed by GitHub
parent 189134e403
commit d91a7c3af1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 16 deletions

View File

@ -199,7 +199,7 @@ Recommendations
===============
This section is non-normative and simply describes "good practices" when using
metadata blocks.
dependency blocks.
While it is permitted for tools to do minimal validation of requirements, in
practice they should do as much "sanity check" validation as possible, even if
@ -234,23 +234,27 @@ reference implementation can be included here.
def read_dependency_block(filename):
# Use the tokenize module to handle any encoding declaration.
with tokenize.open(filename) as f:
# Skip lines until we reach a dependency block (OR EOF).
for line in f:
if re.match(DEPENDENCY_BLOCK_MARKER, line):
for line in f:
if not line.startswith("#"):
break
# Remove comments. An inline comment is introduced by
# a hash, which must be preceded and followed by a
# space. The initial hash will be skipped as it has
# no space before it.
line = line.split(" # ", maxsplit=1)[0]
line = line[1:].strip()
if not line:
break
# Try to convert to a requirement. This will raise
# an error if the line is not a PEP 508 requirement
yield Requirement(line)
break
# Read dependency lines until we hit a line that doesn't
# start with #, or we are at EOF.
for line in f:
if not line.startswith("#"):
break
# Remove comments. An inline comment is introduced by
# a hash, which must be preceded and followed by a
# space.
line = line[1:].split(" # ", maxsplit=1)[0]
line = line.strip()
# Ignore empty lines
if not line:
continue
# Try to convert to a requirement. This will raise
# an error if the line is not a PEP 508 requirement
yield Requirement(line)
A format similar to the one proposed here is already supported `in pipx
<https://github.com/pypa/pipx/pull/916>`__ and in `pip-run
@ -366,7 +370,7 @@ no real benefit.
So the question is essentially, "why not use TOML?"
The key idea behind the "metadata block" format is to define something that
The key idea behind the "dependency block" format is to define something that
reads naturally as a comment in the script. Dependency data is useful both for
tools and for the human reader, so having a human readable format is beneficial.
On the other hand, TOML of necessity has a syntax of its own, which distracts