Skip to content

Commit

Permalink
Python 3.12 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
scanny committed May 1, 2024
1 parent 4cbbdab commit 4762810
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 39 deletions.
18 changes: 9 additions & 9 deletions features/steps/coreprops.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Gherkin step implementations for core properties-related features."""

from datetime import datetime, timedelta
import datetime as dt

from behave import given, then, when
from behave.runner import Context
Expand Down Expand Up @@ -38,13 +38,13 @@ def when_I_assign_new_values_to_the_properties(context: Context):
("category", "Category"),
("comments", "Description"),
("content_status", "Content Status"),
("created", datetime(2013, 6, 15, 12, 34, 56)),
("created", dt.datetime(2013, 6, 15, 12, 34, 56, tzinfo=dt.timezone.utc)),
("identifier", "Identifier"),
("keywords", "key; word; keyword"),
("language", "Language"),
("last_modified_by", "Last Modified By"),
("last_printed", datetime(2013, 6, 15, 12, 34, 56)),
("modified", datetime(2013, 6, 15, 12, 34, 56)),
("last_printed", dt.datetime(2013, 6, 15, 12, 34, 56, tzinfo=dt.timezone.utc)),
("modified", dt.datetime(2013, 6, 15, 12, 34, 56, tzinfo=dt.timezone.utc)),
("revision", 9),
("subject", "Subject"),
("title", "Title"),
Expand All @@ -66,8 +66,8 @@ def then_a_core_properties_part_with_default_values_is_added(context: Context):
assert core_properties.revision == 1
# core_properties.modified only stores time with seconds resolution, so
# comparison needs to be a little loose (within two seconds)
modified_timedelta = datetime.utcnow() - core_properties.modified
max_expected_timedelta = timedelta(seconds=2)
modified_timedelta = dt.datetime.now(dt.timezone.utc) - core_properties.modified
max_expected_timedelta = dt.timedelta(seconds=2)
assert modified_timedelta < max_expected_timedelta


Expand All @@ -85,13 +85,13 @@ def then_the_core_property_values_match_the_known_values(context: Context):
("category", "Category"),
("comments", "Description"),
("content_status", "Content Status"),
("created", datetime(2014, 12, 13, 22, 2, 0)),
("created", dt.datetime(2014, 12, 13, 22, 2, 0, tzinfo=dt.timezone.utc)),
("identifier", "Identifier"),
("keywords", "key; word; keyword"),
("language", "Language"),
("last_modified_by", "Steve Canny"),
("last_printed", datetime(2014, 12, 13, 22, 2, 42)),
("modified", datetime(2014, 12, 13, 22, 6, 0)),
("last_printed", dt.datetime(2014, 12, 13, 22, 2, 42, tzinfo=dt.timezone.utc)),
("modified", dt.datetime(2014, 12, 13, 22, 6, 0, tzinfo=dt.timezone.utc)),
("revision", 2),
("subject", "Subject"),
("title", "Title"),
Expand Down
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ Repository = "https://github.com/python-openxml/python-docx"
line-length = 100
target-version = ["py37", "py38", "py39", "py310", "py311"]

[tool.pyright]
include = ["src/docx", "tests"]
pythonPlatform = "All"
pythonVersion = "3.8"
reportImportCycles = true
reportUnnecessaryCast = true
reportUnnecessaryTypeIgnoreComment = true
stubPath = "./typings"
typeCheckingMode = "strict"
verboseOutput = true

[tool.pytest.ini_options]
filterwarnings = [
# -- exit on any warning not explicitly ignored here --
Expand Down
21 changes: 0 additions & 21 deletions pyrightconfig.json

This file was deleted.

2 changes: 1 addition & 1 deletion src/docx/opc/parts/coreprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def default(cls, package: OpcPackage):
core_properties.title = "Word Document"
core_properties.last_modified_by = "python-docx"
core_properties.revision = 1
core_properties.modified = dt.datetime.utcnow()
core_properties.modified = dt.datetime.now(dt.timezone.utc)
return core_properties_part

@property
Expand Down
4 changes: 2 additions & 2 deletions src/docx/oxml/coreprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ def _parse_W3CDTF_to_datetime(cls, w3cdtf_str: str) -> dt.datetime:
tmpl = "could not parse W3CDTF datetime string '%s'"
raise ValueError(tmpl % w3cdtf_str)
if len(offset_str) == 6:
return cls._offset_dt(dt_, offset_str)
return dt_
dt_ = cls._offset_dt(dt_, offset_str)
return dt_.replace(tzinfo=dt.timezone.utc)

def _set_element_datetime(self, prop_name: str, value: dt.datetime):
"""Set date/time value of child element having `prop_name` to `value`."""
Expand Down
9 changes: 6 additions & 3 deletions tests/opc/parts/test_coreprops.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Unit test suite for the docx.opc.parts.coreprops module."""

from datetime import datetime, timedelta
from __future__ import annotations

import datetime as dt

import pytest

Expand Down Expand Up @@ -35,8 +37,9 @@ def it_can_create_a_default_core_properties_part(self, package_: Mock):
assert core_properties.title == "Word Document"
assert core_properties.last_modified_by == "python-docx"
assert core_properties.revision == 1
delta = datetime.utcnow() - core_properties.modified
max_expected_delta = timedelta(seconds=2)
assert core_properties.modified is not None
delta = dt.datetime.now(dt.timezone.utc) - core_properties.modified
max_expected_delta = dt.timedelta(seconds=2)
assert delta < max_expected_delta

# fixtures ---------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions tests/opc/test_coreprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def it_can_change_the_string_property_values(self, prop_name: str, tagname: str,
@pytest.mark.parametrize(
("prop_name", "expected_datetime"),
[
("created", dt.datetime(2012, 11, 17, 16, 37, 40)),
("last_printed", dt.datetime(2014, 6, 4, 4, 28)),
("created", dt.datetime(2012, 11, 17, 16, 37, 40, tzinfo=dt.timezone.utc)),
("last_printed", dt.datetime(2014, 6, 4, 4, 28, tzinfo=dt.timezone.utc)),
("modified", None),
],
)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py38, py39, py310, py311
envlist = py38, py39, py310, py311, py312

[testenv]
deps = -rrequirements-test.txt
Expand Down

0 comments on commit 4762810

Please sign in to comment.