Skip to content

Commit 0d2c577

Browse files
committed
Allow redefining a function via import
1 parent 90620b0 commit 0d2c577

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

mypy/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ def process_import_over_existing_name(self,
829829
module_symbol: SymbolTableNode,
830830
import_node: ImportBase) -> bool:
831831
if (existing_symbol.kind in (LDEF, GDEF, MDEF) and
832-
isinstance(existing_symbol.node, Var)):
832+
isinstance(existing_symbol.node, (Var, FuncDef))):
833833
# This is a valid import over an existing definition in the file. Construct a dummy
834834
# assignment that we'll use to type check the import.
835835
lvalue = NameExpr(imported_id)

mypy/test/data/check-modules.test

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ except:
512512
x = ''
513513
y = ''
514514

515-
[case testOverrideImportedFunctionViaImport]
515+
[case testRedefineImportedFunctionViaImport]
516516
try:
517517
from m import f, g
518518
except:
@@ -532,6 +532,17 @@ x = 1
532532
[file n.py]
533533
x = ''
534534

535+
[case testRedefineFunctionViaImport]
536+
def f(x): pass
537+
def g(x): pass
538+
try:
539+
from m import f, g # E: Incompatible import of "g" (imported name has type Callable[[Any, Any], Any], local name has type Callable[[Any], Any])
540+
except:
541+
pass
542+
[file m.py]
543+
def f(x): pass
544+
def g(x, y): pass
545+
535546

536547
-- Test cases that simulate 'mypy -m modname'
537548
--

0 commit comments

Comments
 (0)