-
-
Notifications
You must be signed in to change notification settings - Fork 384
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
AttributeError: 'NoneType' object has no attribute 'convert' #527
Comments
I have the same issue using pendulum 2.1.2. I believe this issue was also reported in #160 ( 2017 closed ) and #472 ( 2020 open ). Assigning datetime.timezone.utc in astimezone, the timezone is not the expected datetime.timezone type, but a pendulum.tz.timezone.FixedTimezone. The pendulum.Datetime then throws exceptions in operations including adding a datetime.timedelta, operations that behave as expected in datetime.
import datetime
import pendulum
epoch = '2021-04-30T14:34:56'
date = datetime.datetime.strptime( epoch, '%Y-%m-%dT%H:%M:%S' )
print( f'Date = {date.isoformat( )} Timezone = {date.tzinfo}' )
date = date.astimezone( datetime.timezone.utc )
print( f'Date = {date.isoformat( )} Timezone = {date.tzinfo}' )
date += datetime.timedelta( seconds = 1 )
date = pendulum.parse( epoch )
print( f'Date = {date.isoformat( )} Timezone = {date.tzinfo}' )
date = date.astimezone( datetime.timezone.utc ) # Problem.
print( f'Date = {date.isoformat( )} Timezone = {date.tzinfo}' )
try :
date += datetime.timedelta( seconds = 1 )
except Exception as ex :
print( f'Exception = {ex.__class__.__name__} {ex}' )
|
Pendulum version: 2.1.2
You get:
I'd like to use |
@Q-back s answer, but modified for the |
AttributeError: 'NoneType' object has no attribute 'convert' python-pendulum#527
edit: see my updated code in a more recent comment
|
I have encountered this issue when trying to convert a project from I would like to add to this that on the latest alpha and on this project's master branch, this behavior seems to be broken in a different way. The subtraction no longer raises an exception, but the result no longer seems to have any timezone information whatsoever. It seems to be a naive datetime object, in UTC (implicit). >>> import pendulum
>>> import datetime
>>> now_in_some_non_local_non_utc_timezone = pendulum.DateTime.now(tz=datetime.timezone.max)
>>> now_in_some_non_local_non_utc_timezone
DateTime(2023, 9, 22, 12, 29, 3, 536950, tzinfo=FixedTimezone(86340, name="+23:59"))
>>> now_in_some_non_local_non_utc_timezone.tz
FixedTimezone(86340, name="+23:59")
>>> now_in_some_non_local_non_utc_timezone.tzinfo
FixedTimezone(86340, name="+23:59")
>>> now_in_local_timezone = now_in_some_non_local_non_utc_timezone.astimezone()
>>> now_in_local_timezone
DateTime(2023, 9, 21, 14, 30, 3, 536950, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200), 'CEST'))
>>> now_in_local_timezone.tz
>>> now_in_local_timezone.tzinfo
datetime.timezone(datetime.timedelta(seconds=7200), 'CEST')
>>> one_second_ago_in_local_timezone = now_in_local_timezone - pendulum.Duration(seconds=1)
>>> one_second_ago_in_local_timezone
DateTime(2023, 9, 21, 12, 30, 2, 536950)
>>> one_second_ago_in_local_timezone.tz
>>> one_second_ago_in_local_timezone.tzinfo
>>> one_second_ago_in_local_timezone < now_in_local_timezone
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't compare offset-naive and offset-aware datetimes |
followup to my fixup above, I've encountered other situations so, here's the monkey patch. this one includes:
#
# https://github.com/sdispater/pendulum/issues/527
#
import logging
import pendulum
import traceback
from pendulum.datetime import DateTime
from pendulum.tz.timezone import FixedTimezone, Timezone
logger = logging.getLogger()
logger.warning('monkey patching pendulum.datetime.DateTime')
def monkey_timezone(self): # type: () -> Optional[Timezone]
if not isinstance(self.tzinfo, Timezone):
if hasattr(self.tzinfo, 'key'):
# zoneinfo timezone
logger.info(f'looking up monkey_timezone(zoneinfo) using tzinfo.key {self.tzinfo.key}')
return pendulum.timezone(self.tzinfo.key)
elif hasattr(self.tzinfo, 'name'):
# pendulum Timezone
logger.info(f'looking up monkey_timezone(pendulum) using tzinfo.name {self.tzinfo.name}')
if self.tzinfo.name == '+00:00':
return pendulum.timezone('UTC')
else:
return pendulum.timezone(self.tzinfo.name)
elif hasattr(self.tzinfo, 'zone'):
# pytz
logger.info(f'looking up monkey_timezone(pytz) using tzinfo.zone {self.tzinfo.zone}')
return pendulum.timezone(self.tzinfo.zone)
elif isinstance(self.tzinfo, FixedTimezone):
# pendulum.FixedTimezone
logger.info(f'looking up monkey_timezone(FixedTimezone), probably naive object {self.tzinfo}')
return self.tzinfo
logger.info(f'uhoh, monkey_timezone(naive?) simply returning tzinfo {type(self.tzinfo)}')
stack = traceback.format_stack(limit=6)
stack = ''.join([x for x in stack if 'venv' not in x and 'monkey' not in x])
logger.info(stack)
return self.tzinfo
DateTime.timezone = property(monkey_timezone) |
Issue
It's highly probable to break the adding/substracting time feature after messing with the timezone. And the error is very confusing.
How to reproduce
Run the following script:
Exception
How to fix
Go to https://github.com/sdispater/pendulum/blob/2.1.2/pendulum/datetime.py#L225
and change the line to something like
return pendulum.timezone(self.tzinfo)
. Try to cast it to pendulum's timezone.The story behind a bug
.as_timezone
with somepytz.UTC
data somewhere like infields.py
The text was updated successfully, but these errors were encountered: