adityavaish opened a new pull request, #60182:
URL: https://github.com/apache/spark/pull/60182
### What changes were proposed in this pull request?
Add overloads to `StructType.__getitem__` so that name/index access returns
`StructField`, while slicing returns `StructType`. Update the implementation
annotation and method docstring to describe the existing runtime behavior.
Add typing regression coverage for names, positive and negative indices,
bounded/open/stepped/reversed slices, chained schema operations, and
union-typed keys.
### Why are the changes needed?
`StructType` already supports slicing, but its annotation only accepts `str
| int`
and always returns `StructField`. Type checkers therefore reject valid code:
```python
from pyspark.sql.types import IntegerType, StructField, StructType
schema = StructType([StructField("value", IntegerType())])
schema[:1].fieldNames()
```
Previously, mypy reports both an invalid slice index and that `StructField`
has no
`fieldNames` attribute. The overloads describe the actual return type without
widening ordinary field access to a union.
### Does this PR introduce _any_ user-facing change?
Yes. Static type checkers now accept schema slices and infer `StructType` for
them, while continuing to infer `StructField` for string/integer keys.
Runtime behavior is unchanged.
### How was this patch tested?
- Added `python/pyspark/sql/tests/typing/test_types.yml`.
- Verified the new typing case fails against the original annotation and
passes
with this patch using mypy 1.19.1 and pytest-mypy-plugins 3.2.0.
- The successful local run used the repository's pytest invocation with an
isolated mypy configuration setting `follow_imports = silent` and
`follow_imports_for_stubs = True`. This still resolves and checks the
actual
PySpark types and the test's `assert_type` calls, but suppresses
diagnostics
from imported modules.
- The unmodified repository mypy configuration was attempted but could not
complete successfully because optional typing dependencies are missing.
Restoring the declared dependency groups was blocked by package-download
timeouts. The normal CI typing environment should run this test without
that
local configuration override.
- `ruff check python/pyspark/sql/types.py` and
`ruff format --check python/pyspark/sql/types.py` passed.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: GitHub Copilot App 1.0.102-0.unstable.r40949665530.gab1eef5
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]