A plugin for flake8 which enforces importing `datetime as dt` and `time as tm`.
Project description
flake8-datetime-import
flake8-datetime-import is an opinionated plugin which aims to reduce
confusing or inconsistent usage of Python's datetime module. It checks that
datetime and time are imported as modules and aliased like:
import datetime as dt
import time as tm
Installation
pip install flake8-datetime-import
flake8 codes
| Code | Description |
|---|---|
| DTI100 | from datetime import ... is not allowed. datetime must be imported as a module. |
| DTI101 | datetime imported without aliasing as dt. Expected import datetime as dt. |
| DTI200 | from time import ... is not allowed. time must be imported as a module. |
| DTI201 | time imported without aliasing as tm. Expected import time as tm. |
Rationale
datetime and time are confusing when encountered in code. Are they modules?
Are they classes or functions?
# Bad
import datetime
from datetime import datetime, time, timezone
import time
from time import time, timezone
Consistently importing and aliasing the datetime and time modules helps
prevent this ambiguity.
# Good
import datetime as dt
import time as tm
dt.datetime.now()
tm.time()
Importing and namespacing datetime prevents other naming collisions,
such as Django's django.utils.timezone:
import datetime as dt
from django.utils import timezone
dt.timezone.utc
timezone.now()
This plugin was inspired by:
- A talk by @brandon-rhodes at PyCon Canada in which he mentioned this importing strategy
- Code review fatigue
Other notable mentions of this importing strategy:
- https://adamj.eu/tech/2019/09/12/how-i-import-pythons-datetime-module/
- https://www.atmos.albany.edu/facstaff/ktyle/pythia/foundations/_build/html/core/datetime/datetime.html
pre-commit
To use with pre-commit, add flake8-datetime-import as an additional
dependency to flake8.
# .pre-commit-config.yml
- repo: https://github.com/pycqa/flake8
rev: 5.0.4
hooks:
- id: flake8
additional_dependencies: [
flake8-datetime-import==0.1.0,
]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flake8-datetime-import-0.1.0.tar.gz.
File metadata
- Download URL: flake8-datetime-import-0.1.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77202d350ce2ac232345b0410df8100bd90796a903cbb4dca390d211c703349d
|
|
| MD5 |
d86e11bdf2d97dec3e29c879fc134391
|
|
| BLAKE2b-256 |
8af05f3e4f219269706c952bdf228648f2086eb5403b76ba8c77aa5aded8a864
|
File details
Details for the file flake8_datetime_import-0.1.0-py3-none-any.whl.
File metadata
- Download URL: flake8_datetime_import-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd7c8da888d77eb194e1194994fd9250e28f4613033da8d081486eb87aaef88c
|
|
| MD5 |
c850b43fc314f2dfb28ae02f2dc693fa
|
|
| BLAKE2b-256 |
d941a3bd1c6a5c7fc619fa5e2a77d858804aa5ff339480196d8d1b7744dd9953
|