Skip to content

Commit 28423cd

Browse files
msullivanJukkaL
authored andcommitted
Don't serialize redefined symbol nodes (python#7499)
Redefined nodes sometimes have busted internal references (like a self argument that has the class as its type even though it won't be able to look it up) which can cause crashes. Since they can't have references from outside themselves, we don't lose anything by not serializing them. Fixes a crash I observed at Dropbox. This partially reverts python#7413, which introduced this crash (while fixing another), but leaves the test case it added.
1 parent 1ef02bb commit 28423cd

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

mypy/fixup.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,8 @@ def visit_type_info(self, info: TypeInfo) -> None:
5757
if info.metaclass_type:
5858
info.metaclass_type.accept(self.type_fixer)
5959
if info._mro_refs:
60-
# If the class is a "-redefinition", then its
61-
# reference to itself might be busted, so just use the
62-
# info instead of looking up the first element. Ew.
63-
info.mro = [info] + [
64-
lookup_qualified_typeinfo(self.modules, name, self.allow_missing)
65-
for name in info._mro_refs[1:]]
60+
info.mro = [lookup_qualified_typeinfo(self.modules, name, self.allow_missing)
61+
for name in info._mro_refs]
6662
info._mro_refs = None
6763
finally:
6864
self.current_info = save_info

mypy/nodes.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2996,8 +2996,6 @@ def serialize(self, prefix: str, name: str) -> JsonDict:
29962996
fullname = self.node.fullname()
29972997
if (fullname is not None and '.' in fullname
29982998
and fullname != prefix + '.' + name
2999-
# If it only doesn't match because of -redefinition, that is OK
3000-
and fullname != prefix + '.' + name.split('-redefinition')[0]
30012999
and not (isinstance(self.node, Var)
30023000
and self.node.from_module_getattr)):
30033001
data['cross_ref'] = fullname

mypy/semanal.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4245,6 +4245,11 @@ def add_redefinition(self,
42454245
redefinitions (such as e.g. variable redefined as a class).
42464246
"""
42474247
i = 1
4248+
# Don't serialize redefined nodes. They are likely to have
4249+
# busted internal references which can cause problems with
4250+
# serialization and they can't have any external references to
4251+
# them.
4252+
symbol.no_serialize = True
42484253
while True:
42494254
if i == 1:
42504255
new_name = '{}-redefinition'.format(name)

test-data/unit/check-incremental.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5094,3 +5094,16 @@ tmp/a.py:4: error: Cannot determine type of 'foo'
50945094
[out2]
50955095
tmp/a.py:3: error: Cannot determine type of 'foo'
50965096
tmp/a.py:4: error: Cannot determine type of 'foo'
5097+
5098+
[case testRedefinitionClass]
5099+
import b
5100+
[file a.py]
5101+
from whatever import Foo # type: ignore
5102+
5103+
class Foo: # type: ignore
5104+
def f(self) -> None:
5105+
pass
5106+
[file b.py]
5107+
import a
5108+
[file b.py.2]
5109+
import a # a change

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy