Skip to content

Commit 0a8e9c4

Browse files
committed
fix: Python 3.12 fixes
1 parent 4cbbdab commit 0a8e9c4

File tree

8 files changed

+32
-39
lines changed

8 files changed

+32
-39
lines changed

features/steps/coreprops.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Gherkin step implementations for core properties-related features."""
22

3-
from datetime import datetime, timedelta
3+
import datetime as dt
44

55
from behave import given, then, when
66
from behave.runner import Context
@@ -38,13 +38,13 @@ def when_I_assign_new_values_to_the_properties(context: Context):
3838
("category", "Category"),
3939
("comments", "Description"),
4040
("content_status", "Content Status"),
41-
("created", datetime(2013, 6, 15, 12, 34, 56)),
41+
("created", dt.datetime(2013, 6, 15, 12, 34, 56, tzinfo=dt.timezone.utc)),
4242
("identifier", "Identifier"),
4343
("keywords", "key; word; keyword"),
4444
("language", "Language"),
4545
("last_modified_by", "Last Modified By"),
46-
("last_printed", datetime(2013, 6, 15, 12, 34, 56)),
47-
("modified", datetime(2013, 6, 15, 12, 34, 56)),
46+
("last_printed", dt.datetime(2013, 6, 15, 12, 34, 56, tzinfo=dt.timezone.utc)),
47+
("modified", dt.datetime(2013, 6, 15, 12, 34, 56, tzinfo=dt.timezone.utc)),
4848
("revision", 9),
4949
("subject", "Subject"),
5050
("title", "Title"),
@@ -66,8 +66,8 @@ def then_a_core_properties_part_with_default_values_is_added(context: Context):
6666
assert core_properties.revision == 1
6767
# core_properties.modified only stores time with seconds resolution, so
6868
# comparison needs to be a little loose (within two seconds)
69-
modified_timedelta = datetime.utcnow() - core_properties.modified
70-
max_expected_timedelta = timedelta(seconds=2)
69+
modified_timedelta = dt.datetime.now(dt.timezone.utc) - core_properties.modified
70+
max_expected_timedelta = dt.timedelta(seconds=2)
7171
assert modified_timedelta < max_expected_timedelta
7272

7373

@@ -85,13 +85,13 @@ def then_the_core_property_values_match_the_known_values(context: Context):
8585
("category", "Category"),
8686
("comments", "Description"),
8787
("content_status", "Content Status"),
88-
("created", datetime(2014, 12, 13, 22, 2, 0)),
88+
("created", dt.datetime(2014, 12, 13, 22, 2, 0, tzinfo=dt.timezone.utc)),
8989
("identifier", "Identifier"),
9090
("keywords", "key; word; keyword"),
9191
("language", "Language"),
9292
("last_modified_by", "Steve Canny"),
93-
("last_printed", datetime(2014, 12, 13, 22, 2, 42)),
94-
("modified", datetime(2014, 12, 13, 22, 6, 0)),
93+
("last_printed", dt.datetime(2014, 12, 13, 22, 2, 42, tzinfo=dt.timezone.utc)),
94+
("modified", dt.datetime(2014, 12, 13, 22, 6, 0, tzinfo=dt.timezone.utc)),
9595
("revision", 2),
9696
("subject", "Subject"),
9797
("title", "Title"),

pyproject.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ Repository = "https://github.com/python-openxml/python-docx"
4242
line-length = 100
4343
target-version = ["py37", "py38", "py39", "py310", "py311"]
4444

45+
[tool.pyright]
46+
include = ["src/docx", "tests"]
47+
pythonPlatform = "All"
48+
pythonVersion = "3.8"
49+
reportImportCycles = true
50+
reportUnnecessaryCast = true
51+
reportUnnecessaryTypeIgnoreComment = true
52+
stubPath = "./typings"
53+
typeCheckingMode = "strict"
54+
verboseOutput = true
55+
4556
[tool.pytest.ini_options]
4657
filterwarnings = [
4758
# -- exit on any warning not explicitly ignored here --

pyrightconfig.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/docx/opc/parts/coreprops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def default(cls, package: OpcPackage):
3131
core_properties.title = "Word Document"
3232
core_properties.last_modified_by = "python-docx"
3333
core_properties.revision = 1
34-
core_properties.modified = dt.datetime.utcnow()
34+
core_properties.modified = dt.datetime.now(dt.timezone.utc)
3535
return core_properties_part
3636

3737
@property

src/docx/oxml/coreprops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ def _parse_W3CDTF_to_datetime(cls, w3cdtf_str: str) -> dt.datetime:
254254
tmpl = "could not parse W3CDTF datetime string '%s'"
255255
raise ValueError(tmpl % w3cdtf_str)
256256
if len(offset_str) == 6:
257-
return cls._offset_dt(dt_, offset_str)
258-
return dt_
257+
dt_ = cls._offset_dt(dt_, offset_str)
258+
return dt_.replace(tzinfo=dt.timezone.utc)
259259

260260
def _set_element_datetime(self, prop_name: str, value: dt.datetime):
261261
"""Set date/time value of child element having `prop_name` to `value`."""

tests/opc/parts/test_coreprops.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Unit test suite for the docx.opc.parts.coreprops module."""
22

3-
from datetime import datetime, timedelta
3+
from __future__ import annotations
4+
5+
import datetime as dt
46

57
import pytest
68

@@ -35,8 +37,9 @@ def it_can_create_a_default_core_properties_part(self, package_: Mock):
3537
assert core_properties.title == "Word Document"
3638
assert core_properties.last_modified_by == "python-docx"
3739
assert core_properties.revision == 1
38-
delta = datetime.utcnow() - core_properties.modified
39-
max_expected_delta = timedelta(seconds=2)
40+
assert core_properties.modified is not None
41+
delta = dt.datetime.now(dt.timezone.utc) - core_properties.modified
42+
max_expected_delta = dt.timedelta(seconds=2)
4043
assert delta < max_expected_delta
4144

4245
# fixtures ---------------------------------------------

tests/opc/test_coreprops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def it_can_change_the_string_property_values(self, prop_name: str, tagname: str,
6868
@pytest.mark.parametrize(
6969
("prop_name", "expected_datetime"),
7070
[
71-
("created", dt.datetime(2012, 11, 17, 16, 37, 40)),
72-
("last_printed", dt.datetime(2014, 6, 4, 4, 28)),
71+
("created", dt.datetime(2012, 11, 17, 16, 37, 40, tzinfo=dt.timezone.utc)),
72+
("last_printed", dt.datetime(2014, 6, 4, 4, 28, tzinfo=dt.timezone.utc)),
7373
("modified", None),
7474
],
7575
)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py38, py39, py310, py311
2+
envlist = py38, py39, py310, py311, py312
33

44
[testenv]
55
deps = -rrequirements-test.txt

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy