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

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

Open
patrickmckenna opened this issue Feb 8, 2024 · 0 comments
Open

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

patrickmckenna opened this issue Feb 8, 2024 · 0 comments

Comments

@patrickmckenna
Copy link

  • 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:

elif hasattr(obj, "localize"):
obj = obj.zone # type: ignore[attr-defined]

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant