Skip to content

Commit 0382fcd

Browse files
committed
ENH: add check for arg kwarg conflict for the positional case
1 parent 9b821e5 commit 0382fcd

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

asv_bench/benchmarks/tslibs/timestamp.py

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def time_from_pd_timestamp(self):
5151
def time_from_positional(self):
5252
Timestamp(2020, 1, 1, 0, 0, 0)
5353

54+
def time_from_positional_tz(self):
55+
Timestamp(2020, 1, 1, 0, 0, 0, tzinfo=pytz.UTC)
56+
5457

5558
class TimestampProperties:
5659
params = [_tzs]

pandas/_libs/tslibs/timestamps.pyx

+13
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,19 @@ class Timestamp(_Timestamp):
15841584

15851585
if kwargs:
15861586
# Positional or keyword arguments
1587+
err_msg = (
1588+
"argument for function given by name ('{}') and position ({})"
1589+
)
1590+
1591+
datetime_components = ["year", "month", "day", "hour", "minute",
1592+
"second", "microsecond", "tzinfo"]
1593+
for i, key in enumerate(datetime_components):
1594+
if args_len > i:
1595+
if key in kwargs:
1596+
raise TypeError(err_msg.format(key, i))
1597+
else:
1598+
break
1599+
15871600
hour = kwargs.get("hour", hour)
15881601
minute = kwargs.get("minute", minute)
15891602
second = kwargs.get("second", second)

pandas/tests/scalar/timestamp/test_constructors.py

+18
Original file line numberDiff line numberDiff line change
@@ -909,3 +909,21 @@ def test_timestamp_constructor_str_invalid_kwargs():
909909
)
910910
with pytest.raises(ValueError, match=msg):
911911
Timestamp("2020-01-01", foo="bar")
912+
913+
914+
@pytest.mark.parametrize(
915+
"kwargs,pos_offset",
916+
[
917+
({"day": 1}, 2),
918+
({"hour": 1}, 3),
919+
],
920+
)
921+
def test_timestamp_constructor_positional_arg_kwarg_conflict(kwargs, pos_offset):
922+
# Check that we didn't pass anything except
923+
# tz, tzinfo, unit with a string
924+
msg = (
925+
f"argument for function given by name \\('{next(iter(kwargs.keys()))}'\\) "
926+
f"and position \\({pos_offset}\\)"
927+
)
928+
with pytest.raises(TypeError, match=msg):
929+
Timestamp(2020, 1, 1, 1, **kwargs)

0 commit comments

Comments
 (0)