You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
no-redef when import-importing a module to satisfy a protocol #13803
modules-as-protocols triggers "no-redef" when using import-imports, but is fine with from-imports
(I'm testing out this unreleased feature on the primary branch)
# main.pyimportosfromtypingimportProtocolclassP(Protocol):
deff(self) ->str: ...
t: Pifos.environ.get('ENV') =='prod':
importtast# Name "t" already defined on line 7 [no-redef]else:
importuast# Name "t" already defined on line 7 [no-redef]
# t.py and u.pydeff() ->str:
return''
the errors triggered are inline above
oddly enough, a from import seems to work fine (so there's an easy workaround if the module isn't top-level):
mkdir y
touch y/__init__.py
mv {t,u}.py y
importosfromtypingimportProtocolclassP(Protocol):
deff(self) ->str: ...
t: Pifos.environ.get('ENV') =='prod':
fromyimporttast# no errors!else:
fromyimportuast# no errors!
Your Environment
Mypy version used: mypy 0.990+dev.dc5c299aa190949f2300b163ccc10257a779006d (compiled: no)
Mypy command-line flags: none
Mypy configuration options from mypy.ini (and other config files): none
Activity
hauntsaninja commentedon Oct 4, 2022
Thanks for the report!
Note mainly for self: looks like the difference is between
add_symbol_table_nodemypy/mypy/semanal.py
Line 5457 in ddd9177
and
process_imported_symbolmypy/mypy/semanal.py
Line 2309 in ddd9177
process_import_over_existing_nameisn't called for normal imports, but just changing that should probably fixBetter story for import redefinitions
Better story for import redefinitions (#13969)
hauntsaninja commentedon Nov 3, 2022
Thanks for reporting! This should now be fixed on master, but isn't part of the 0.990 release.