Skip to content

instance() fails for datetimes with pytz.FixedOffsets #807

@patrickmckenna

Description

@patrickmckenna
  • OS version and name: Linux-6.6.12-linuxkit-aarch64-with-glibc2.36
  • Pendulum version: 3.0.0

Issue

pendulum.instance() errors if passed a datetime.datetime where tzinfo is a pytz._FixedOffset() instance, e.g.

>>> pendulum.instance(datetime.datetime(2021, 2, 3, tzinfo=pytz.FixedOffset(60))
... 
... )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/prefect/.venv/lib/python3.12/site-packages/pendulum/__init__.py", line 244, in instance
    return DateTime.instance(obj, tz=tz)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/prefect/.venv/lib/python3.12/site-packages/pendulum/datetime.py", line 133, in instance
    tz = pendulum._safe_timezone(tz, dt=dt)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/prefect/.venv/lib/python3.12/site-packages/pendulum/__init__.py", line 124, in _safe_timezone
    return timezone(obj)
           ^^^^^^^^^^^^^
  File "/opt/prefect/.venv/lib/python3.12/site-packages/pendulum/__init__.py", line 83, in timezone
    if name.lower() == "utc":
       ^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'lower'

The error seems to originate here: https://github.com/sdispater/pendulum/blob/3e3fec6a5b66759078c5ae39c736ca95b78adbef/src/pendulum/__init__.py#L110-L111

pytz._FixedOffset does define a localize() method, but it also explicitly sets zone = None. (I'm not familiar enough with pytz, or datetime.tzinfo, conventions to have anything useful to say about whether this is surprising.)

FWIW, pendulum.UTC.convert() does handle pytz._FixedOffset properly. Unsure whether the fix is to reuse that same logic in _safe_timezone()?

Also, #158 did add some logic to handle pytz offsets, but it appears test cases using pytz.FixedOffset instances might have been removed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions