Skip to content

Commit 722ac1b

Browse files
xinrong-mengueshin
authored andcommitted
[SPARK-36910][PYTHON] Inline type hints for python/pyspark/sql/types.py
### What changes were proposed in this pull request? Inline type hints for python/pyspark/sql/types.py ### Why are the changes needed? Current stub files cannot support type checking for the function body. Inline type hints can type check the function body. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Existing tests. Closes apache#34174 from xinrong-databricks/inline_types. Authored-by: Xinrong Meng <[email protected]> Signed-off-by: Takuya UESHIN <[email protected]>
1 parent e3b4d1e commit 722ac1b

File tree

5 files changed

+280
-378
lines changed

5 files changed

+280
-378
lines changed

python/pyspark/pandas/frame.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6417,12 +6417,14 @@ def select_dtypes(
64176417
"""
64186418
from pyspark.sql.types import _parse_datatype_string # type: ignore[attr-defined]
64196419

6420+
include_list: List[str]
64206421
if not is_list_like(include):
6421-
include_list = [include] if include is not None else []
6422+
include_list = [cast(str, include)] if include is not None else []
64226423
else:
64236424
include_list = list(include)
6425+
exclude_list: List[str]
64246426
if not is_list_like(exclude):
6425-
exclude_list = [exclude] if exclude is not None else []
6427+
exclude_list = [cast(str, exclude)] if exclude is not None else []
64266428
else:
64276429
exclude_list = list(exclude)
64286430

python/pyspark/sql/dataframe.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ def schema(self) -> StructType:
310310
"""
311311
if self._schema is None:
312312
try:
313-
self._schema = _parse_datatype_json_string(self._jdf.schema().json())
313+
self._schema = cast(
314+
StructType, _parse_datatype_json_string(self._jdf.schema().json()))
314315
except Exception as e:
315316
raise ValueError(
316317
"Unable to parse datatype from schema. %s" % e) from e

python/pyspark/sql/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ def createDataFrame( # type: ignore[misc]
790790
raise TypeError("data is already a DataFrame")
791791

792792
if isinstance(schema, str):
793-
schema = _parse_datatype_string(schema)
793+
schema = cast(Union[AtomicType, StructType, str], _parse_datatype_string(schema))
794794
elif isinstance(schema, (list, tuple)):
795795
# Must re-encode any unicode strings to be consistent with StructField names
796796
schema = [x.encode('utf-8') if not isinstance(x, str) else x for x in schema]

0 commit comments

Comments
 (0)