Skip to content
Open
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions pandas/tests/config/test_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
import warnings

import pytest

from pandas._config import config as cf
Expand Down Expand Up @@ -477,6 +480,21 @@ def test_option_context_scope(self):
# Ensure the current context is reset
assert cf.get_option(option_name) == original_value

def test_option_context_deprecated_stacklevel(self):
cf.register_option("a", 1)
cf.deprecate_option("a", FutureWarning)

expected = os.path.normcase(os.path.abspath(__file__))

with warnings.catch_warnings(record=True) as recorded:
warnings.simplefilter("always")
with pd.option_context("a", 2):
pass

assert recorded
for warning in recorded:
assert os.path.normcase(warning.filename) == expected

def test_dictwrapper_getattr(self):
options = cf.options
# GH 19789
Expand Down
7 changes: 7 additions & 0 deletions pandas/util/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def find_stack_level() -> int:

pkg_dir = os.path.dirname(pd.__file__)
test_dir = os.path.join(pkg_dir, "tests")
contextlib_file = getattr(contextlib, "__file__", None)
contextlib_dir = (
os.path.dirname(contextlib_file) if contextlib_file is not None else None
)

# https://stackoverflow.com/questions/17407119/python-inspect-stack-is-slow
frame: FrameType | None = inspect.currentframe()
Expand All @@ -54,6 +58,9 @@ def find_stack_level() -> int:
if filename.startswith(pkg_dir) and not filename.startswith(test_dir):
frame = frame.f_back
n += 1
elif contextlib_dir and filename.startswith(contextlib_dir):
frame = frame.f_back
n += 1
else:
break
finally:
Expand Down
Loading