Skip to content

Commit

Permalink
Add compatibility shim for Python 3.9 and earlier.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Apr 28, 2024
1 parent e14070a commit e5c6df7
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/towncrier/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@

from __future__ import annotations

import sys

from pathlib import Path
from typing import Any


if sys.version_info < (3, 10):

def _newline_write_text(path: Path, content: str, **kwargs: Any) -> None:
with path.open("w", **kwargs) as strm:
strm.write(content)

else:

def _newline_write_text(path: Path, content: str, **kwargs: Any) -> None:
path.write_text(content, **kwargs)


def append_to_newsfile(
Expand Down Expand Up @@ -37,7 +52,8 @@ def append_to_newsfile(
if top_line and top_line in prev_body:
raise ValueError("It seems you've already produced newsfiles for this version?")

news_file.write_text(
_newline_write_text(
news_file,
# If there is no previous body that means we're writing a brand new news file.
# We don't want extra whitespace at the end of this new file.
header + (content + prev_body if prev_body else content.rstrip() + "\n"),
Expand Down

0 comments on commit e5c6df7

Please sign in to comment.