MatthiasValvekens / pyHanko

pyHanko: sign and stamp PDF files
MIT License
483 stars 71 forks source link

Test fail if no timezone is set on system #256

Closed carlsmedstad closed 1 year ago

carlsmedstad commented 1 year ago

Describe the bug

Running the tests on a system with no timezone set makes pyhanko_tests/cli_tests/conftest.py fail:

================================================================== ERRORS ==================================================================
______________________________________________________ ERROR collecting test session _______________________________________________________
/usr/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1050: in _gcd_import
    ???
<frozen importlib._bootstrap>:1027: in _find_and_load
    ???
<frozen importlib._bootstrap>:1006: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:688: in _load_unlocked
    ???
/usr/lib/python3.10/site-packages/_pytest/assertion/rewrite.py:172: in exec_module
    exec(co, module.__dict__)
pyhanko_tests/cli_tests/conftest.py:52: in <module>
    FREEZE_DT = tzlocal.get_localzone().localize(datetime.datetime(2020, 8, 1))
E   AttributeError: 'datetime.timezone' object has no attribute 'localize'
========================================================= short test summary info ==========================================================
ERROR  - AttributeError: 'datetime.timezone' object has no attribute 'localize'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================= 1 error in 1.45s =============================================================

This is due to the following deprecated use of datetime:

>>> import datetime
>>> import tzlocal
>>> tzlocal.get_localzone().localize(datetime.datetime(2020, 8, 1))
/usr/lib/python3.10/site-packages/tzlocal/unix.py:183: UserWarning: Can not find any timezone configuration, defaulting to UTC.
  warnings.warn("Can not find any timezone configuration, defaulting to UTC.")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'datetime.timezone' object has no attribute 'localize'

On a system with a timezone set, this part still produces a deprecation warning:

>>> import datetime
>>> import tzlocal
>>> FREEZE_DT = tzlocal.get_localzone().localize(datetime.datetime(2020, 8, 1))
<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

See this for more info: https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html

To Reproduce

Running in a docker image is the easiest way to reproduce.

Expected behavior

Tests should pass, regardless of if a timezone is set or not.

Environment (please complete the following information):

MatthiasValvekens commented 1 year ago

Interesting... I guess I assumed that the local zone would default to pytz's version of UTC or something.

I explicitly quashed that deprecation warning because I thought that passing the timezone directly into the datetime init function would produce unexpected results on Python 3.8 (3.9?) and under---burned myself on that one in the past---but I suppose I might have to get to the bottom of that anyway. Worst case, we branch based on the Python version.

Will take a look; thanks!