Repository navigation
[SPARK-60144][PYTHON] Add type annotations to LooseVersion comparisons - #59345
Open
bearomorphism wants to merge 3 commits into
Open
bearomorphism wants to merge 3 commits into
bearomorphism wants to merge 3 commits into
Conversation
bearomorphism
marked this pull request as ready for review
October 11, 2026 04:27
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Annotate
LooseVersioncomparison methods and_cmp, removing sixno-untyped-defsuppressions. Keep the existingOptionalstyle and useUnionto minimize the diff. Onlypython/pyspark/loose_version.pychanges.Why are the changes needed?
Comparisons currently infer as
Anyrather thanbool. The annotations also let mypy reject unsupported ordering operands.Two implementation adjustments are needed for the annotations to pass mypy:
return NotImplementedinstead ofreturn c: mypy 1.19.1 does not narrowcafterc is NotImplemented. It still seesUnion[int, NotImplementedType], which cannot be returned asbool. Returning the sentinel directly avoids this limitation; typeshed permits it for boolean-returning comparison methods.return 1: mypy cannot prove the==,<, and>checks exhaustive. Keeping the finalifproduces a missing-return error because the function could implicitly returnNone. 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?
python/pyspark/tests/typingpassed.python -m mypy --config-file python/mypy.ini --follow-imports=silent python/pyspark/loose_version.pygit diff --checkpassed.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