⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions python/pyspark/sql/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
from types import TracebackType
from typing import (
Any,
Callable,
ClassVar,
Dict,
Iterable,
Generic,
List,
Optional,
Tuple,
Type,
TypeVar,
Union,
Set,
cast,
Expand Down Expand Up @@ -136,7 +139,10 @@ def toDF(self, schema=None, sampleRatio=None):
RDD.toDF = toDF # type: ignore[method-assign]


class classproperty(property):
T = TypeVar("T")


class classproperty(Generic[T]):
"""Same as Python's @property decorator, but for class attributes.

Examples
Expand All @@ -161,12 +167,13 @@ class classproperty(property):
True
"""

def __get__(self, instance: Any, owner: Any = None) -> "SparkSession.Builder":
# The "type: ignore" below silences the following error from mypy:
# error: Argument 1 to "classmethod" has incompatible
# type "Optional[Callable[[Any], Any]]";
# expected "Callable[..., Any]" [arg-type]
return classmethod(self.fget).__get__(None, owner)() # type: ignore
def __init__(self, fget: Callable[[type], T]) -> None:
self.fget = fget

def __get__(self, instance: Any, owner: Optional[type]) -> T:
if owner is None:
owner = type(instance)
return self.fget(owner)


class SparkSession(SparkConversionMixin):
Expand Down Expand Up @@ -603,12 +610,14 @@ def create(self) -> "SparkSession":

# SPARK-47544: Explicitly declaring this as an identifier instead of a method.
# If changing, make sure this bug is not reintroduced.
builder: Builder = classproperty(lambda cls: cls.Builder()) # type: ignore
"""Creates a :class:`Builder` for constructing a :class:`SparkSession`.
@classproperty
def builder(cls: Type["SparkSession"]) -> "Builder": # type: ignore[misc]
"""Creates a :class:`Builder` for constructing a :class:`SparkSession`.

.. versionchanged:: 3.4.0
Supports Spark Connect.
"""
.. versionchanged:: 3.4.0
Supports Spark Connect.
"""
return cls.Builder()

_instantiatedSession: ClassVar[Optional["SparkSession"]] = None
_activeSession: ClassVar[Optional["SparkSession"]] = None
Expand Down