Skip to content

Commit 7d4bc0a

Browse files
Anupam-USPjrebackMarcoGorelli
authored andcommitted
Stacklevel argument updated pandas-dev#46687 (pandas-dev#47035)
* Stacklevel argument updated pandas-dev#46687 * argument removed * param added * fixup * remove stackevel from read_sas decorator * add read_table_check_warnings and use it in read_table posargs deprecation test Co-authored-by: Jeff Reback <[email protected]> Co-authored-by: Marco Gorelli <[email protected]>
1 parent a44d8ee commit 7d4bc0a

File tree

8 files changed

+18
-23
lines changed

8 files changed

+18
-23
lines changed

pandas/core/generic.py

-1
Original file line numberDiff line numberDiff line change
@@ -10973,7 +10973,6 @@ def _add_numeric_operations(cls):
1097310973
@deprecate_nonkeyword_arguments(
1097410974
version=None,
1097510975
allowed_args=["self"],
10976-
stacklevel=find_stack_level() - 1,
1097710976
name="DataFrame.any and Series.any",
1097810977
)
1097910978
@doc(

pandas/io/json/_json.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,7 @@ def read_json(
461461
decompression_options=_shared_docs["decompression_options"] % "path_or_buf",
462462
)
463463
@deprecate_kwarg(old_arg_name="numpy", new_arg_name=None)
464-
@deprecate_nonkeyword_arguments(
465-
version="2.0", allowed_args=["path_or_buf"], stacklevel=3
466-
)
464+
@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["path_or_buf"])
467465
def read_json(
468466
path_or_buf: FilePath | ReadBuffer[str] | ReadBuffer[bytes],
469467
orient=None,

pandas/io/parsers/readers.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1182,9 +1182,7 @@ def read_table(
11821182
...
11831183

11841184

1185-
@deprecate_nonkeyword_arguments(
1186-
version=None, allowed_args=["filepath_or_buffer"], stacklevel=3
1187-
)
1185+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["filepath_or_buffer"])
11881186
@Appender(
11891187
_doc_read_csv_and_table.format(
11901188
func_name="read_table",
@@ -1281,9 +1279,7 @@ def read_table(
12811279
return _read(filepath_or_buffer, kwds)
12821280

12831281

1284-
@deprecate_nonkeyword_arguments(
1285-
version=None, allowed_args=["filepath_or_buffer"], stacklevel=2
1286-
)
1282+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["filepath_or_buffer"])
12871283
def read_fwf(
12881284
filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str],
12891285
colspecs: Sequence[tuple[int, int]] | str | None = "infer",

pandas/io/sas/sasreader.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ def read_sas(
7878
...
7979

8080

81-
@deprecate_nonkeyword_arguments(
82-
version=None, allowed_args=["filepath_or_buffer"], stacklevel=2
83-
)
81+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["filepath_or_buffer"])
8482
@doc(decompression_options=_shared_docs["decompression_options"])
8583
def read_sas(
8684
filepath_or_buffer: FilePath | ReadBuffer[bytes],

pandas/io/xml.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -974,9 +974,7 @@ def _parse(
974974
)
975975

976976

977-
@deprecate_nonkeyword_arguments(
978-
version=None, allowed_args=["path_or_buffer"], stacklevel=2
979-
)
977+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["path_or_buffer"])
980978
@doc(
981979
storage_options=_shared_docs["storage_options"],
982980
decompression_options=_shared_docs["decompression_options"] % "path_or_buffer",

pandas/tests/io/parser/common/test_common_basic.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -920,5 +920,4 @@ def test_read_table_posargs_deprecation(all_parsers):
920920
"In a future version of pandas all arguments of read_table "
921921
"except for the argument 'filepath_or_buffer' will be keyword-only"
922922
)
923-
with tm.assert_produces_warning(FutureWarning, match=msg):
924-
parser.read_table(data, " ")
923+
parser.read_table_check_warnings(FutureWarning, msg, data, " ")

pandas/tests/io/parser/conftest.py

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ def read_table(self, *args, **kwargs):
4242
kwargs = self.update_kwargs(kwargs)
4343
return read_table(*args, **kwargs)
4444

45+
def read_table_check_warnings(
46+
self, warn_type: type[Warning], warn_msg: str, *args, **kwargs
47+
):
48+
# We need to check the stacklevel here instead of in the tests
49+
# since this is where read_table is called and where the warning
50+
# should point to.
51+
kwargs = self.update_kwargs(kwargs)
52+
with tm.assert_produces_warning(warn_type, match=warn_msg):
53+
return read_table(*args, **kwargs)
54+
4555

4656
class CParser(BaseParser):
4757
engine = "c"

pandas/util/_decorators.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from pandas._libs.properties import cache_readonly # noqa:F401
1515
from pandas._typing import F
16+
from pandas.util._exceptions import find_stack_level
1617

1718

1819
def deprecate(
@@ -260,7 +261,6 @@ def future_version_msg(version: str | None) -> str:
260261
def deprecate_nonkeyword_arguments(
261262
version: str | None,
262263
allowed_args: list[str] | None = None,
263-
stacklevel: int = 2,
264264
name: str | None = None,
265265
) -> Callable[[F], F]:
266266
"""
@@ -280,9 +280,6 @@ def deprecate_nonkeyword_arguments(
280280
defaults to list of all arguments not having the
281281
default value.
282282
283-
stacklevel : int, default=2
284-
The stack level for warnings.warn
285-
286283
name : str, optional
287284
The specific name of the function to show in the warning
288285
message. If None, then the Qualified name of the function
@@ -312,7 +309,7 @@ def wrapper(*args, **kwargs):
312309
warnings.warn(
313310
msg.format(arguments=arguments),
314311
FutureWarning,
315-
stacklevel=stacklevel,
312+
stacklevel=find_stack_level(),
316313
)
317314
return func(*args, **kwargs)
318315

0 commit comments

Comments
 (0)