-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Ruff emits no F811 diagnostics for this snippet:
import ctypes
import ctypes.wintypesHowever, it emits an F811 diagnostic for this snippet:
import importlib
ctypes = importlib.import_module("ctypes")
import ctypes.wintypesI think Ruff should either emit a diagnostic for both of these, or neither of these, since they're equivalent in terms of what happens with respect to symbol definitions:
- A
ctypessymbol is defined. For the first snippet this is defined via animportstatement, and for the second it is defined via a function call, but this difference shouldn't really be relevant. - The
import ctypes.wintypesstatement causes:- The
ctypesvariable to be reassigned in the global namespace (but to the same object, which is just looked up in thesys.modulescache) - The
ctypes.wintypessubmodule to be loaded and stored as awintypesattribute on thectypesmodule object
- The
I would lean towards not emitting a diagnostic on either of these, because although the submodule import of ctypes.wintypes is strictly-speaking a redefinition of the ctypes variable in both cases, many people consider it unidiomatic to access top-level members from ctypes in a module that only has import ctypes.wintypes in it. (It's sort-of an "implicit import" of the top-level ctypes module.)
To be clear: I'm not suggesting special-casing the importlib.import_module. I'm suggesting that a submodule foo.bar import should be considered a redefinition of a prior foo.bar import, and should never be considered a redefinition of a previously defined foo symbol.
This came up in python/cpython#124384.
Terms I searched for before filing the issue:
- F811
- redefined-while-unused