[SPARK-58346][PYTHON] Remove unnecessary type: ignore[union-attr] comments in PySpark#57521
Open
Spenserrrr wants to merge 1 commit into
Open
[SPARK-58346][PYTHON] Remove unnecessary type: ignore[union-attr] comments in PySpark#57521Spenserrrr wants to merge 1 commit into
Spenserrrr wants to merge 1 commit into
Conversation
…ments in PySpark ### What changes were proposed in this pull request? Removes 16 `# type: ignore[union-attr]` comments across 4 files in `python/pyspark` by making the implicit non-None assumption explicit at each access site: - `sql/types.py` (9): convert the atomic-type branches in `_parse_datatype_json_value` to the walrus form (`elif m := PATTERN.match(json_value):`), removing both the ignore and a redundant second regex match. - `errors/exceptions/captured.py` (5): add `assert SparkContext._jvm is not None` alongside the existing `assert SparkContext._gateway is not None`. - `sql/connect/client/core.py` (1): use the already-narrowed local `session` instead of re-reading `PySparkSession._instantiatedSession._jvm`. - `sql/catalog.py` (1): add `assert sc._gateway is not None`, completing the guard the code had already started with `assert sc is not None`. ### Why are the changes needed? These ignores suppressed mypy `[union-attr]` errors that arise only because a value is typed Optional at the access site, even though the surrounding code guarantees it is not None. Making the assumption explicit is clearer and matches idioms the codebase already uses nearby. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? `mypy` passes at full scope over `python/pyspark` (1287 source files). No behavior change; existing tests cover the affected code paths. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Opus 4.8)
Spenserrrr
marked this pull request as ready for review
July 25, 2026 01:25
Contributor
Author
|
Hi @gaogaotiantian! This is a PR on type: ignore. Please let me know if there is anything I need to change. |
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?
Removes 16
# type: ignore[union-attr]comments across 4 files inpython/pysparkby making the implicit non-None assumption explicit at each access site:sql/types.py(9): In_parse_datatype_json_value, each atomic-type branch matched its regex twice - once in theeliftest and again to bindm- then suppressed theOptional[re.Match]onm.group(...). Converting these to the walrus form (elif m := PATTERN.match(json_value):) matches once, narrowsmfor the branch body, and drops the ignore. This also removes the redundant second match and makes these branches consistent with the geometry/geography branches in the same function, which already use single-match-plus-guard.errors/exceptions/captured.py(5): TheCapturedExceptionaccessors accessSparkContext._jvm.PythonErrorUtils, where_jvmisOptional[JVMView]. Each method already assertsSparkContext._gateway is not None; adding the matchingassert SparkContext._jvm is not None(the same pattern already used elsewhere in the file) narrows_jvmand removes the ignore.sql/connect/client/core.py(1): The code narrowed a localsessionviaif session is not None:but then re-read the Optional class attributePySparkSession._instantiatedSession._jvm. Using the narrowed local (session._jvm) removes the ignore and the redundant re-read.sql/catalog.py(1): The code assertedsc is not Nonebut the ignore was aboutsc._gatewaybeing Optional. Addingassert sc._gateway is not Nonecompletes the guard the code had already started.Why are the changes needed?
These ignores suppressed mypy
[union-attr]errors that arise only because a value is typed Optional at the access site, even though the surrounding code guarantees it is not None. Making the assumption explicit is clearer and matches idioms the codebase already uses nearby. Theassertadditions do not introduce a new failure mode - the affected code already required a live JVM/gateway on these paths.Does this PR introduce any user-facing change?
No.
How was this patch tested?
mypypasses at full scope overpython/pyspark(1287 source files). No behavior change; existing tests cover the affected code paths.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 4.8)