Skip to content

Commit d91a7c3

Browse files
authored
PEP 722: Fix reference implementation (gh-3284)
1 parent 189134e commit d91a7c3

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

pep-0722.rst

+20-16
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Recommendations
199199
===============
200200

201201
This section is non-normative and simply describes "good practices" when using
202-
metadata blocks.
202+
dependency blocks.
203203

204204
While it is permitted for tools to do minimal validation of requirements, in
205205
practice they should do as much "sanity check" validation as possible, even if
@@ -234,23 +234,27 @@ reference implementation can be included here.
234234
def read_dependency_block(filename):
235235
# Use the tokenize module to handle any encoding declaration.
236236
with tokenize.open(filename) as f:
237+
# Skip lines until we reach a dependency block (OR EOF).
237238
for line in f:
238239
if re.match(DEPENDENCY_BLOCK_MARKER, line):
239-
for line in f:
240-
if not line.startswith("#"):
241-
break
242-
# Remove comments. An inline comment is introduced by
243-
# a hash, which must be preceded and followed by a
244-
# space. The initial hash will be skipped as it has
245-
# no space before it.
246-
line = line.split(" # ", maxsplit=1)[0]
247-
line = line[1:].strip()
248-
if not line:
249-
break
250-
# Try to convert to a requirement. This will raise
251-
# an error if the line is not a PEP 508 requirement
252-
yield Requirement(line)
253240
break
241+
# Read dependency lines until we hit a line that doesn't
242+
# start with #, or we are at EOF.
243+
for line in f:
244+
if not line.startswith("#"):
245+
break
246+
# Remove comments. An inline comment is introduced by
247+
# a hash, which must be preceded and followed by a
248+
# space.
249+
line = line[1:].split(" # ", maxsplit=1)[0]
250+
line = line.strip()
251+
# Ignore empty lines
252+
if not line:
253+
continue
254+
# Try to convert to a requirement. This will raise
255+
# an error if the line is not a PEP 508 requirement
256+
yield Requirement(line)
257+
254258
255259
A format similar to the one proposed here is already supported `in pipx
256260
<https://github.com/pypa/pipx/pull/916>`__ and in `pip-run
@@ -366,7 +370,7 @@ no real benefit.
366370

367371
So the question is essentially, "why not use TOML?"
368372

369-
The key idea behind the "metadata block" format is to define something that
373+
The key idea behind the "dependency block" format is to define something that
370374
reads naturally as a comment in the script. Dependency data is useful both for
371375
tools and for the human reader, so having a human readable format is beneficial.
372376
On the other hand, TOML of necessity has a syntax of its own, which distracts

0 commit comments

Comments
 (0)