Skip to content

Commit 919045f

Browse files
committed
Fix and add test
1 parent 484ab81 commit 919045f

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

mypy/exportjson.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
TypeType,
5959
TypeVarTupleType,
6060
TypeVarType,
61+
UnboundType,
6162
UninhabitedType,
6263
UnionType,
6364
UnpackType,
@@ -357,6 +358,8 @@ def convert_type(typ: Type) -> JsonDict:
357358
return convert_parameters(typ)
358359
elif isinstance(typ, TypedDictType):
359360
return convert_typeddict_type(typ)
361+
elif isinstance(typ, UnboundType):
362+
return convert_unbound_type(typ)
360363
assert False, type(typ)
361364

362365

@@ -516,6 +519,16 @@ def convert_typeddict_type(self: TypedDictType) -> JsonDict:
516519
}
517520

518521

522+
def convert_unbound_type(self: UnboundType) -> JsonDict:
523+
return {
524+
".class": "UnboundType",
525+
"name": self.name,
526+
"args": [convert_type(a) for a in self.args],
527+
"expr": self.original_str_expr,
528+
"expr_fallback": self.original_str_fallback,
529+
}
530+
531+
519532
def main() -> None:
520533
parser = argparse.ArgumentParser()
521534
parser.add_argument("path", nargs="+")

mypy/test/testexportjson.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22

33
from __future__ import annotations
44

5-
import os
65
import json
6+
import os
77
import re
8-
import pprint
98
import sys
109

1110
from mypy import build
1211
from mypy.errors import CompileError
12+
from mypy.exportjson import convert_binary_cache_to_json
1313
from mypy.modulefinder import BuildSource
1414
from mypy.options import Options
1515
from mypy.test.config import test_temp_dir
1616
from mypy.test.data import DataDrivenTestCase, DataSuite
1717
from mypy.test.helpers import assert_string_arrays_equal
18-
from mypy.exportjson import convert_binary_cache_to_json
1918

2019

2120
class TypeExportSuite(DataSuite):
2221
required_out_section = True
2322
files = ["exportjson.test"]
2423

2524
def run_case(self, testcase: DataDrivenTestCase) -> None:
25+
error = False
26+
src = "\n".join(testcase.input)
2627
try:
27-
src = "\n".join(testcase.input)
2828
options = Options()
2929
options.use_builtins_fixtures = True
3030
options.show_traceback = True
@@ -34,17 +34,23 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
3434
with open(fnam, "w") as f:
3535
f.write(src)
3636
result = build.build(
37-
sources=[BuildSource(fnam, "main")],
38-
options=options,
39-
alt_lib_path=test_temp_dir,
37+
sources=[BuildSource(fnam, "main")], options=options, alt_lib_path=test_temp_dir
4038
)
4139
a = result.errors
40+
error = bool(a)
4241

4342
major, minor = sys.version_info[:2]
4443
cache_dir = os.path.join(".mypy_cache", f"{major}.{minor}")
4544

4645
for module in result.files:
47-
if module in ("builtins", "typing", "_typeshed"):
46+
if module in (
47+
"builtins",
48+
"typing",
49+
"_typeshed",
50+
"__future__",
51+
"typing_extensions",
52+
"sys",
53+
):
4854
continue
4955
fnam = os.path.join(cache_dir, f"{module}.data.ff")
5056
with open(fnam, "rb") as f:
@@ -54,11 +60,10 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
5460
# We source file path is unpredictable, so filter it out
5561
line = re.sub(r'"[^"]+\.pyi?"', "...", line)
5662
a.append(line)
57-
print(fnam)
5863
except CompileError as e:
5964
a = e.messages
60-
assert_string_arrays_equal(
61-
testcase.output,
62-
a,
63-
f"Invalid output ({testcase.file}, line {testcase.line})",
64-
)
65+
error = True
66+
if error or "\n".join(testcase.output).strip() != "<not checked>":
67+
assert_string_arrays_equal(
68+
testcase.output, a, f"Invalid output ({testcase.file}, line {testcase.line})"
69+
)

test-data/unit/exportjson.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
--
33
-- The tool is maintained on a best effort basis so we don't attempt to have
44
-- full test coverage.
5+
--
6+
-- Some tests only ensure that *some* JSON is generated successfully. These
7+
-- have <not checked> as [out].
58

69
[case testExportVar]
710
x = 0
@@ -114,3 +117,20 @@ class C:
114117
"is_partial_stub_package": false,
115118
"future_import_flags": []
116119
}
120+
121+
[case testExportDifferentTypes]
122+
from __future__ import annotations
123+
124+
from typing import Callable, Any, Literal
125+
126+
list_ann: list[int]
127+
any_ann: Any
128+
tuple_ann: tuple[int, str]
129+
union_ann: int | None
130+
callable_ann: Callable[[int], str]
131+
type_type_ann: type[int]
132+
literal_ann: Literal['x', 5, False]
133+
134+
[builtins fixtures/tuple.pyi]
135+
[out]
136+
<not checked>

0 commit comments

Comments
 (0)