pendulum icon indicating copy to clipboard operation
pendulum copied to clipboard

AttributeError: 'NoneType' object has no attribute 'convert' with ZoneInfo

Open taosharma opened this issue 2 years ago • 2 comments

  • [x] I am on the latest Pendulum version.
  • [x] I have searched the issues of this repo and believe that this is not a duplicate.
  • OS version and name: 4.19.128-microsoft-standard Linux
  • Pendulum version: 2.1.2

Issue

Reproducible example

import pendulum
from zoneinfo import ZoneInfo

tz = ZoneInfo("Europe/London")
my_datetime = pendulum.now()

my_datetime.astimezone(tz)  # AttributeError: 'NoneType' object has no attribute 'convert'

Description

The above error seems to be thrown when calling pendulum.astimezone() with a ZoneInfo timezone. We are in the process of migrating a project from pytz to zoneinfo. The above example works perfectly if pendulum is replaced with datetime.datetime.

Thanks for reading and looking into this!

Expected behaviour

For pendulum.astimezone() to successfully convert a datetime object with a ZoneInfo timezone.

Stack trace

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 my_datetime.astimezone(tz)

File ~/.cache/pypoetry/virtualenvs/vidacycle_webapps-foslGWoH-py3.9/lib/python3.9/site-packages/pendulum/datetime.py:1467, in DateTime.astimezone(self, tz)
   1466 def astimezone(self, tz=None):
-> 1467     return pendulum.instance(super(DateTime, self).astimezone(tz))

File ~/.cache/pypoetry/virtualenvs/vidacycle_webapps-foslGWoH-py3.9/lib/python3.9/site-packages/pendulum/datetime.py:1443, in DateTime.__add__(self, other)
   1440 if not isinstance(other, datetime.timedelta):
   1441     return NotImplemented
-> 1443 return self._add_timedelta_(other)

File ~/.cache/pypoetry/virtualenvs/vidacycle_webapps-foslGWoH-py3.9/lib/python3.9/site-packages/pendulum/datetime.py:760, in DateTime._add_timedelta_(self, delta)
    755 elif isinstance(delta, pendulum.Duration):
    756     return self.add(
    757         years=delta.years, months=delta.months, seconds=delta._total
    758     )
--> 760 return self.add(seconds=delta.total_seconds())

File ~/.cache/pypoetry/virtualenvs/vidacycle_webapps-foslGWoH-py3.9/lib/python3.9/site-packages/pendulum/datetime.py:667, in DateTime.add(self, years, months, weeks, days, hours, minutes, seconds, microseconds)
    645     return pendulum.datetime(
    646         dt.year,
    647         dt.month,
   (...)
    653         tz=self.tz,
    654     )
    656 dt = self.__class__(
    657     dt.year,
    658     dt.month,
   (...)
    664     tzinfo=UTC,
    665 )
--> 667 dt = self.tz.convert(dt)
    669 return self.__class__(
    670     dt.year,
    671     dt.month,
   (...)
    678     fold=dt.fold,
    679 )

AttributeError: 'NoneType' object has no attribute 'convert'

taosharma avatar Aug 26 '22 14:08 taosharma

I stubbed by toes hard on this bug, and here it's been fixed for 8 months. Would be supremely grateful for a new release. :bowing_man:

thinkwelltwd avatar Aug 29 '22 15:08 thinkwelltwd

this happens also when upgrading Django 3.x to 4.x since Django 4.x is using zoneinfo as default timezone implementation. just following this to know when its fixed

muhamadBrew avatar Jul 03 '23 10:07 muhamadBrew