Skip to content

[SPARK-60144][PYTHON] Add type annotations to LooseVersion comparisons - #59345

Open
bearomorphism wants to merge 3 commits into
apache:masterfrom
bearomorphism:fix-loose-version-typing
Open

bearomorphism wants to merge 3 commits into
apache:masterfrom
bearomorphism:fix-loose-version-typing

Conversation

@bearomorphism

@bearomorphism bearomorphism commented Oct 11, 2026 •

Copy link
Copy Markdown

What changes were proposed in this pull request?

Annotate LooseVersion comparison methods and _cmp, removing six no-untyped-def suppressions. Keep the existing Optional style and use Union to minimize the diff. Only python/pyspark/loose_version.py changes.

Why are the changes needed?

Comparisons currently infer as Any rather than bool. The annotations also let mypy reject unsupported ordering operands.

Two implementation adjustments are needed for the annotations to pass mypy:

  • return NotImplemented instead of return c: mypy 1.19.1 does not narrow c after c is NotImplemented. It still sees Union[int, NotImplementedType], which cannot be returned as bool. Returning the sentinel directly avoids this limitation; typeshed permits it for boolean-returning comparison methods.
  • Unconditional final return 1: mypy cannot prove the ==, <, and > checks exhaustive. Keeping the final if produces a missing-return error because the function could implicitly return None. After equality and less-than are ruled out, greater-than is the remaining case for comparable parsed components.

An upstream refresh does not solve this: CPython removed distutils in 3.12, and both the 3.11 source and setuptools copy still leave these methods untyped.

Does this PR introduce any user-facing change?

Static comparisons infer as bool, and unsupported ordering operands are rejected. No runtime behavior change is intended for parsed version strings.

How was this patch tested?

  • All 6 existing core typing cases in python/pyspark/tests/typing passed.
  • Targeted mypy 1.19.1 check passed:
    python -m mypy --config-file python/mypy.ini --follow-imports=silent python/pyspark/loose_version.py
  • Ruff lint/format and git diff --check passed.

No test files are changed. No full Spark build or JVM-backed tests were run.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: pi 1.1.0

@bearomorphism bearomorphism changed the title [PYTHON] Add type annotations to LooseVersion comparisons [SPARK-60144][PYTHON] Add type annotations to LooseVersion comparisons Oct 11, 2026
@bearomorphism
bearomorphism marked this pull request as ready for review October 11, 2026 04:27

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant