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

Improve error for local version label with unsupported operators #675

Merged
merged 1 commit into from Feb 1, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/packaging/_parser.py
Expand Up @@ -233,6 +233,12 @@ def _parse_version_many(tokenizer: Tokenizer) -> str:
span_start=span_start,
span_end=tokenizer.position + 1,
)
if tokenizer.check("VERSION_LOCAL_LABEL_TRAIL", peek=True):
tokenizer.raise_syntax_error(
"Local version label can only be used with `==` or `!=` operators",
span_start=span_start,
span_end=tokenizer.position,
)
tokenizer.consume("WS")
if not tokenizer.check("COMMA"):
break
Expand Down
1 change: 1 addition & 0 deletions src/packaging/_tokenizer.py
Expand Up @@ -79,6 +79,7 @@ def __str__(self) -> str:
"URL": r"[^ \t]+",
"IDENTIFIER": r"\b[a-zA-Z0-9][a-zA-Z0-9._-]*\b",
"VERSION_PREFIX_TRAIL": r"\.\*",
"VERSION_LOCAL_LABEL_TRAIL": r"\+[a-z0-9]+(?:[-_\.][a-z0-9]+)*",
"WS": r"[ \t]+",
"END": r"$",
}
Expand Down
20 changes: 20 additions & 0 deletions tests/test_requirements.py
Expand Up @@ -301,6 +301,26 @@ def test_error_when_prefix_match_is_used_incorrectly(self) -> None:
" ~~~~~^"
)

@pytest.mark.parametrize("operator", [">=", "<=", ">", "<", "~="])
def test_error_when_local_version_label_is_used_incorrectly(
self, operator: str
) -> None:
# GIVEN
to_parse = f"name {operator} 1.0+local.version.label"
op_tilde = len(operator) * "~"

# WHEN
with pytest.raises(InvalidRequirement) as ctx:
Requirement(to_parse)

# THEN
assert ctx.exconly() == (
"packaging.requirements.InvalidRequirement: "
"Local version label can only be used with `==` or `!=` operators\n"
f" name {operator} 1.0+local.version.label\n"
f" {op_tilde}~~~~^"
)

def test_error_when_bracket_not_closed_correctly(self) -> None:
# GIVEN
to_parse = "name[bar, baz >= 1.0"
Expand Down