Skip to content

Commit 942225e

Browse files
authored
Fix unpickling when compiled with newer mypycs (#9)
mypyc breaks pickling by default now (https://mypyc.readthedocs.io/en/latest/differences_from_python.html#pickling-and-copying-objects), and we need to opt out. Somewhat surprisingly, that seems to be insufficient. When allow_interpreted_subclasses isn't set, many of the objects will be allegedly unpickled but none of their attributes are set. This /seems/ like a mypyc bug, but I haven't looked into it yet.
1 parent 34fadf0 commit 942225e

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

parsing/ast.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
from __future__ import annotations
99

10+
from mypy_extensions import mypyc_attr
1011

12+
13+
@mypyc_attr(serializable=True, allow_interpreted_subclasses=True)
1114
class Symbol:
1215
pass
1316

parsing/automaton.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Tuple,
1616
Type,
1717
)
18+
from mypy_extensions import mypyc_attr
1819

1920
import collections
2021
import inspect
@@ -97,6 +98,7 @@ def computeFirstSet(s: Tuple[SymbolSpec, ...]) -> frozenset[SymbolSpec]:
9798
return firstSet
9899

99100

101+
@mypyc_attr(serializable=True, allow_interpreted_subclasses=True)
100102
class ItemSet:
101103
def __init__(self, items: Iterable[Tuple[Item, set[SymbolSpec]]]) -> None:
102104
self._kernel: Dict[Item, set[SymbolSpec]] = {}
@@ -216,9 +218,9 @@ def closure(self) -> None:
216218
def goto(self, sym: SymbolSpec) -> ItemSet | None:
217219
items = self._symMap.get(sym)
218220
if items:
219-
return ItemSet(
221+
return ItemSet([
220222
(i.production.item(i.dotPos + 1), self._all[i]) for i in items
221-
)
223+
])
222224
else:
223225
return None
224226

@@ -274,6 +276,7 @@ def weakCompat(self, other: ItemSet) -> bool:
274276
return True
275277

276278

279+
@mypyc_attr(serializable=True, allow_interpreted_subclasses=True)
277280
class Spec(interfaces.Spec):
278281
"""
279282
The Spec class contains the read-only data structures that the Parser

parsing/grammar.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
Type,
3939
)
4040

41+
from mypy_extensions import mypyc_attr
42+
4143
import re
4244
import sys
4345
from parsing.ast import Token, Nonterm
@@ -48,6 +50,7 @@
4850
import types
4951

5052

53+
@mypyc_attr(serializable=True, allow_interpreted_subclasses=True)
5154
class PrecedenceSpec:
5255
assoc_tok_re: ClassVar[re.Pattern[str]] = re.compile(
5356
r"([<>=])([A-Za-z]\w*)"
@@ -83,11 +86,13 @@ def __repr__(self) -> str:
8386
)
8487

8588

89+
@mypyc_attr(serializable=True, allow_interpreted_subclasses=True)
8690
class PrecedenceRef(PrecedenceSpec):
8791
def __init__(self, name: str) -> None:
8892
super().__init__(name, "fail", {})
8993

9094

95+
@mypyc_attr(serializable=True, allow_interpreted_subclasses=True)
9196
class SymbolSpec:
9297
name: str
9398
prec: PrecedenceSpec
@@ -122,6 +127,7 @@ def followSetMerge(self, set: Iterable[SymbolSpec]) -> bool:
122127
return ret
123128

124129

130+
@mypyc_attr(serializable=True, allow_interpreted_subclasses=True)
125131
class NontermSpec(SymbolSpec):
126132
token_re: ClassVar[re.Pattern[str]] = re.compile(r"([A-Za-z]\w*)")
127133
precedence_tok_re: ClassVar[re.Pattern[str]] = re.compile(
@@ -193,6 +199,7 @@ def from_class(
193199

194200

195201
# AKA terminal symbol.
202+
@mypyc_attr(serializable=True, allow_interpreted_subclasses=True)
196203
class TokenSpec(SymbolSpec):
197204
def __init__(
198205
self,
@@ -204,6 +211,7 @@ def __init__(
204211
self.tokenType = tokenType
205212

206213

214+
@mypyc_attr(serializable=True, allow_interpreted_subclasses=True)
207215
class Item:
208216
production: Production
209217
dotPos: int
@@ -260,6 +268,7 @@ def lr0__repr__(self) -> str:
260268
_item_cache: dict[tuple[Production, int], Item] = {}
261269

262270

271+
@mypyc_attr(serializable=True, allow_interpreted_subclasses=True)
263272
class Production:
264273
def __init__(
265274
self,
@@ -347,6 +356,7 @@ def reduce(self, userStartSym: SymbolSpec, eoi: EndOfInputSpec) -> None:
347356
pass
348357

349358

359+
@mypyc_attr(serializable=True, allow_interpreted_subclasses=True)
350360
class Action:
351361
"""
352362
Abstract base class, subclassed by {Shift,Reduce}Action."""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929
USE_MYPYC = False
30-
MYPY_DEPENDENCY = "mypy>=0.910"
30+
MYPY_DEPENDENCY = "mypy>=1.4.1"
3131
setup_requires = []
3232
ext_modules = []
3333

0 commit comments

Comments
 (0)