Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle non-normalised specifiers in requirements #634

Merged
merged 3 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/packaging/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,12 @@ def _parse_specifier(tokenizer: Tokenizer) -> str:

def _parse_version_many(tokenizer: Tokenizer) -> str:
"""
version_many = (OP VERSION (COMMA OP VERSION)*)?
version_many = (SPECIFIER (WS? COMMA WS? SPECIFIER)*)?
"""
parsed_specifiers = ""
while tokenizer.check("OP"):
while tokenizer.check("SPECIFIER"):
parsed_specifiers += tokenizer.read().text

# We intentionally do not consume whitespace here, since the regular expression
# for `VERSION` uses a lookback for the operator, to determine what
# corresponding syntax is permitted.

version_token = tokenizer.expect("VERSION", expected="version after operator")
parsed_specifiers += version_token.text
tokenizer.consume("WS")

if not tokenizer.check("COMMA"):
break
parsed_specifiers += tokenizer.read().text
Expand Down
5 changes: 4 additions & 1 deletion src/packaging/_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ def __str__(self) -> str:
""",
re.VERBOSE,
),
"VERSION": re.compile(Specifier._version_regex_str, re.VERBOSE | re.IGNORECASE),
"SPECIFIER": re.compile(
Specifier._operator_regex_str + Specifier._version_regex_str,
re.VERBOSE | re.IGNORECASE,
),
"AT": r"\@",
"URL": r"[^ \t]+",
"IDENTIFIER": r"\b[a-zA-Z0-9][a-zA-Z0-9._-]*\b",
Expand Down
6 changes: 3 additions & 3 deletions src/packaging/specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class Specifier(BaseSpecifier):
|
(?: # pre release
[-_\.]?
(a|b|c|rc|alpha|beta|pre|preview)
(alpha|beta|preview|pre|a|b|c|rc)
[-_\.]?
[0-9]*
)?
Expand All @@ -163,7 +163,7 @@ class Specifier(BaseSpecifier):
[0-9]+(?:\.[0-9]+)+ # release (We have a + instead of a *)
(?: # pre release
[-_\.]?
(a|b|c|rc|alpha|beta|pre|preview)
(alpha|beta|preview|pre|a|b|c|rc)
[-_\.]?
[0-9]*
)?
Expand All @@ -188,7 +188,7 @@ class Specifier(BaseSpecifier):
[0-9]+(?:\.[0-9]+)* # release
(?: # pre release
[-_\.]?
(a|b|c|rc|alpha|beta|pre|preview)
(alpha|beta|preview|pre|a|b|c|rc)
[-_\.]?
[0-9]*
)?
Expand Down
5 changes: 3 additions & 2 deletions tests/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
(None, "({ws}==={ws}arbitrarystring{ws})"),
(None, "=={ws}1.0"),
(None, "({ws}=={ws}1.0{ws})"),
(None, "=={ws}1.0-alpha"),
(None, "<={ws}1!3.0.0.rc2"),
(None, ">{ws}2.2{ws},{ws}<{ws}3"),
(None, "(>{ws}2.2{ws},{ws}<{ws}3)"),
Expand Down Expand Up @@ -492,9 +493,9 @@ def test_error_on_missing_version_after_op(self) -> None:
# THEN
assert ctx.exconly() == (
"packaging.requirements.InvalidRequirement: "
"Expected version after operator\n"
"Expected end or semicolon (after name and no valid version specifier)\n"
" name==\n"
" ^"
" ^"
)

def test_error_on_missing_op_after_name(self) -> None:
Expand Down