Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test failure on Python 3.12 due to datetime.datetime.utcnow() deprecation #1300

Open
musicinmybrain opened this issue Nov 20, 2023 · 2 comments

Comments

@musicinmybrain
Copy link
Contributor

tox -e py312
================================================= FAILURES =================================================
_________________ DescribeCorePropertiesPart.it_can_create_a_default_core_properties_part __________________

self = <tests.opc.parts.test_coreprops.DescribeCorePropertiesPart object at 0x7f50c936b6e0>

    def it_can_create_a_default_core_properties_part(self):
>       core_properties_part = CorePropertiesPart.default(None)

tests/opc/parts/test_coreprops.py:22: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <class 'docx.opc.parts.coreprops.CorePropertiesPart'>, package = None

    @classmethod
    def default(cls, package):
        """Return a new |CorePropertiesPart| object initialized with default values for
        its base properties."""
        core_properties_part = cls._new(package)
        core_properties = core_properties_part.core_properties
        core_properties.title = "Word Document"
        core_properties.last_modified_by = "python-docx"
        core_properties.revision = 1
>       core_properties.modified = datetime.utcnow()
E       DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).

.tox/py312/lib/python3.12/site-packages/docx/opc/parts/coreprops.py:25: DeprecationWarning
========================================= short test summary info ==========================================
FAILED tests/opc/parts/test_coreprops.py::DescribeCorePropertiesPart::it_can_create_a_default_core_properties_part - DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future vers...
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1 failed, 676 passed in 3.44s
py312: exit 1 (3.66 seconds) /home/ben/src/forks/python-docx> py.test -qx pid=1743764
.pkg: _exit> python /usr/lib/python3.12/site-packages/pyproject_api/_backend.py True setuptools.build_meta
  py312: FAIL code 1 (7.96=setup[4.30]+cmd[3.66] seconds)
  evaluation failed :( (8.02 seconds)

See also:

https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow

python/cpython#103857

https://discuss.python.org/t/deprecating-utcnow-and-utcfromtimestamp/26221/14

@merkys
Copy link

merkys commented Dec 15, 2023

I attempted to replace datetime.datetime.utcnow() calls with datetime.datetime.now(datetime.UTC), but I get the following failure for Python 3.12:

self = <tests.opc.parts.test_coreprops.DescribeCorePropertiesPart object at 0x7f2c29bda960>

    def it_can_create_a_default_core_properties_part(self):
        core_properties_part = CorePropertiesPart.default(None)
        assert isinstance(core_properties_part, CorePropertiesPart)
        core_properties = core_properties_part.core_properties
        assert core_properties.title == "Word Document"
        assert core_properties.last_modified_by == "python-docx"
        assert core_properties.revision == 1
>       delta = datetime.now(UTC) - core_properties.modified
E       TypeError: can't subtract offset-naive and offset-aware datetimes

I have no idea how to fix this, alas.

@msebas
Copy link

msebas commented Mar 1, 2024

I got the test running with delta = datetime.now(UTC).replace(tzinfo=None) - core_properties.modified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants