Replace deprecated ``datetime`` functions (#3148)

From Python 3.12, datetime.datetime.utcnow and
datetime.datetime.utcfromtimestamp emit deprecation warnings.
This commit is contained in:
Hugo van Kemenade 2023-05-18 03:45:51 +03:00 committed by GitHub
parent 87b4a4c392
commit 9d6e5b653f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 13 deletions

View File

@ -3,7 +3,7 @@
# CC0-1.0-Universal license, whichever is more permissive. # CC0-1.0-Universal license, whichever is more permissive.
import argparse import argparse
import datetime import datetime as dt
import email.utils import email.utils
from html import escape from html import escape
from pathlib import Path from pathlib import Path
@ -19,9 +19,9 @@ from docutils.parsers.rst import roles
PEP_ROOT = Path(__file__).parent PEP_ROOT = Path(__file__).parent
def _format_rfc_2822(dt: datetime.datetime) -> str: def _format_rfc_2822(datetime: dt.datetime) -> str:
dt = dt.replace(tzinfo=datetime.timezone.utc) datetime = datetime.replace(tzinfo=dt.timezone.utc)
return email.utils.format_datetime(dt, usegmt=True) return email.utils.format_datetime(datetime, usegmt=True)
line_cache: dict[Path, dict[str, str]] = {} line_cache: dict[Path, dict[str, str]] = {}
@ -111,12 +111,12 @@ def first_line_starting_with(full_path: Path, text: str) -> str:
return path_cache.get(text, "") return path_cache.get(text, "")
def pep_creation(full_path: Path) -> datetime.datetime: def pep_creation(full_path: Path) -> dt.datetime:
created_str = first_line_starting_with(full_path, "Created:") created_str = first_line_starting_with(full_path, "Created:")
if full_path.stem == "pep-0102": if full_path.stem == "pep-0102":
# remove additional content on the Created line # remove additional content on the Created line
created_str = created_str.split(" ", 1)[0] created_str = created_str.split(" ", 1)[0]
return datetime.datetime.strptime(created_str, "%d-%b-%Y") return dt.datetime.strptime(created_str, "%d-%b-%Y")
def parse_rst(full_path: Path) -> nodes.document: def parse_rst(full_path: Path) -> nodes.document:
@ -150,7 +150,7 @@ def main():
# generate rss items for 10 most recent peps # generate rss items for 10 most recent peps
items = [] items = []
for dt, full_path in peps_with_dt[-10:]: for datetime, full_path in peps_with_dt[-10:]:
try: try:
pep_num = int(full_path.stem.split("-")[-1]) pep_num = int(full_path.stem.split("-")[-1])
except ValueError: except ValueError:
@ -172,7 +172,7 @@ def main():
<description>{escape(pep_abstract(full_path), quote=False)}</description> <description>{escape(pep_abstract(full_path), quote=False)}</description>
<author>{escape(joined_authors, quote=False)}</author> <author>{escape(joined_authors, quote=False)}</author>
<guid isPermaLink="true">{url}</guid> <guid isPermaLink="true">{url}</guid>
<pubDate>{_format_rfc_2822(dt)}</pubDate> <pubDate>{_format_rfc_2822(datetime)}</pubDate>
</item>""" </item>"""
items.append(item) items.append(item)
@ -182,7 +182,7 @@ def main():
language features, and some meta-information like release language features, and some meta-information like release
procedure and schedules. procedure and schedules.
""" """
last_build_date = _format_rfc_2822(datetime.datetime.utcnow()) last_build_date = _format_rfc_2822(dt.datetime.now(dt.timezone.utc))
items = "\n".join(reversed(items)) items = "\n".join(reversed(items))
output = f"""\ output = f"""\
<?xml version='1.0' encoding='UTF-8'?> <?xml version='1.0' encoding='UTF-8'?>

View File

@ -1,4 +1,4 @@
import datetime import datetime as dt
from pathlib import Path from pathlib import Path
import subprocess import subprocess
@ -66,7 +66,8 @@ def _add_commit_history_info(pep_source_path: Path) -> nodes.paragraph:
except KeyError: except KeyError:
return nodes.paragraph() return nodes.paragraph()
iso_time = datetime.datetime.utcfromtimestamp(since_epoch).isoformat(sep=" ") epoch_dt = dt.datetime.fromtimestamp(since_epoch, dt.timezone.utc)
iso_time = epoch_dt.isoformat(sep=" ")
commit_link = f"https://github.com/python/peps/commits/main/{pep_source_path.name}" commit_link = f"https://github.com/python/peps/commits/main/{pep_source_path.name}"
link_node = nodes.reference("", f"{iso_time} GMT", refuri=commit_link) link_node = nodes.reference("", f"{iso_time} GMT", refuri=commit_link)
return nodes.paragraph("", "Last modified: ", link_node) return nodes.paragraph("", "Last modified: ", link_node)

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
import datetime import datetime as dt
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
import unicodedata import unicodedata
@ -32,7 +32,7 @@ if TYPE_CHECKING:
HEADER = f"""\ HEADER = f"""\
PEP: 0 PEP: 0
Title: Index of Python Enhancement Proposals (PEPs) Title: Index of Python Enhancement Proposals (PEPs)
Last-Modified: {datetime.date.today()} Last-Modified: {dt.date.today()}
Author: python-dev <python-dev@python.org> Author: python-dev <python-dev@python.org>
Status: Active Status: Active
Type: Informational Type: Informational