Skip to content

Commit

Permalink
fix: Gracefully handle invalid python_requires when finding minversion (
Browse files Browse the repository at this point in the history
#37317)

python_requires must follow the specification defined in:
        https://packaging.python.org/en/latest/specifications/version-specifiers/#id5

    In the event that a package version specifies a `python_requires`
    that does not match that spec, execution terminates with
    an InvalidSpecifier exception.

    This commit makes it so that we catch and log the exception,
    ignoring the version that has the invalid python version specifier.

    An alternative solution could have been to attempt to correct
    the version specifier (e.g. '>= 3.5.*' is invalid, but it'd be
    trivial to correct it to '>=3.5'). But that would potentially invite
    a complexity that costs more than it's worth.
  • Loading branch information
kdestin authored Sep 12, 2024
1 parent 0b71075 commit a31e507
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions tools/azure-sdk-tools/pypi_tools/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ def project_release(self, package_name, version):

def filter_packages_for_compatibility(self, package_name, version_set):
# only need the packaging.specifiers import if we're actually executing this filter.
from packaging.specifiers import SpecifierSet
from packaging.specifiers import InvalidSpecifier, SpecifierSet

results = []

for version in version_set:
requires_python = self.project_release(package_name, version)["info"]["requires_python"]
if requires_python:
if Version(".".join(map(str, sys.version_info[:3]))) in SpecifierSet(requires_python):
results.append(version)
try:
if Version(".".join(map(str, sys.version_info[:3]))) in SpecifierSet(requires_python):
results.append(version)
except InvalidSpecifier:
logging.warn(f"Invalid python_requires {requires_python!r} for package {package_name}=={version}")
continue
else:
results.append(version)

Expand Down

0 comments on commit a31e507

Please sign in to comment.