Skip to content

Commit c6eff9d

Browse files
committed
Avoid extra errors in edge cases; add tests
1 parent d603b42 commit c6eff9d

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

mypy/semanal.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7083,8 +7083,10 @@ def add_symbol_table_node(
70837083
if isinstance(old, Var) and is_init_only(old):
70847084
if old.has_explicit_value:
70857085
self.fail("InitVar with default value cannot be redefined", context)
7086-
elif not (
7087-
isinstance(new, (FuncDef, Decorator)) and self.set_original_def(old, new)
7086+
elif (
7087+
not (isinstance(new, (FuncDef, Decorator)) and self.set_original_def(old, new))
7088+
# Avoid (additional) errors for internal symbols.
7089+
and "@" not in name
70887090
):
70897091
self.name_already_defined(name, context, existing)
70907092
elif type_param or (

test-data/unit/check-incremental.test

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8011,3 +8011,50 @@ x = "hello"
80118011
[builtins fixtures/module.pyi]
80128012
[stale b]
80138013
[rechecked b]
8014+
8015+
[case testNamedTupleInlineBaseClassRedefined]
8016+
import m
8017+
[file m.py]
8018+
from lib import A
8019+
a: A
8020+
reveal_type(a)
8021+
[file m.py.2]
8022+
from lib import A
8023+
a: A # touch
8024+
reveal_type(a)
8025+
[file lib.py]
8026+
from typing import NamedTuple
8027+
class A(NamedTuple("N", [('x', int)])): pass
8028+
class A(NamedTuple("N", [('x', str)])): pass
8029+
[builtins fixtures/tuple.pyi]
8030+
[out]
8031+
tmp/lib.py:3: error: Name "A" already defined on line 2
8032+
tmp/m.py:3: note: Revealed type is "tuple[builtins.int, fallback=lib.A]"
8033+
[out2]
8034+
tmp/lib.py:3: error: Name "A" already defined on line 2
8035+
tmp/m.py:3: note: Revealed type is "tuple[builtins.int, fallback=lib.A]"
8036+
8037+
[case testTypedDictInlineBaseClassRedefined]
8038+
import m
8039+
[file m.py]
8040+
from lib import A
8041+
a: A
8042+
reveal_type(a)
8043+
[file m.py.2]
8044+
from lib import A
8045+
a: A # touch
8046+
reveal_type(a)
8047+
[file lib.py]
8048+
from typing import TypedDict
8049+
class A(TypedDict("TD", {'x': int})):
8050+
pass
8051+
class A(TypedDict("TD", {'x': str})):
8052+
pass
8053+
[builtins fixtures/dict.pyi]
8054+
[typing fixtures/typing-typeddict.pyi]
8055+
[out]
8056+
tmp/lib.py:4: error: Name "A" already defined (possibly by an import)
8057+
tmp/m.py:3: note: Revealed type is "TypedDict('lib.A', {'x': builtins.int})"
8058+
[out2]
8059+
tmp/lib.py:4: error: Name "A" already defined (possibly by an import)
8060+
tmp/m.py:3: note: Revealed type is "TypedDict('lib.A', {'x': builtins.int})"

0 commit comments

Comments
 (0)