Skip to content
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

Fix multiple bugs in arrow.get() tzinfo kwarg handling #968

Merged
merged 6 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions arrow/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,14 @@ def get(self, *args: Any, **kwargs: Any) -> Arrow:
if len(kwargs) == 1 and tz is None:
arg_count = 3

# () -> now, @ utc.
# () -> now, @ tzinfo or utc
if arg_count == 0:
if isinstance(tz, str):
tz = parser.TzinfoParser.parse(tz)
return self.type.now(tz)
return self.type.now(tzinfo=tz)

if isinstance(tz, dt_tzinfo):
return self.type.now(tz)
return self.type.now(tzinfo=tz)

return self.type.utcnow()

Expand All @@ -223,42 +223,42 @@ def get(self, *args: Any, **kwargs: Any) -> Arrow:
if arg is None:
raise TypeError("Cannot parse argument of type None.")

# try (int, float) -> from timestamp with tz
# try (int, float) -> from timestamp @ tzinfo
elif not isinstance(arg, str) and is_timestamp(arg):
if tz is None:
# set to UTC by default
tz = dateutil_tz.tzutc()
return self.type.fromtimestamp(arg, tzinfo=tz)

# (Arrow) -> from the object's datetime.
# (Arrow) -> from the object's datetime @ tzinfo
elif isinstance(arg, Arrow):
return self.type.fromdatetime(arg.datetime)
return self.type.fromdatetime(arg.datetime, tzinfo=tz)

# (datetime) -> from datetime.
# (datetime) -> from datetime @ tzinfo
elif isinstance(arg, datetime):
return self.type.fromdatetime(arg)
return self.type.fromdatetime(arg, tzinfo=tz)

# (date) -> from date.
# (date) -> from date @ tzinfo
elif isinstance(arg, date):
return self.type.fromdate(arg)
return self.type.fromdate(arg, tzinfo=tz)

# (tzinfo) -> now, @ tzinfo.
# (tzinfo) -> now @ tzinfo
elif isinstance(arg, dt_tzinfo):
return self.type.now(arg)
return self.type.now(tzinfo=arg)

# (str) -> parse.
# (str) -> parse @ tzinfo
elif isinstance(arg, str):
dt = parser.DateTimeParser(locale).parse_iso(arg, normalize_whitespace)
return self.type.fromdatetime(dt, tz)
return self.type.fromdatetime(dt, tzinfo=tz)

# (struct_time) -> from struct_time
elif isinstance(arg, struct_time):
return self.type.utcfromtimestamp(calendar.timegm(arg))

# (iso calendar) -> convert then from date
# (iso calendar) -> convert then from date @ tzinfo
elif isinstance(arg, tuple) and len(arg) == 3:
d = iso_to_gregorian(*arg)
return self.type.fromdate(d)
return self.type.fromdate(d, tzinfo=tz)

else:
raise TypeError(f"Cannot parse single argument of type {type(arg)!r}.")
Expand All @@ -269,25 +269,25 @@ def get(self, *args: Any, **kwargs: Any) -> Arrow:

if isinstance(arg_1, datetime):

# (datetime, tzinfo/str) -> fromdatetime replace tzinfo.
# (datetime, tzinfo/str) -> fromdatetime @ tzinfo
if isinstance(arg_2, (dt_tzinfo, str)):
return self.type.fromdatetime(arg_1, arg_2)
return self.type.fromdatetime(arg_1, tzinfo=arg_2)
else:
raise TypeError(
f"Cannot parse two arguments of types 'datetime', {type(arg_2)!r}."
)

elif isinstance(arg_1, date):

# (date, tzinfo/str) -> fromdate replace tzinfo.
# (date, tzinfo/str) -> fromdate @ tzinfo
if isinstance(arg_2, (dt_tzinfo, str)):
return self.type.fromdate(arg_1, tzinfo=arg_2)
else:
raise TypeError(
f"Cannot parse two arguments of types 'date', {type(arg_2)!r}."
)

# (str, format) -> parse.
# (str, format) -> parse @ tzinfo
elif isinstance(arg_1, str) and isinstance(arg_2, (str, list)):
dt = parser.DateTimeParser(locale).parse(
args[0], args[1], normalize_whitespace
Expand All @@ -299,7 +299,7 @@ def get(self, *args: Any, **kwargs: Any) -> Arrow:
f"Cannot parse two arguments of types {type(arg_1)!r} and {type(arg_2)!r}."
)

# 3+ args -> datetime-like via constructor.
# 3+ args -> datetime-like via constructor
else:
return self.type(*args, **kwargs)

Expand Down
41 changes: 41 additions & 0 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from dateutil import tz

from arrow import Arrow
from arrow.parser import ParserError

from .utils import assert_datetime_equality
Expand Down Expand Up @@ -172,6 +173,46 @@ def test_kwarg_normalize_whitespace(self):
2013, 5, 5, 12, 30, 45, 123456, tzinfo=tz.tzutc()
)

# regression test for #944
def test_one_arg_datetime_tzinfo_kwarg(self):

dt = datetime(2021, 4, 29, 6)

result = self.factory.get(dt, tzinfo="America/Chicago")

expected = datetime(2021, 4, 29, 6, tzinfo=tz.gettz("America/Chicago"))

assert_datetime_equality(result._datetime, expected)

def test_one_arg_arrow_tzinfo_kwarg(self):

arw = Arrow(2021, 4, 29, 6)

result = self.factory.get(arw, tzinfo="America/Chicago")

expected = datetime(2021, 4, 29, 6, tzinfo=tz.gettz("America/Chicago"))

assert_datetime_equality(result._datetime, expected)

def test_one_arg_date_tzinfo_kwarg(self):

da = date(2021, 4, 29)

result = self.factory.get(da, tzinfo="America/Chicago")

expected = Arrow(2021, 4, 29, tzinfo=tz.gettz("America/Chicago"))

assert result.date() == expected.date()
assert result.tzinfo == expected.tzinfo

def test_one_arg_iso_calendar_tzinfo_kwarg(self):

result = self.factory.get((2004, 1, 7), tzinfo="America/Chicago")

expected = Arrow(2004, 1, 4, tzinfo="America/Chicago")

assert_datetime_equality(result, expected)

def test_one_arg_iso_str(self):

dt = datetime.utcnow()
Expand Down