Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False-positive E0:601 on datetime when USE_TYPECHECKING #9493

Closed
Kein opened this issue Mar 9, 2024 · 1 comment
Closed

False-positive E0:601 on datetime when USE_TYPECHECKING #9493

Kein opened this issue Mar 9, 2024 · 1 comment
Labels
Invalid Not a bug, already exists or already fixed

Comments

@Kein
Copy link

Kein commented Mar 9, 2024

Bug description

from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from datetime import datetime

def _do_thing(dob: str) -> bool:
    dateBirth = datetime.strptime(dob, "%d.%m.%Y")
    return True

Configuration

--disable=broad-except, fixme, unsupported-binary-operation, multiple-imports, global-statement, relative-beyond-top-level,superfluous-parens,import-error,missing-function-docstring,invalid-name,line-too-long,missing-class-docstring,unused-variable,unused-argument,multiple-statements"

Command used

N/A

Pylint output

[{
	"resource": "/d:/Unlinked//1.py",
	"owner": "_generated_diagnostic_collection_name_#11",
	"code": {
		"value": "E0601:used-before-assignment",
		"target": {
			"$mid": 1,
			"path": "/en/latest/user_guide/messages/error/used-before-assignment.html",
			"scheme": "https",
			"authority": "pylint.readthedocs.io"
		}
	},
	"severity": 8,
	"message": "Using variable 'datetime' before assignment",
	"source": "Pylint",
	"startLineNumber": 7,
	"startColumn": 17,
	"endLineNumber": 7,
	"endColumn": 25
}]

Expected behavior

No error message.

Pylint version

pylint 3.1.0: https://github.com/microsoft/vscode-pylint

OS / Environment

Windows 10

Additional dependencies

No response

@Kein Kein added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Mar 9, 2024
@jacobtylerwalls
Copy link
Member

Thanks for the report, but this is intentional. If I add a call to that function at the end of your script, a NameError is raised:

% python3 a.py
Traceback (most recent call last):
  File "/Users/jwalls/a.py", line 10, in <module>
    _do_thing('')
  File "/Users/jwalls/a.py", line 7, in _do_thing
    dateBirth = datetime.strptime(dob, "%d.%m.%Y")
                ^^^^^^^^
NameError: name 'datetime' is not defined. Did you forget to import 'datetime'?

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Mar 9, 2024
@jacobtylerwalls jacobtylerwalls added Invalid Not a bug, already exists or already fixed and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Mar 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Invalid Not a bug, already exists or already fixed
Projects
None yet
Development

No branches or pull requests

2 participants