You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by gu...@apache.org on 2025/07/07 23:19:42 UTC

(spark) branch master updated: [SPARK-52698][PYTHON] Improve type hints for datasource module

This is an automated email from the ASF dual-hosted git repository.

gurwls223 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new a9b8e370893b [SPARK-52698][PYTHON] Improve type hints for datasource module
a9b8e370893b is described below

commit a9b8e370893b271e2a8974c42feb31094b5bee8e
Author: Allison Wang <al...@databricks.com>
AuthorDate: Tue Jul 8 08:19:31 2025 +0900

    [SPARK-52698][PYTHON] Improve type hints for datasource module
    
    ### What changes were proposed in this pull request?
    
    This PR improves the type annotations in python/pyspark/sql/datasource.py to use Python 3.10 typing syntax and built-in types instead of their typing module equivalents.
    
    ### Why are the changes needed?
    
    Follows current Python typing recommendations and best practices.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No
    
    ### How was this patch tested?
    
    Existing tests
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    No
    
    Closes #51392 from allisonwang-db/spark-52698-type-hint.
    
    Authored-by: Allison Wang <al...@databricks.com>
    Signed-off-by: Hyukjin Kwon <gu...@apache.org>
---
 python/pyspark/sql/datasource.py | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)

diff --git a/python/pyspark/sql/datasource.py b/python/pyspark/sql/datasource.py
index c9704ff9f259..ca8dffbdbb4c 100644
--- a/python/pyspark/sql/datasource.py
+++ b/python/pyspark/sql/datasource.py
@@ -19,15 +19,12 @@ from collections import UserDict
 from dataclasses import dataclass
 from typing import (
     Any,
-    Dict,
     Iterable,
     Iterator,
     List,
     Optional,
     Sequence,
-    Tuple,
     Type,
-    Union,
     TYPE_CHECKING,
 )
 
@@ -49,7 +46,6 @@ __all__ = [
     "DataSourceStreamWriter",
     "DataSourceRegistration",
     "InputPartition",
-    "SimpleDataSourceStreamReader",
     "WriterCommitMessage",
     "Filter",
     "EqualTo",
@@ -84,7 +80,7 @@ class DataSource(ABC):
     .. versionadded: 4.0.0
     """
 
-    def __init__(self, options: Dict[str, str]) -> None:
+    def __init__(self, options: dict[str, str]) -> None:
         """
         Initializes the data source with user-provided options.
 
@@ -114,7 +110,7 @@ class DataSource(ABC):
         """
         return cls.__name__
 
-    def schema(self) -> Union[StructType, str]:
+    def schema(self) -> StructType | str:
         """
         Returns the schema of the data source.
 
@@ -261,7 +257,7 @@ class DataSource(ABC):
         )
 
 
-ColumnPath = Tuple[str, ...]
+ColumnPath = tuple[str, ...]
 """
 A tuple of strings representing a column reference.
 
@@ -407,7 +403,7 @@ class In(Filter):
     """
 
     attribute: ColumnPath
-    value: Tuple[Any, ...]
+    value: tuple[Any, ...]
 
 
 @dataclass(frozen=True)
@@ -631,7 +627,7 @@ class DataSourceReader(ABC):
         )
 
     @abstractmethod
-    def read(self, partition: InputPartition) -> Union[Iterator[Tuple], Iterator["RecordBatch"]]:
+    def read(self, partition: InputPartition) -> Iterator[tuple] | Iterator["RecordBatch"]:
         """
         Generates data for a given partition and returns an iterator of tuples or rows.
 
@@ -760,7 +756,7 @@ class DataSourceStreamReader(ABC):
         )
 
     @abstractmethod
-    def read(self, partition: InputPartition) -> Union[Iterator[Tuple], Iterator["RecordBatch"]]:
+    def read(self, partition: InputPartition) -> Iterator[tuple] | Iterator["RecordBatch"]:
         """
         Generates data for a given partition and returns an iterator of tuples or rows.
 
@@ -852,7 +848,7 @@ class SimpleDataSourceStreamReader(ABC):
             messageParameters={"feature": "initialOffset"},
         )
 
-    def read(self, start: dict) -> Tuple[Iterator[Tuple], dict]:
+    def read(self, start: dict) -> tuple[Iterator[tuple], dict]:
         """
         Read all available data from start offset and return the offset that next read attempt
         starts from.
@@ -864,7 +860,7 @@ class SimpleDataSourceStreamReader(ABC):
 
         Returns
         -------
-        A :class:`Tuple` of an iterator of :class:`Tuple` and a dict\\s
+        A :class:`tuple` of an iterator of :class:`tuple` and a dict\\s
             The iterator contains all the available records after start offset.
             The dict is the end offset of this read attempt and the start of next read attempt.
         """
@@ -873,7 +869,7 @@ class SimpleDataSourceStreamReader(ABC):
             messageParameters={"feature": "read"},
         )
 
-    def readBetweenOffsets(self, start: dict, end: dict) -> Iterator[Tuple]:
+    def readBetweenOffsets(self, start: dict, end: dict) -> Iterator[tuple]:
         """
         Read all available data from specific start offset and end offset.
         This is invoked during failure recovery to re-read a batch deterministically.
@@ -888,7 +884,7 @@ class SimpleDataSourceStreamReader(ABC):
 
         Returns
         -------
-        iterator of :class:`Tuple`\\s
+        iterator of :class:`tuple`\\s
             All the records between start offset and end offset.
         """
         raise PySparkNotImplementedError(


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org