astral-sh / astral-sh/ruff

False-positive `DTZ007`: The use of `datetime.datetime.strptime()` without %z must be followed by `.replace(tzinfo=)`

Open
#1,306 11 comments 4 reactions 0 assignees View on GitHub
type-inference wontfix
Dominant language
Rust
Stars
49.6k
Forks
2.4k
Avg merge
2d 1h
Merged PRs (30d)
445

Description

```python
import datetime

DT_FORMAT = "%a, %d %b %Y %H:%M:%S %z"
file_data = "Fri, 23 Sep 2022 08:42:36 +0000"

parsed_date = datetime.datetime.strptime(
file_data, DT_FORMAT
)
```

```console
$ ruff test_ruff_DTZ007.py
Found 1 error(s).
test_ruff_DTZ007.py:7:15: DTZ007 The use of `datetime.datetime.strptime()` without %z must be followed by `.replace(tzinfo=)`
```

Contributor guide

Open the contributing guide

Research direction

Start by running the provided `ruff test_ruff_DTZ007.py` reproduction and inspect the DTZ007 rule that reports the example. The change is done when a `strptime()` call whose format contains `%z` no longer produces a false-positive DTZ007 diagnostic, while the existing warning remains for formats without `%z`.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.