-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Don't issue no-redef
with conditional imports
#13914
Comments
This comment was marked as outdated.
This comment was marked as outdated.
A workaround can be found in mkorpela/overrides#109 (comment). |
This changes our importing logic to be more consistent and to treat import statements more like assignments. Fixes #13803, fixes #13914, fixes half of #12965, probably fixes #12574 The primary motivation for this is when typing modules as protocols, as in #13803. But it turns out we already allowed redefinition with "from" imports, so this also seems like a nice consistency win. We move shared logic from visit_import_all and visit_import_from (via process_imported_symbol) into add_imported_symbol. We then reuse it in visit_import. To simplify stuff, we inline the code from add_module_symbol into visit_import. Then we copy over logic from add_symbol, because MypyFile is not a SymbolTableNode, but this isn't the worst thing ever. Finally, we now need to check non-from import statements like assignments, which was a thing we weren't doing earlier.
@hauntsaninja I may be missing something, but the code example I posted in #13914 (comment) still issues the same
Using |
Just for the record (because I had been hoping it might have been fixed), this is still an issue on 1.0.0. |
Some more details: """With `overrides==7.3.1` installed, mypy issues `no-redef` in `except`."""
from typing import Callable, TypeVar
F = TypeVar("F", bound=Callable)
try:
from overrides import override
except ModuleNotFoundError:
def override(method: F) -> F:
return method
print(override) The workaround works , but stops working when removing """The workaround silences `mypy`, but now mypy issues `no-redef` in *`try`* if `overrides` is *not* installed."""
from typing import Callable, TypeVar
F = TypeVar("F", bound=Callable)
# Workaround
override: Callable[[F], F]
try:
from overrides import override
except ModuleNotFoundError:
def override(method: F) -> F:
return method
print(override) This is a problem if one uses a construct such as this to import """Prepare for Python 3.12."""
from typing import Callable, TypeVar
F = TypeVar("F", bound=Callable)
# Workaround
override: Callable[[F], F]
try:
from typing import override
except ModuleNotFoundError:
def override(method: F) -> F:
return method
print(override) For the record, this works: """Prepare for Python 3.12."""
import sys
from typing import Callable, TypeVar
F = TypeVar("F", bound=Callable)
if sys.version_info >= (3, 12):
from typing import override
else:
def override(method: F) -> F:
return method
print(override) |
Bug Report
When an exception is caught from a block with a single import statement, one can be certain that the import failed (right?).
When an import fails, a to-be-imported function is not defined.
If a function is not defined,
mypy
should not issue ano-redef
message.To Reproduce
pip install overrides
Expected Behavior
No message
Actual Behavior
Your Environment
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: