Skip to content

Commit 85418fb

Browse files
msullivanilevkivskyi
authored andcommitted
Move some stuff back *out* of mypyc_hacks (#5488)
Faced with needing to move like 7 more exception classes into mypyc_hacks, I went and implemented defining new exceptions in mypyc, so we can move `DecodeError` back. `SymbolTable` (inherits from `dict`) can be also now moved back.
1 parent 88c8cc6 commit 85418fb

File tree

3 files changed

+50
-68
lines changed

3 files changed

+50
-68
lines changed

mypy/mypyc_hacks.py

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
"""Stuff that we had to move out of its right place because of mypyc limitations."""
22

33

4-
# Moved from util.py, because it inherits from Exception
5-
class DecodeError(Exception):
6-
"""Exception raised when a file cannot be decoded due to an unknown encoding type.
7-
8-
Essentially a wrapper for the LookupError raised by `bytearray.decode`
9-
"""
10-
11-
124
# Moved from types.py, because it inherits from Enum, which uses a
135
# metaclass in a nontrivial way.
146
from enum import Enum
@@ -36,60 +28,3 @@ class TypeOfAny(Enum):
3628
from_another_any = 'from_another_any'
3729
# Does this Any come from an implementation limitation/bug?
3830
implementation_artifact = 'implementation_artifact'
39-
40-
41-
# Moved from nodes.py, because it inherits from dict
42-
from typing import Dict, List, Any
43-
44-
JsonDict = Dict[str, Any]
45-
46-
MYPY = False
47-
if MYPY:
48-
from mypy.nodes import SymbolTableNode
49-
50-
51-
class SymbolTable(Dict[str, 'SymbolTableNode']):
52-
def __str__(self) -> str:
53-
from mypy.nodes import implicit_module_attrs, SymbolTableNode # noqa
54-
55-
a = [] # type: List[str]
56-
for key, value in self.items():
57-
# Filter out the implicit import of builtins.
58-
if isinstance(value, SymbolTableNode):
59-
if (value.fullname != 'builtins' and
60-
(value.fullname or '').split('.')[-1] not in
61-
implicit_module_attrs):
62-
a.append(' ' + str(key) + ' : ' + str(value))
63-
else:
64-
a.append(' <invalid item>')
65-
a = sorted(a)
66-
a.insert(0, 'SymbolTable(')
67-
a[-1] += ')'
68-
return '\n'.join(a)
69-
70-
def copy(self) -> 'SymbolTable':
71-
return SymbolTable((key, node.copy())
72-
for key, node in self.items())
73-
74-
def serialize(self, fullname: str) -> JsonDict:
75-
data = {'.class': 'SymbolTable'} # type: JsonDict
76-
for key, value in self.items():
77-
# Skip __builtins__: it's a reference to the builtins
78-
# module that gets added to every module by
79-
# SemanticAnalyzerPass2.visit_file(), but it shouldn't be
80-
# accessed by users of the module.
81-
if key == '__builtins__' or value.no_serialize:
82-
continue
83-
data[key] = value.serialize(fullname, key)
84-
return data
85-
86-
@classmethod
87-
def deserialize(cls, data: JsonDict) -> 'SymbolTable':
88-
from mypy.nodes import SymbolTableNode # noqa
89-
90-
assert data['.class'] == 'SymbolTable'
91-
st = SymbolTable()
92-
for key, value in data.items():
93-
if key != '.class':
94-
st[key] = SymbolTableNode.deserialize(value)
95-
return st

mypy/nodes.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import mypy.strconv
1515
from mypy.util import short_type
1616
from mypy.visitor import NodeVisitor, StatementVisitor, ExpressionVisitor
17-
from mypy.mypyc_hacks import SymbolTable
1817

1918
from mypy.bogus_type import Bogus
2019

@@ -2747,6 +2746,49 @@ def deserialize(cls, data: JsonDict) -> 'SymbolTableNode':
27472746
return stnode
27482747

27492748

2749+
class SymbolTable(Dict[str, SymbolTableNode]):
2750+
def __str__(self) -> str:
2751+
a = [] # type: List[str]
2752+
for key, value in self.items():
2753+
# Filter out the implicit import of builtins.
2754+
if isinstance(value, SymbolTableNode):
2755+
if (value.fullname != 'builtins' and
2756+
(value.fullname or '').split('.')[-1] not in
2757+
implicit_module_attrs):
2758+
a.append(' ' + str(key) + ' : ' + str(value))
2759+
else:
2760+
a.append(' <invalid item>')
2761+
a = sorted(a)
2762+
a.insert(0, 'SymbolTable(')
2763+
a[-1] += ')'
2764+
return '\n'.join(a)
2765+
2766+
def copy(self) -> 'SymbolTable':
2767+
return SymbolTable((key, node.copy())
2768+
for key, node in self.items())
2769+
2770+
def serialize(self, fullname: str) -> JsonDict:
2771+
data = {'.class': 'SymbolTable'} # type: JsonDict
2772+
for key, value in self.items():
2773+
# Skip __builtins__: it's a reference to the builtins
2774+
# module that gets added to every module by
2775+
# SemanticAnalyzerPass2.visit_file(), but it shouldn't be
2776+
# accessed by users of the module.
2777+
if key == '__builtins__' or value.no_serialize:
2778+
continue
2779+
data[key] = value.serialize(fullname, key)
2780+
return data
2781+
2782+
@classmethod
2783+
def deserialize(cls, data: JsonDict) -> 'SymbolTable':
2784+
assert data['.class'] == 'SymbolTable'
2785+
st = SymbolTable()
2786+
for key, value in data.items():
2787+
if key != '.class':
2788+
st[key] = SymbolTableNode.deserialize(value)
2789+
return st
2790+
2791+
27502792
def get_flags(node: Node, names: List[str]) -> List[str]:
27512793
return [name for name in names if getattr(node, name)]
27522794

mypy/util.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
if MYPY:
1414
from typing import Type
1515

16-
from mypy.mypyc_hacks import DecodeError
17-
1816
T = TypeVar('T')
1917

2018
ENCODING_RE = re.compile(br'([ \t\v]*#.*(\r\n?|\n))??[ \t\v]*#.*coding[:=][ \t]*([-\w.]+)')
@@ -69,6 +67,13 @@ def find_python_encoding(text: bytes, pyversion: Tuple[int, int]) -> Tuple[str,
6967
return default_encoding, -1
7068

7169

70+
class DecodeError(Exception):
71+
"""Exception raised when a file cannot be decoded due to an unknown encoding type.
72+
73+
Essentially a wrapper for the LookupError raised by `bytearray.decode`
74+
"""
75+
76+
7277
def decode_python_encoding(source: bytes, pyversion: Tuple[int, int]) -> str:
7378
"""Read the Python file with while obeying PEP-263 encoding detection.
7479

0 commit comments

Comments
 (0)