Skip to content

Commit 0436390

Browse files
authored
template: Add flake8-datetimez (#506)
The `flake8-datetimez` plugin is a Flake8 extension that makes sure no functions that create naive datetime objects (without a timezone) are not used. Fixes #140.
2 parents 59a64dc + d25fbf7 commit 0436390

9 files changed

Lines changed: 46 additions & 0 deletions

File tree

RELEASE_NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ But you might still need to adapt your code:
3636
- `dco-merge-queue.yml`: `DCO`
3737
- `labeler.yml`: `Label`
3838

39+
- Added the [`flake8-datetimez`](https://github.com/pjknkda/flake8-datetimez) plugin to the `flake8` session. This plugin prevents accidental use of naive `datetime` objects by flagging calls that create or return datetimes without timezone information.
40+
3941
## Bug Fixes
4042

4143
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->

cookiecutter/migrate.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def main() -> None:
4343
print("Migrating pyproject license metadata to SPDX format...")
4444
migrate_pyproject_license()
4545
print("=" * 72)
46+
print("Adding flake8-datetimez plugin to dev-flake8 dependencies...")
47+
migrate_add_flake8_datetimez()
48+
print("=" * 72)
4649
print()
4750

4851
if _manual_steps:
@@ -298,6 +301,40 @@ def migrate_pyproject_license() -> None: # pylint: disable=too-many-branches
298301
print(" Updated pyproject.toml: migrated license metadata")
299302

300303

304+
def migrate_add_flake8_datetimez() -> None:
305+
"""Add the flake8-datetimez plugin to dev-flake8 dependencies."""
306+
pyproject_path = Path("pyproject.toml")
307+
if not pyproject_path.exists():
308+
print(" Skipping pyproject.toml (file not found)")
309+
return
310+
311+
content = pyproject_path.read_text(encoding="utf-8")
312+
313+
if "flake8-datetimez" in content:
314+
print(" Skipped pyproject.toml (flake8-datetimez already present)")
315+
return
316+
317+
# Look for a pinned flake8 dependency line (e.g. "flake8 == 7.3.0") and
318+
# insert flake8-datetimez right after it.
319+
match = re.search(r'( "flake8\s*==.*",?\n)', content)
320+
if not match:
321+
manual_step(
322+
"Could not find a flake8 pin in pyproject.toml. "
323+
'Please add `"flake8-datetimez == 20.10.0"` to the '
324+
"`dev-flake8` optional dependencies."
325+
)
326+
return
327+
328+
flake8_line = match.group(1)
329+
new_content = content.replace(
330+
flake8_line,
331+
flake8_line + ' "flake8-datetimez == 20.10.0",\n',
332+
1,
333+
)
334+
replace_file_contents_atomically(pyproject_path, content, new_content, count=1)
335+
print(" Updated pyproject.toml: added flake8-datetimez plugin")
336+
337+
301338
def read_project_type() -> str | None:
302339
"""Read the cookiecutter project type from the replay file."""
303340
replay_path = Path(".cookiecutter-replay.json")

cookiecutter/{{cookiecutter.github_repo_name}}/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ email = "{{cookiecutter.author_email}}"
8787
[project.optional-dependencies]
8888
dev-flake8 = [
8989
"flake8 == 7.3.0",
90+
"flake8-datetimez == 20.10.0",
9091
"flake8-docstrings == 1.7.0",
9192
"flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
9293
"pydoclint == 0.6.10",

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ extra-lint-examples = [
6969
]
7070
dev-flake8 = [
7171
"flake8 == 7.3.0",
72+
"flake8-datetimez == 20.10.0",
7273
"flake8-docstrings == 1.7.0",
7374
"flake8-pyproject == 1.2.4", # For reading the flake8 config from pyproject.toml
7475
"pydoclint == 0.8.3",

tests_golden/integration/test_cookiecutter_generation/actor/frequenz-actor-test/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ email = "floss@frequenz.com"
4444
[project.optional-dependencies]
4545
dev-flake8 = [
4646
"flake8 == 7.3.0",
47+
"flake8-datetimez == 20.10.0",
4748
"flake8-docstrings == 1.7.0",
4849
"flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
4950
"pydoclint == 0.6.10",

tests_golden/integration/test_cookiecutter_generation/api/frequenz-api-test/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ email = "floss@frequenz.com"
5555
[project.optional-dependencies]
5656
dev-flake8 = [
5757
"flake8 == 7.3.0",
58+
"flake8-datetimez == 20.10.0",
5859
"flake8-docstrings == 1.7.0",
5960
"flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
6061
"pydoclint == 0.6.10",

tests_golden/integration/test_cookiecutter_generation/app/frequenz-app-test/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ email = "floss@frequenz.com"
4343
[project.optional-dependencies]
4444
dev-flake8 = [
4545
"flake8 == 7.3.0",
46+
"flake8-datetimez == 20.10.0",
4647
"flake8-docstrings == 1.7.0",
4748
"flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
4849
"pydoclint == 0.6.10",

tests_golden/integration/test_cookiecutter_generation/lib/frequenz-test-python/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ email = "floss@frequenz.com"
4040
[project.optional-dependencies]
4141
dev-flake8 = [
4242
"flake8 == 7.3.0",
43+
"flake8-datetimez == 20.10.0",
4344
"flake8-docstrings == 1.7.0",
4445
"flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
4546
"pydoclint == 0.6.10",

tests_golden/integration/test_cookiecutter_generation/model/frequenz-model-test/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ email = "floss@frequenz.com"
4444
[project.optional-dependencies]
4545
dev-flake8 = [
4646
"flake8 == 7.3.0",
47+
"flake8-datetimez == 20.10.0",
4748
"flake8-docstrings == 1.7.0",
4849
"flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
4950
"pydoclint == 0.6.10",

0 commit comments

Comments
 (0)