Skip to content

Stacklevel argument updated #46687 #47035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 30, 2022
1 change: 0 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10973,7 +10973,6 @@ def _add_numeric_operations(cls):
@deprecate_nonkeyword_arguments(
version=None,
allowed_args=["self"],
stacklevel=find_stack_level() - 1,
name="DataFrame.any and Series.any",
)
@doc(
Expand Down
4 changes: 1 addition & 3 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,7 @@ def obj_to_write(self) -> NDFrame | Mapping[IndexLabel, Any]:
decompression_options=_shared_docs["decompression_options"] % "path_or_buf",
)
@deprecate_kwarg(old_arg_name="numpy", new_arg_name=None)
@deprecate_nonkeyword_arguments(
version="2.0", allowed_args=["path_or_buf"], stacklevel=3
)
@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["path_or_buf"])
def read_json(
path_or_buf=None,
orient=None,
Expand Down
8 changes: 2 additions & 6 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,9 +1166,7 @@ def read_table(
...


@deprecate_nonkeyword_arguments(
version=None, allowed_args=["filepath_or_buffer"], stacklevel=3
)
@deprecate_nonkeyword_arguments(version=None, allowed_args=["filepath_or_buffer"])
@Appender(
_doc_read_csv_and_table.format(
func_name="read_table",
Expand Down Expand Up @@ -1265,9 +1263,7 @@ def read_table(
return _read(filepath_or_buffer, kwds)


@deprecate_nonkeyword_arguments(
version=None, allowed_args=["filepath_or_buffer"], stacklevel=2
)
@deprecate_nonkeyword_arguments(version=None, allowed_args=["filepath_or_buffer"])
def read_fwf(
filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str],
colspecs: Sequence[tuple[int, int]] | str | None = "infer",
Expand Down
4 changes: 1 addition & 3 deletions pandas/io/sas/sasreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def read_sas(
...


@deprecate_nonkeyword_arguments(
version=None, allowed_args=["filepath_or_buffer"], stacklevel=2
)
@deprecate_nonkeyword_arguments(version=None, allowed_args=["filepath_or_buffer"])
@doc(decompression_options=_shared_docs["decompression_options"])
def read_sas(
filepath_or_buffer: FilePath | ReadBuffer[bytes],
Expand Down
4 changes: 1 addition & 3 deletions pandas/io/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,9 +974,7 @@ def _parse(
)


@deprecate_nonkeyword_arguments(
version=None, allowed_args=["path_or_buffer"], stacklevel=2
)
@deprecate_nonkeyword_arguments(version=None, allowed_args=["path_or_buffer"])
@doc(
storage_options=_shared_docs["storage_options"],
decompression_options=_shared_docs["decompression_options"] % "path_or_buffer",
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/io/parser/common/test_common_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,5 +920,4 @@ def test_read_table_posargs_deprecation(all_parsers):
"In a future version of pandas all arguments of read_table "
"except for the argument 'filepath_or_buffer' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
parser.read_table(data, " ")
parser.read_table_check_warnings(FutureWarning, msg, data, " ")
10 changes: 10 additions & 0 deletions pandas/tests/io/parser/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ def read_table(self, *args, **kwargs):
kwargs = self.update_kwargs(kwargs)
return read_table(*args, **kwargs)

def read_table_check_warnings(
self, warn_type: type[Warning], warn_msg: str, *args, **kwargs
):
# We need to check the stacklevel here instead of in the tests
# since this is where read_table is called and where the warning
# should point to.
kwargs = self.update_kwargs(kwargs)
with tm.assert_produces_warning(warn_type, match=warn_msg):
return read_table(*args, **kwargs)


class CParser(BaseParser):
engine = "c"
Expand Down
7 changes: 2 additions & 5 deletions pandas/util/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from pandas._libs.properties import cache_readonly # noqa:F401
from pandas._typing import F
from pandas.util._exceptions import find_stack_level


def deprecate(
Expand Down Expand Up @@ -260,7 +261,6 @@ def future_version_msg(version: str | None) -> str:
def deprecate_nonkeyword_arguments(
version: str | None,
allowed_args: list[str] | None = None,
stacklevel: int = 2,
name: str | None = None,
) -> Callable[[F], F]:
"""
Expand All @@ -280,9 +280,6 @@ def deprecate_nonkeyword_arguments(
defaults to list of all arguments not having the
default value.

stacklevel : int, default=2
The stack level for warnings.warn

name : str, optional
The specific name of the function to show in the warning
message. If None, then the Qualified name of the function
Expand Down Expand Up @@ -312,7 +309,7 @@ def wrapper(*args, **kwargs):
warnings.warn(
msg.format(arguments=arguments),
FutureWarning,
stacklevel=stacklevel,
stacklevel=find_stack_level(),
)
return func(*args, **kwargs)

Expand Down