Skip to content

Commit 9b30a8e

Browse files
authored
handle case when pandera is run with optimized python mode (#1749)
Signed-off-by: cosmicBboy <[email protected]>
1 parent b8604b3 commit 9b30a8e

File tree

4 files changed

+16
-23
lines changed

4 files changed

+16
-23
lines changed

pandera/api/extensions.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414
from pandera.api.hypotheses import Hypothesis
1515
from pandera.strategies.base_strategies import STRATEGY_DISPATCHER
1616

17-
try:
18-
import pyspark.sql as ps
19-
20-
PYSPARK_INSTALLED = True
21-
except ImportError: # pragma: no cover
22-
PYSPARK_INSTALLED = False
23-
2417

2518
class BuiltinCheckRegistrationError(Exception):
2619
"""
@@ -180,9 +173,17 @@ def register_check_method( # pylint:disable=too-many-branches
180173
:return: register check function wrapper.
181174
"""
182175

183-
# pylint: disable=import-outside-toplevel
176+
# pylint: disable=import-outside-toplevel,too-many-statements
184177
from pandera.strategies.pandas_strategies import register_check_strategy
185178

179+
# NOTE: this needs to handle different dataframe types more elegantly
180+
try:
181+
import pyspark.sql as ps
182+
183+
PYSPARK_INSTALLED = True
184+
except ImportError: # pragma: no cover
185+
PYSPARK_INSTALLED = False
186+
186187
if statistics is None:
187188
statistics = []
188189

pandera/engines/pyspark_engine.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,15 @@
1717

1818
import pyspark
1919
import pyspark.sql.types as pst
20+
from pyspark.sql import DataFrame
2021
from packaging import version
2122

2223
from pandera import dtypes, errors
2324
from pandera.dtypes import immutable
2425
from pandera.engines import engine
25-
from pandera.engines.type_aliases import PysparkObject
2626

27-
try:
28-
import pyarrow # pylint:disable=unused-import
2927

30-
PYARROW_INSTALLED = True
31-
except ImportError: # pragma: no cover
32-
PYARROW_INSTALLED = False
28+
PysparkObject = Union[DataFrame]
3329

3430

3531
DEFAULT_PYSPARK_PREC = pst.DecimalType().precision

pandera/engines/type_aliases.py

-9
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,7 @@
55
import numpy as np
66
import pandas as pd
77

8-
try:
9-
from pyspark.sql import DataFrame
10-
11-
PYSPARK_INSTALLED = True
12-
except ImportError: # pragma: no cover
13-
PYSPARK_INSTALLED = False
148

159
PandasObject = Union[pd.Series, pd.DataFrame]
1610
PandasExtensionType = pd.core.dtypes.base.ExtensionDtype
1711
PandasDataType = Union[pd.core.dtypes.base.ExtensionDtype, np.dtype, type]
18-
19-
if PYSPARK_INSTALLED:
20-
PysparkObject = Union[DataFrame]

pandera/utils.py

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ def docstring_substitution(*args: Any, **kwargs: Any) -> Callable[[F], F]:
99
"""Typed wrapper around pandas.util.Substitution."""
1010

1111
def decorator(func: F) -> F:
12+
# handle case when pandera is run in optimized mode:
13+
# https://docs.python.org/3/using/cmdline.html#cmdoption-OO
14+
if func.__doc__ is None:
15+
return func
16+
1217
if args:
1318
_doc = func.__doc__ % tuple(args) # type: ignore[operator]
1419
elif kwargs:

0 commit comments

Comments
 (0)