Skip to main content

Shims to make deprecation of pytz easier

Project description

pytz_deprecation_shim: Shims to help you safely remove pytz

pytz has served the Python community well for many years, but it is no longer the best option for providing time zones. pytz has a non-standard interface that is very easy to misuse; this interface was necessary when pytz was created, because datetime had no way to represent ambiguous datetimes, but this was solved in in Python 3.6, which added a fold attribute to datetimes in PEP 495. With the addition of the zoneinfo module in Python 3.9 (PEP 615), there has never been a better time to migrate away from pytz.

However, since pytz time zones are used very differently from a standard tzinfo, and many libraries have built pytz zones into their standard time zone interface (and thus may have users relying on the existence of the localize and normalize methods); this library provides shim classes that are compatible with both PEP 495 and pytz’s interface, to make it easier for libraries to deprecate pytz.

Usage

This library is intended for temporary usage only, and should allow you to drop your dependency on pytz while also giving your users notice that eventually you will remove support for the pytz-specific interface.

Within your own code, use pytz_deprecation_shim.timezone shims as if they were zoneinfo or dateutil.tz zones — do not use localize or normalize:

>>> import pytz_deprecation_shim as pds
>>> from datetime import datetime, timedelta
>>> LA = pds.timezone("America/Los_Angeles")

>>> dt = datetime(2020, 10, 31, 12, tzinfo=LA)
>>> print(dt)
2020-10-31 12:00:00-07:00

>>> dt.tzname()
'PDT'

Datetime addition will work like normal Python datetime arithmetic, even across a daylight saving time transition:

>>> dt_add = dt + timedelta(days=1)

>>> print(dt_add)
2020-11-01 12:00:00-08:00

>>> dt_add.tzname()
'PST'

However, if you have exposed a time zone to end users who are using localize and/or normalize or any other pytz-specific features (or if you’ve failed to convert some of your own code all the way), those users will see a warning (rather than an exception) when they use those features:

>>> dt = LA.localize(datetime(2020, 10, 31, 12))
<stdin>:1: PytzUsageWarning: The localize method is no longer necessary, as
this time zone supports the fold attribute (PEP 495). For more details on
migrating to a PEP 495-compliant implementation, see
https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html

 >>> print(dt)
2020-10-31 12:00:00-07:00
>>> dt.tzname()
'PDT'

>>> dt_add = LA.normalize(dt + timedelta(days=1))
<stdin>:1: PytzUsageWarning: The normalize method is no longer necessary,
as this time zone supports the fold attribute (PEP 495). For more details
on migrating to a PEP 495-compliant implementation, see
https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html

>>> print(dt_add)
2020-11-01 12:00:00-08:00
>>> dt_add.tzname()
'PST'

For IANA time zones, calling str() on the shim zones (and indeed on pytz and zoneinfo zones as well) returns the IANA key, so end users who would like to actively migrate to a zoneinfo (or backports.zoneinfo) can do so:

>>> from zoneinfo import ZoneInfo
>>> LA = pds.timezone("America/Los_Angeles")
>>> LA_zi = ZoneInfo(str(LA))
>>> print(LA_zi)
zoneinfo.ZoneInfo(key='America/Los_Angeles')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytz_deprecation_shim-0.1.0.tar.gz (60.1 kB view details)

Uploaded Source

Built Distribution

pytz_deprecation_shim-0.1.0-py2.py3-none-any.whl (15.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pytz_deprecation_shim-0.1.0.tar.gz.

File metadata

  • Download URL: pytz_deprecation_shim-0.1.0.tar.gz
  • Upload date:
  • Size: 60.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for pytz_deprecation_shim-0.1.0.tar.gz
Algorithm Hash digest
SHA256 37ea60c0ecb98bad0ff895c9568649b56d1a480b2b2d9770a320b67ac01d3dce
MD5 fdebf21df8742c0c7feb31f6fd337b01
BLAKE2b-256 71bfe0d1dfdc74c48dddb87e865d240bdbd47d777fb99da613f057cacb53b844

See more details on using hashes here.

File details

Details for the file pytz_deprecation_shim-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: pytz_deprecation_shim-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for pytz_deprecation_shim-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4b4adc9952240b638de66a0a4d040050fcb8b48112619e38bbd58ead83de5506
MD5 88c0b35570291eaee2fb4e816f1e0a66
BLAKE2b-256 d3998e6c2e788d301eba74fb911f2531f03e8064f9dbcd149ebb53ddef81fd4a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page