Skip to content

Commit a6585a3

Browse files
committed
Add test cases for AST serialization
The test cases use a serialized module in incremental mode in various ways to make sure that all important information in the AST nodes is preserved during serialization and deserialization. These are all very straightforward. The idea is to help catch regressions early and to make it easier to refactor or optimize serialization code. These found 2 issues -- separated the first to PR #3275. The second issue (#3274) is still unfixed and there is a skipped test case for it. More complex cases such as import cycles will still go to check-incremental.test.
1 parent a27963b commit a6585a3

File tree

3 files changed

+1208
-46
lines changed

3 files changed

+1208
-46
lines changed

mypy/test/testcheck.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
'check-semanal-error.test',
5656
'check-flags.test',
5757
'check-incremental.test',
58+
'check-serialize.test',
5859
'check-bound.test',
5960
'check-optional.test',
6061
'check-fastparse.test',
@@ -90,7 +91,9 @@ def cases(cls) -> List[DataDrivenTestCase]:
9091
return c
9192

9293
def run_case(self, testcase: DataDrivenTestCase) -> None:
93-
incremental = 'incremental' in testcase.name.lower() or 'incremental' in testcase.file
94+
incremental = ('incremental' in testcase.name.lower()
95+
or 'incremental' in testcase.file
96+
or 'serialize' in testcase.file)
9497
optional = 'optional' in testcase.file
9598
if incremental:
9699
# Incremental tests are run once with a cold cache, once with a warm cache.

test-data/unit/check-incremental.test

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
-- all cases so we can avoid constantly having to annotate it. The list of
2727
-- rechecked/stale files can be in any arbitrary order, or can be left empty
2828
-- if no files should be rechecked/stale.
29+
--
30+
-- There are additional incremental mode test cases in check-serialize.test.
2931

3032
[case testIncrementalEmpty]
3133
[rechecked]
@@ -1692,28 +1694,6 @@ main:1: error: Module 'ntcrash' has no attribute 'nope'
16921694
[out2]
16931695
main:1: error: Module 'ntcrash' has no attribute 'nope'
16941696

1695-
[case testIncrementalNamedTupleInMethod4]
1696-
from ntcrash import C
1697-
reveal_type(C().a)
1698-
reveal_type(C().b)
1699-
reveal_type(C().c)
1700-
[file ntcrash.py]
1701-
from typing import NamedTuple
1702-
class C:
1703-
def __init__(self) -> None:
1704-
A = NamedTuple('A', [('x', int)])
1705-
self.a = A(0)
1706-
self.b = A(0) # type: A
1707-
self.c = A
1708-
[out1]
1709-
main:2: error: Revealed type is 'Tuple[builtins.int, fallback=ntcrash.C.A@4]'
1710-
main:3: error: Revealed type is 'Tuple[builtins.int, fallback=ntcrash.C.A@4]'
1711-
main:4: error: Revealed type is 'def (x: builtins.int) -> Tuple[builtins.int, fallback=ntcrash.C.A@4]'
1712-
[out2]
1713-
main:2: error: Revealed type is 'Tuple[builtins.int, fallback=ntcrash.C.A@4]'
1714-
main:3: error: Revealed type is 'Tuple[builtins.int, fallback=ntcrash.C.A@4]'
1715-
main:4: error: Revealed type is 'def (x: builtins.int) -> Tuple[builtins.int, fallback=ntcrash.C.A@4]'
1716-
17171697
[case testIncrementalTypedDictInMethod]
17181698
from tdcrash import nope
17191699
[file tdcrash.py]
@@ -1756,29 +1736,6 @@ main:1: error: Module 'tdcrash' has no attribute 'nope'
17561736
[out2]
17571737
main:1: error: Module 'tdcrash' has no attribute 'nope'
17581738

1759-
[case testIncrementalTypedDictInMethod4]
1760-
from ntcrash import C
1761-
reveal_type(C().a)
1762-
reveal_type(C().b)
1763-
reveal_type(C().c)
1764-
[file ntcrash.py]
1765-
from mypy_extensions import TypedDict
1766-
class C:
1767-
def __init__(self) -> None:
1768-
A = TypedDict('A', {'x': int})
1769-
self.a = A(x=0)
1770-
self.b = A(x=0) # type: A
1771-
self.c = A
1772-
[builtins fixtures/dict.pyi]
1773-
[out1]
1774-
main:2: error: Revealed type is 'TypedDict(x=builtins.int, _fallback=typing.Mapping[builtins.str, builtins.int])'
1775-
main:3: error: Revealed type is 'TypedDict(x=builtins.int, _fallback=ntcrash.C.A@4)'
1776-
main:4: error: Revealed type is 'def () -> ntcrash.C.A@4'
1777-
[out2]
1778-
main:2: error: Revealed type is 'TypedDict(x=builtins.int, _fallback=typing.Mapping[builtins.str, builtins.int])'
1779-
main:3: error: Revealed type is 'TypedDict(x=builtins.int, _fallback=ntcrash.C.A@4)'
1780-
main:4: error: Revealed type is 'def () -> ntcrash.C.A@4'
1781-
17821739
[case testIncrementalInnerClassAttrInMethod]
17831740
import crash
17841741
nonexisting

0 commit comments

Comments
 (0)