https://github.com/marcgibbons/flake8-datetime-import
A plugin for flake8 which enforces importing `datetime as dt` and `time as tm`.
https://github.com/marcgibbons/flake8-datetime-import
Last synced: about 2 months ago
JSON representation
A plugin for flake8 which enforces importing `datetime as dt` and `time as tm`.
- Host: GitHub
- URL: https://github.com/marcgibbons/flake8-datetime-import
- Owner: marcgibbons
- License: mit
- Created: 2022-10-14T01:09:47.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-24T17:03:06.000Z (3 months ago)
- Last Synced: 2025-03-28T08:11:10.822Z (2 months ago)
- Language: Python
- Homepage:
- Size: 65.4 KB
- Stars: 11
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-flake8-extensions - flake8-datetime-import - Enforce importing `datetime as dt` and `time as tm`. (Imports)
README
# flake8-datetime-import
[](https://badge.fury.io/py/flake8-datetime-import)


[](https://codecov.io/gh/marcgibbons/flake8-datetime-import)
[](https://results.pre-commit.ci/latest/github/marcgibbons/flake8-datetime-import/main)`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:```python
import datetime as dt
import time as tm
```## Installation
```bash
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?```python
# Bad
import datetime
from datetime import datetime, time, timezoneimport time
from time import time, timezone
```Consistently importing and aliasing the `datetime` and `time` modules helps
prevent this ambiguity.```python
# Good
import datetime as dt
import time as tmdt.datetime.now()
tm.time()
```Importing and namespacing `datetime` prevents other naming collisions,
such as Django's `django.utils.timezone`:```python
import datetime as dtfrom 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 fatigueOther notable mentions of this importing strategy:
-
-## pre-commit
To use with pre-commit, add `flake8-datetime-import` as an additional
dependency to `flake8`.```yaml
# .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,
]
```