StaticTzInfo has localize() but not normalize(), so pytz-detecting code crashes on it

Aperta Adatta ai principianti
#1,591 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

  • #1592 di @Aschenbath — chiusa senza merge
  • #1597 di @seragAhmed — chiusa senza merge
  • #1602 di @draarivpatel-ui — chiusa senza merge
  • #1603 di @draarivpatel-ui — chiusa senza merge

Valutazione

Difficoltà
1/5
Tempo stimato
1-3 ore
Idoneità per principianti
70/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia in dateparser/timezone_parser.py alla classe StaticTzInfo, accanto al suo metodo esistente localize(); è l'unica classe che il report nomina. Aggiungi un normalize() che restituisce il datetime convertito in questo offset, corrispondendo alle zone a offset fisso di pytz, e solleva ValueError per un datetime naive come fa lo snippet suggerito. Copri entrambi i casi nei test del parser dei fusi orari sotto tests/ ed eseguili; fatto significa che lo snippet di riproduzione non solleva più AttributeError. Nota che due pull request precedenti sono state chiuse senza essere unite, quindi controlla il thread prima di iniziare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Slightly related to #1590. When dateparser creates a pytz-lookalike TZ-object, it would probably be nice to add a .normalize() (which would be a noop for a static offset TZ), as downstream callers may expect it. Of course, moving to zoneinfo would make this issue moot. As far as I can understand, StaticTzInfo is a perfectly valid Tz-object, so it's also a perfectly valid option to just close this as "not a dateparser problem".

The details below the line are generated by Claude Opus 5.5, but I think I've summarized the issue pretty well already - so if you don't enjoy reading AI-generated text blobs, ignore the rest.


Describe the bug

For an explicit UTC offset or an abbreviation, dateparser returns a dateparser.timezone_parser.StaticTzInfo. It implements pytz's localize() but not normalize(). Libraries that detect pytz by duck typing (hasattr(tz, "localize"), as dateparser's own localize_timezone() does) then call normalize() and crash. This happened in recurring_ical_events: https://github.com/niccokunzmann/python-recurring-ical-events/issues/311

To Reproduce

import dateparser
dt = dateparser.parse("2026-11-21T05:00+01:00")
print(type(dt.tzinfo))           # <class 'dateparser.timezone_parser.StaticTzInfo'>
dt.tzinfo.localize               # exists
dt.tzinfo.normalize(dt)          # AttributeError: 'StaticTzInfo' object has no attribute 'normalize'

dateparser 1.5.1, Python 3.14, Arch Linux.

Suggested fix

Complete the imitation with the method pytz's own fixed-offset zones have:

    def normalize(self, dt: datetime) -> datetime:
        if dt.tzinfo is None:
            raise ValueError("Naive time - no tzinfo set")
        return dt.astimezone(self)

The alternative, returning a stdlib datetime.timezone(offset, name) for fixed offsets, avoids the pytz imitation altogether, but breaks callers relying on StaticTzInfo.localize(); that fits better with a general move away from pytz (#1590).

I can send a PR with a test for the normalize() method if wanted.

Lingua principale
Python
Stelle
2.9k
Fork
545
Merge medio
2g 4h
PR unite (30g)
17

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di scrapinghub/dateparser

Tutte le issue di scrapinghub/dateparser

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.