Title: Keep naive datetimes naive in DateTime.diff() (fixes #880) by mokashang · Pull Request #968 · python-pendulum/pendulum · GitHub
Open Graph Title: Keep naive datetimes naive in DateTime.diff() (fixes #880) by mokashang · Pull Request #968 · python-pendulum/pendulum
X Title: Keep naive datetimes naive in DateTime.diff() (fixes #880) by mokashang · Pull Request #968 · python-pendulum/pendulum
Description: Pull Request Check List Added tests for changed code. Updated documentation for changed code. Summary DateTime.diff() raises a TypeError when a naive DateTime is diffed against a naive stdlib datetime: from datetime import datetime import pendulum pendulum.today().naive().diff(datetime.today()) # TypeError: can't compare offset-naive and offset-aware datetimes Root cause diff() builds an Interval(self, dt). Interval.__new__ validates that both endpoints have the same awareness (both naive or both aware) and otherwise raises. Interval.__init__, however, re-normalises the endpoints through pendulum.instance() using the default tz=UTC. For a naive stdlib datetime that turns it into a UTC-aware DateTime, so a naive receiver and the (now aware) argument end up mixed, and the subsequent start > end / precise_diff comparison raises. Fix Pass each endpoint's own tzinfo to instance() instead of letting it default to UTC: pendulum.instance(start, tz=start.tzinfo) pendulum.instance(end, tz=end.tzinfo) DateTime.instance resolves the timezone as tz = dt.tzinfo or tz, so: naive inputs (tzinfo is None) stay naive — fixing the bug, and aware inputs keep their existing timezone — identical to the previous behaviour. Because __new__ already guarantees both endpoints share the same awareness, forcing a timezone in __init__ was unnecessary and was the sole source of the mismatch. The mixed naive/aware case still raises in __new__ exactly as before. Testing Added test_diff_naive_with_naive_stdlib_datetime covering the reported case (both abs=True and abs=False). Full test suite passes locally (1834 passed, 3 skipped). ruff check and ruff format --check pass on the changed files. Fixes #880
Open Graph Description: Pull Request Check List Added tests for changed code. Updated documentation for changed code. Summary DateTime.diff() raises a TypeError when a naive DateTime is diffed against a naive stdlib d...
X Description: Pull Request Check List Added tests for changed code. Updated documentation for changed code. Summary DateTime.diff() raises a TypeError when a naive DateTime is diffed against a naive stdlib d...
Opengraph URL: https://github.com/python-pendulum/pendulum/pull/968
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:bd0b64a3-44a6-81e6-3d41-73152283dd8a |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 81D2:1E3B91:3B61C4:5198BC:6B046D0F |
| html-safe-nonce | e77dca57abaecdca02cb618b00dfbd2371ba1068119d657e15df9cb712c5ed47 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MUQyOjFFM0I5MTozQjYxQzQ6NTE5OEJDOjZCMDQ2RDBGIiwidmlzaXRvcl9pZCI6IjY3NzUyNTgxNzMzMzIwMjQ1OTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2b60b10184686d4f3df6a460accd83e88596ac96a9d52d544749d17c9b6180be |
| hovercard-subject-tag | pull_request:3735507175 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python-pendulum/pendulum/pull/968/files |
| twitter:image | https://avatars.githubusercontent.com/u/64570909?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/64570909?s=400&v=4 |
| og:image:alt | Pull Request Check List Added tests for changed code. Updated documentation for changed code. Summary DateTime.diff() raises a TypeError when a naive DateTime is diffed against a naive stdlib d... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | ae8802492f12d3ba4f6b0d814054e617a2a5616b6327bf8555c10cd5b0dc2a6f |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python-pendulum/pendulum git https://github.com/python-pendulum/pendulum.git |
| octolytics-dimension-user_id | 191041900 |
| octolytics-dimension-user_login | python-pendulum |
| octolytics-dimension-repository_id | 62095504 |
| octolytics-dimension-repository_nwo | python-pendulum/pendulum |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 62095504 |
| octolytics-dimension-repository_network_root_nwo | python-pendulum/pendulum |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | cfb5f24c6e01d85f70804652a5107e5cac03a161 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width