From 7cccfa1d422ce32c360d37799a653e9619e1a684 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Thu, 2 Dec 2021 17:25:36 -0500 Subject: [PATCH 1/4] Add tracking code --- pandas/util/_exceptions.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pandas/util/_exceptions.py b/pandas/util/_exceptions.py index 806e2abe83a92..1869d1120f160 100644 --- a/pandas/util/_exceptions.py +++ b/pandas/util/_exceptions.py @@ -36,6 +36,20 @@ def find_stack_level() -> int: pkg_dir = os.path.dirname(pd.__file__) test_dir = os.path.join(pkg_dir, "tests") + import json + + filename = "/home/richard/pandas/find_stack_level.json" + if os.path.exists(filename): + with open(filename) as f: + calls = json.load(f) + else: + calls = {} + caller = f"{stack[1].filename[len(pkg_dir):]}:{stack[1].lineno}" + calls[caller] = calls.get(caller, 0) + 1 + + with open(filename, "w") as f: + json.dump(calls, f) + for n in range(len(stack)): fname = stack[n].filename if fname.startswith(pkg_dir) and not fname.startswith(test_dir): From f7fee11e5896366433bfe6a395fcfc592258aca9 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sat, 4 Dec 2021 05:30:01 -0500 Subject: [PATCH 2/4] WIP --- pandas/conftest.py | 12 ++++++++++++ pandas/core/arrays/categorical.py | 3 ++- pandas/core/indexes/base.py | 2 +- pandas/util/_exceptions.py | 20 ++++++++++++++++++-- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index eb9a952250f36..dbce45b63d99d 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -67,6 +67,18 @@ MultiIndex, ) + +@pytest.fixture(autouse=True) +def get_test_id(request): + if hasattr(request.node, "callspec"): + id = request.node.callspec.id + else: + # When a test is not parametrized, callspec isn't yet setup + id = "none" + with open("/home/richard/pandas/pytest_hack.txt", "w") as f: + f.write(id) + + # Until https://github.com/numpy/numpy/issues/19078 is sorted out, just suppress suppress_npdev_promotion_warning = pytest.mark.filterwarnings( "ignore:Promotion of numbers and bools:FutureWarning" diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 4c6a32ff1ba4e..0047fc1d6d314 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -945,7 +945,8 @@ def set_categories( "a future version. Removing unused categories will always " "return a new Categorical object.", FutureWarning, - stacklevel=find_stack_level(), + # stacklevel=find_stack_level(), + stacklevel=1, ) else: inplace = False diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 2cf8ea95a345a..f292316386f5e 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -7016,7 +7016,7 @@ def _maybe_cast_data_without_dtype( "In a future version, the Index constructor will not infer numeric " "dtypes when passed object-dtype sequences (matching Series behavior)", FutureWarning, - stacklevel=find_stack_level(), + stacklevel=3, ) if result.dtype.kind in ["b", "c"]: return subarr diff --git a/pandas/util/_exceptions.py b/pandas/util/_exceptions.py index 1869d1120f160..9da0ecf973ac7 100644 --- a/pandas/util/_exceptions.py +++ b/pandas/util/_exceptions.py @@ -44,8 +44,24 @@ def find_stack_level() -> int: calls = json.load(f) else: calls = {} - caller = f"{stack[1].filename[len(pkg_dir):]}:{stack[1].lineno}" - calls[caller] = calls.get(caller, 0) + 1 + + with open("/home/richard/pandas/pytest_hack.txt") as f: + pytest_id = f.read() + + mystack = inspect.stack() + test_files = [ + f"{e.filename[len(pkg_dir):]}:{e.lineno}:{pytest_id}" + for e in mystack + if e.filename.startswith(test_dir) + ] + if len(test_files) == 0: + tester = "UNKNOWN" + else: + tester = test_files[0] + # caller = f"{stack[1].filename[len(pkg_dir):]}:{stack[1].lineno}" + entry = calls.get(tester, {}) + entry["calls"] = entry.get("calls", 0) + 1 + calls[tester] = entry with open(filename, "w") as f: json.dump(calls, f) From 2bcf23955b6715b8f79cb61a479a5451ec694bdb Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sat, 4 Dec 2021 10:29:50 -0500 Subject: [PATCH 3/4] Remove testing code, revert stacklevel --- pandas/conftest.py | 12 ------------ pandas/core/arrays/categorical.py | 3 +-- pandas/util/_exceptions.py | 30 ------------------------------ 3 files changed, 1 insertion(+), 44 deletions(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index dbce45b63d99d..eb9a952250f36 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -67,18 +67,6 @@ MultiIndex, ) - -@pytest.fixture(autouse=True) -def get_test_id(request): - if hasattr(request.node, "callspec"): - id = request.node.callspec.id - else: - # When a test is not parametrized, callspec isn't yet setup - id = "none" - with open("/home/richard/pandas/pytest_hack.txt", "w") as f: - f.write(id) - - # Until https://github.com/numpy/numpy/issues/19078 is sorted out, just suppress suppress_npdev_promotion_warning = pytest.mark.filterwarnings( "ignore:Promotion of numbers and bools:FutureWarning" diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 968204b5f9994..67dc6ade25254 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -940,8 +940,7 @@ def set_categories( "a future version. Removing unused categories will always " "return a new Categorical object.", FutureWarning, - # stacklevel=find_stack_level(), - stacklevel=1, + stacklevel=2, ) else: inplace = False diff --git a/pandas/util/_exceptions.py b/pandas/util/_exceptions.py index 9da0ecf973ac7..806e2abe83a92 100644 --- a/pandas/util/_exceptions.py +++ b/pandas/util/_exceptions.py @@ -36,36 +36,6 @@ def find_stack_level() -> int: pkg_dir = os.path.dirname(pd.__file__) test_dir = os.path.join(pkg_dir, "tests") - import json - - filename = "/home/richard/pandas/find_stack_level.json" - if os.path.exists(filename): - with open(filename) as f: - calls = json.load(f) - else: - calls = {} - - with open("/home/richard/pandas/pytest_hack.txt") as f: - pytest_id = f.read() - - mystack = inspect.stack() - test_files = [ - f"{e.filename[len(pkg_dir):]}:{e.lineno}:{pytest_id}" - for e in mystack - if e.filename.startswith(test_dir) - ] - if len(test_files) == 0: - tester = "UNKNOWN" - else: - tester = test_files[0] - # caller = f"{stack[1].filename[len(pkg_dir):]}:{stack[1].lineno}" - entry = calls.get(tester, {}) - entry["calls"] = entry.get("calls", 0) + 1 - calls[tester] = entry - - with open(filename, "w") as f: - json.dump(calls, f) - for n in range(len(stack)): fname = stack[n].filename if fname.startswith(pkg_dir) and not fname.startswith(test_dir): From 88928c06aa52f3b8039edfaa3a331de17c4b5a89 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sat, 4 Dec 2021 11:03:08 -0500 Subject: [PATCH 4/4] Remove testing code, revert stacklevel --- pandas/tests/frame/indexing/test_setitem.py | 2 +- pandas/tests/series/accessors/test_cat_accessor.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index 597216f55e444..d30b726966e3d 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -947,7 +947,7 @@ def test_setitem_mask_categorical(self): df = DataFrame({"cats": catsf, "values": valuesf}, index=idxf) exp_fancy = exp_multi_row.copy() - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): # issue #37643 inplace kwarg deprecated return_value = exp_fancy["cats"].cat.set_categories( ["a", "b", "c"], inplace=True diff --git a/pandas/tests/series/accessors/test_cat_accessor.py b/pandas/tests/series/accessors/test_cat_accessor.py index fb2071ac9c3f6..9dfb36a71c861 100644 --- a/pandas/tests/series/accessors/test_cat_accessor.py +++ b/pandas/tests/series/accessors/test_cat_accessor.py @@ -51,7 +51,7 @@ def test_cat_accessor(self): exp = Categorical(["a", "b", np.nan, "a"], categories=["b", "a"]) - with tm.assert_produces_warning(FutureWarning): + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): # issue #37643 inplace kwarg deprecated return_value = ser.cat.set_categories(["b", "a"], inplace=True)