Skip to content

Commit

Permalink
Call str.format function in better way and update documentation string
Browse files Browse the repository at this point in the history
  • Loading branch information
alexitkes committed Aug 6, 2019
1 parent 61e51eb commit cd84833
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
9 changes: 6 additions & 3 deletions pandas/tests/io/json/test_deprecated_kwargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def test_good_kwargs():
argument passed as keyword.
"""
df = pd.DataFrame({"A": [2, 4, 6], "B": [3, 6, 9]}, index=[0, 1, 2])
assert_frame_equal(df, read_json(df.to_json(orient="split"), orient="split"))
assert_frame_equal(df, read_json(df.to_json(orient="columns"), orient="columns"))
assert_frame_equal(df, read_json(df.to_json(orient="index"), orient="index"))
with tm.assert_produces_warning(None):
assert_frame_equal(df, read_json(df.to_json(orient="split"), orient="split"))
assert_frame_equal(
df, read_json(df.to_json(orient="columns"), orient="columns")
)
assert_frame_equal(df, read_json(df.to_json(orient="index"), orient="index"))
20 changes: 13 additions & 7 deletions pandas/tests/util/test_deprecate_nonkeyword_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ def f(a, b=0, c=0, d=0):
return a + b + c + d


def test_one_arguments():
def test_one_argument():
"""
Check whether no future warning is produced if one
positional argument given.
"""
assert f(19) == 19
with tm.assert_produces_warning(None):
assert f(19) == 19


def test_one_and_one_arguments():
Expand All @@ -30,15 +31,17 @@ def test_one_and_one_arguments():
positional argument and one keyword argument are
given.
"""
assert f(19, d=6) == 25
with tm.assert_produces_warning(None):
assert f(19, d=6) == 25


def test_two_arguments():
"""
Check whether no future warning is produced if two
positional arguments given.
"""
assert f(1, 5) == 6
with tm.assert_produces_warning(None):
assert f(1, 5) == 6


def test_two_and_two_arguments():
Expand All @@ -47,7 +50,8 @@ def test_two_and_two_arguments():
positional arguments and two keyword arguments are
given.
"""
assert f(1, 3, c=3, d=5) == 12
with tm.assert_produces_warning(None):
assert f(1, 3, c=3, d=5) == 12


def test_three_arguments():
Expand All @@ -74,7 +78,8 @@ def g(a, b=0, c=0, d=0):
Sum of one to four numbers, but three of them have default
values, so may be given as keyword arguments only.
"""
return a + b + c + d
with tm.assert_produces_warning(None):
return a + b + c + d


def test_one_and_three_arguments_default_allowed_args():
Expand All @@ -85,7 +90,8 @@ def test_one_and_three_arguments_default_allowed_args():
option, meaning that all arguments with default value
are keyword-only.
"""
assert g(1, b=3, c=3, d=5) == 12
with tm.assert_produces_warning(None):
assert g(1, b=3, c=3, d=5) == 12


def test_three_arguments_default_allowed_args():
Expand Down
20 changes: 13 additions & 7 deletions pandas/util/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def deprecate_nonkeyword_arguments(
The version in which positional arguments will become
keyword-only.
allowed_args : list, int or None, default=None
allowed_args : list or int, optional
In case of list, it must be the list of names of some
first arguments of the decorated functions that are
OK to be given as positional arguments. In case of an
Expand All @@ -248,14 +248,20 @@ def decorate(func):
def wrapper(*args, **kwargs):
if isinstance(allow_args, int) and len(args) > allow_args:
msg = (
"After version %s all arguments of %s "
"except for the first %i will be keyword-only"
) % (version, func.__name__, allow_args)
"After Pandas version {version} all arguments of {funcname} "
"except for the first {num_args} will be keyword-only"
).format(version=version, funcname=func.__name__, num_args=allow_args)
elif isinstance(allow_args, (list, tuple)) and len(args) > len(allow_args):
msg = (
"After version %s all arguments of %s "
"except for (%s) will be keyword-only"
) % (version, func.__name__, ", ".join(allow_args))
"After Pandas version {version} all arguments of {funcname} "
"except for {first_args} will be keyword-only"
).format(
version=version,
funcname=func.__name__,
first_args="(" + ", ".join(allow_args) + ")"
if len(allow_args) > 1
else allow_args[0],
)
else:
msg = None
if msg:
Expand Down

0 comments on commit cd84833

Please sign in to comment.