Skip to content

Commit 5a82762

Browse files
committed
Fix is_valid_version() for prerelease versions
By default, the check if an empty specifier contains a prerelease version returns `false`. This made the `is_valid_version()` check for prerelease versions fail, when there is also a requirement with a non-empty specifier. In this case, the previous logic detected a conflict of the prerelease version and the empty specifier. Fix this by explicitly allowing prerelease versions when checking if a specifier contains a version. Note that the function that collects candidates for a dependency still ignores prerelease versions if any release versions are available, as required by the specs [1]. [1]: https://packaging.python.org/en/latest/specifications/version-specifiers/#handling-of-pre-releases Signed-off-by: Martin Nonnenmacher <[email protected]>
1 parent 51ee252 commit 5a82762

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/python_inspector/resolution.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ def is_valid_version(
172172
"""
173173
if parsed_version in bad_versions:
174174
return False
175-
if any(parsed_version not in r.specifier for r in requirements[identifier]):
175+
if any(
176+
not r.specifier.contains(parsed_version, prereleases=True) for r in requirements[identifier]
177+
):
176178
if all(not r.specifier for r in requirements[identifier]):
177179
return True
178180
return False

tests/test_resolution.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ def test_is_valid_version_with_no_specifier_and_pre_release():
196196
assert is_valid_version(parsed_version, requirements, identifier, bad_versions)
197197

198198

199+
def test_is_valid_version_with_empty_and_non_empty_specifier_and_pre_release():
200+
parsed_version = packvers.version.parse("1.0.0.dev1+01234abcd")
201+
requirements = {"flask": [Requirement("flask"), Requirement("flask==1.0.0.dev1+01234abcd")]}
202+
bad_versions = []
203+
identifier = "flask"
204+
assert is_valid_version(parsed_version, requirements, identifier, bad_versions)
205+
206+
199207
def test_get_requirements_from_dependencies():
200208
dependencies = [
201209
models.DependentPackage(

0 commit comments

Comments
 (0)