Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 959eb76

Browse files
authored
Fix aststrip deletion crash (#7543)
Only delete from self.type.names if the key is actually in there. I've included one test case that this fixes, though I suspect there are others. Fixes #7238.
1 parent 00530db commit 959eb76

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

mypy/server/aststrip.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ def process_lvalue_in_method(self, lvalue: Node) -> None:
219219
# true for a MemberExpr, we know that it must be an assignment through
220220
# self, since only those can define new attributes.
221221
assert self.type is not None
222-
del self.type.names[lvalue.name]
222+
if lvalue.name in self.type.names:
223+
del self.type.names[lvalue.name]
223224
key = (self.type.defn, lvalue.name)
224225
if key in self.saved_class_attrs:
225226
del self.saved_class_attrs[key]

test-data/unit/fine-grained.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9202,3 +9202,25 @@ class A: # type: ignore
92029202

92039203
[out]
92049204
==
9205+
9206+
[case testAddAttributeThroughNewBaseClass]
9207+
import a
9208+
9209+
[file a.py]
9210+
class C:
9211+
def __init__(self) -> None:
9212+
self.x = 0
9213+
9214+
[file a.py.2]
9215+
from b import B
9216+
9217+
class C(B):
9218+
def __init__(self) -> None:
9219+
self.x = 0
9220+
9221+
[file b.py.2]
9222+
class B:
9223+
def __init__(self) -> None:
9224+
self.x = 0
9225+
[out]
9226+
==

0 commit comments

Comments
 (0)