Hasty Briefsbeta

Bilingual

Why Pendulum had to write the most cursed "+" operator in all of Python

a day ago
  • Pendulum's '+' operator uses the call stack to decide whether to apply DST-aware or standard arithmetic, making it hundreds of times slower than the standard library.
  • This hack stems from a conflict between two promises: DST-aware arithmetic and drop-in compatibility as a subclass of datetime.
  • The call-stack check is fragile, causing bugs with third-party time zones, PyPy, and other unexpected callers.
  • Other Pendulum guesses (e.g., assuming UTC for missing time zones) silently produce incorrect or platform-dependent results.
  • A proposed fix removes the guess by passing a plain datetime to astimezone() and wrapping the result, improving speed and correctness.
  • The underlying issue remains: because Pendulum's DateTime subclasses datetime, any code expecting standard arithmetic may silently get Pendulum's behavior.