Skip to content

Commit 0477065

Browse files
committed
Add the deprecate_nonkeyword_arguments decorator and some tests for it.
1 parent 99e0e3c commit 0477065

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

pandas/io/html.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from pandas.io.formats.printing import pprint_thing
2121
from pandas.io.parsers import TextParser
2222

23+
from pandas.util._decorators import deprecate_nonkeyword_arguments
24+
2325
_IMPORTS = False
2426
_HAS_BS4 = False
2527
_HAS_LXML = False
@@ -920,10 +922,10 @@ def _parse(flavor, io, match, attrs, encoding, displayed_only, **kwargs):
920922
return ret
921923

922924

925+
@deprecate_nonkeyword_arguments(version="1.0", allowed_args=['io', 'match'])
923926
def read_html(
924927
io,
925928
match=".+",
926-
*,
927929
flavor=None,
928930
header=None,
929931
index_col=None,

pandas/io/json/_json.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
from pandas.io.formats.printing import pprint_thing
2424
from pandas.io.parsers import _validate_integer
2525

26+
from pandas.util._decorators import deprecate_nonkeyword_arguments
27+
2628
from ._normalize import convert_to_line_delimits
2729
from ._table_schema import build_table_schema, parse_table_schema
2830

@@ -331,9 +333,9 @@ def _write(
331333
return serialized
332334

333335

336+
@deprecate_nonkeyword_arguments(version="1.0", allowed_args=['path_or_buf'])
334337
def read_json(
335338
path_or_buf=None,
336-
*,
337339
orient=None,
338340
typ="frame",
339341
dtype=None,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Tests for the `deprecate_nonkeyword_arguments` decorator
3+
"""
4+
5+
from pandas.util._decorators import deprecate_nonkeyword_arguments
6+
7+
import pandas.util.testing as tm
8+
9+
10+
@deprecate_nonkeyword_arguments(version="1.0", allowed_args=['a', 'b'])
11+
def f(a, b=0, c=0, d=0):
12+
"""
13+
Sum of one to four numbers, the two last arguments
14+
should be given as keyword arguments only
15+
"""
16+
return a + b + c + d
17+
18+
19+
def test_two_arguments(x):
20+
"""
21+
Check whether no future warning is produced if two
22+
positional arguments given.
23+
"""
24+
assert f(1, 5) = 6
25+
26+
27+
def test_two_arguments(x):
28+
"""
29+
Check whether no future warning is produced if two
30+
positional arguments and two keyword arguments are
31+
given.
32+
"""
33+
assert f(1, 3, c=3, d=5) = 12
34+
35+
36+
def test_four_arguments(x):
37+
"""
38+
Check whether a future warning is produced if four
39+
positional arguments given.
40+
"""
41+
with tm.assert_produces_warning(FutureWarning):
42+
assert f(1, 2, 3, 4) = 10

pandas/util/_decorators.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,42 @@ def wrapper(*args, **kwargs):
212212
return _deprecate_kwarg
213213

214214

215+
def deprecate_nonkeyword_arguments(
216+
version: str,
217+
allowed_args:,
218+
*,
219+
stacklevel : int = 2
220+
) -> Callable
221+
"""
222+
Decorator to deprecate a use of non-keyword arguments of a function.
223+
224+
Parameters
225+
----------
226+
version : str
227+
The version in which positional arguments will become
228+
keyword-only.
229+
230+
allowed_args : list or int
231+
In case of list, it must be the list of names of some
232+
first arguments of the decorated functions that are
233+
OK to be given as positional arguments. In case of an
234+
integer, this is the number of positional arguments
235+
that will stay positional.
236+
237+
stacklevel : int, default=2
238+
The stack level for warnings.warn
239+
"""
240+
241+
def decorate(func):
242+
@wraps(func)
243+
def wrapper(*args, **kwargs):
244+
return func(*args, **kwargs)
245+
246+
return wrapper
247+
248+
return decorate
249+
250+
215251
def rewrite_axis_style_signature(
216252
name: str, extra_params: List[Tuple[str, Any]]
217253
) -> Callable:

0 commit comments

Comments
 (0)