Skip to content

Commit

Permalink
chore: Drop dependency on pytz by using stdlib `datetime.timezone.u…
Browse files Browse the repository at this point in the history
…tc` (#721)

* Fix type hints for non-None return

* Use stdlib instead of pytz.utc

---------

Co-authored-by: Athira Sabu <102021496+AsabuHere@users.noreply.github.com>
  • Loading branch information
Zac-HD and AsabuHere authored Oct 4, 2023
1 parent 6d30502 commit c18ba65
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 14 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pygments>=2.7.4 # not directly required, pinned by Snyk to avoid a vulnerability
pytz
requests>=2.0.0
PyJWT>=2.0.0, <3.0.0
aiohttp>=3.8.4
Expand Down
1 change: 0 additions & 1 deletion setup.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
keywords=["twilio", "twiml"],
python_requires=">=3.7.0",
install_requires=[
"pytz",
"requests >= 2.0.0",
"PyJWT >= 2.0.0, < 3.0.0",
"aiohttp>=3.8.4",
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/base/test_deserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import unittest
from decimal import Decimal

import pytz

from twilio.base import deserialize


Expand All @@ -21,7 +19,7 @@ def test_not_parsable(self):
class Iso8601DateTimeTestCase(unittest.TestCase):
def test_parsable(self):
actual = deserialize.iso8601_datetime("2015-01-02T03:04:05Z")
expected = datetime.datetime(2015, 1, 2, 3, 4, 5, 0, pytz.utc)
expected = datetime.datetime(2015, 1, 2, 3, 4, 5, 0, datetime.timezone.utc)
self.assertEqual(expected, actual)

def test_not_parsable(self):
Expand Down
16 changes: 7 additions & 9 deletions twilio/base/deserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
from email.utils import parsedate
from typing import Optional, Union

import pytz

ISO8601_DATE_FORMAT = "%Y-%m-%d"
ISO8601_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"


def iso8601_date(s: str) -> Optional[Union[datetime.date, str]]:
def iso8601_date(s: str) -> Union[datetime.date, str]:
"""
Parses an ISO 8601 date string and returns a UTC date object or the string
if the parsing failed.
Expand All @@ -19,7 +17,7 @@ def iso8601_date(s: str) -> Optional[Union[datetime.date, str]]:
try:
return (
datetime.datetime.strptime(s, ISO8601_DATE_FORMAT)
.replace(tzinfo=pytz.utc)
.replace(tzinfo=datetime.timezone.utc)
.date()
)
except (TypeError, ValueError):
Expand All @@ -28,15 +26,15 @@ def iso8601_date(s: str) -> Optional[Union[datetime.date, str]]:

def iso8601_datetime(
s: str,
) -> Optional[Union[datetime.datetime, str]]:
) -> Union[datetime.datetime, str]:
"""
Parses an ISO 8601 datetime string and returns a UTC datetime object,
or the string if parsing failed.
:param s: ISO 8601-formatted datetime string (2015-01-25T12:34:56Z)
"""
try:
return datetime.datetime.strptime(s, ISO8601_DATETIME_FORMAT).replace(
tzinfo=pytz.utc
tzinfo=datetime.timezone.utc
)
except (TypeError, ValueError):
return s
Expand All @@ -52,10 +50,10 @@ def rfc2822_datetime(s: str) -> Optional[datetime.datetime]:
date_tuple = parsedate(s)
if date_tuple is None:
return None
return datetime.datetime(*date_tuple[:6]).replace(tzinfo=pytz.utc)
return datetime.datetime(*date_tuple[:6]).replace(tzinfo=datetime.timezone.utc)


def decimal(d: Optional[str]) -> Optional[Union[Decimal, str]]:
def decimal(d: Optional[str]) -> Union[Decimal, str]:
"""
Parses a decimal string into a Decimal
:param d: decimal string
Expand All @@ -65,7 +63,7 @@ def decimal(d: Optional[str]) -> Optional[Union[Decimal, str]]:
return Decimal(d, BasicContext)


def integer(i: str) -> Optional[Union[int, str]]:
def integer(i: str) -> Union[int, str]:
"""
Parses an integer string into an int
:param i: integer string
Expand Down

0 comments on commit c18ba65

Please sign in to comment.