| 1 |
"""Test cases for inferring always defined attributes in classes.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import os.path |
| 6 |
|
| 7 |
from mypy.errors import CompileError |
| 8 |
from mypy.test.config import test_temp_dir |
| 9 |
from mypy.test.data import DataDrivenTestCase |
| 10 |
from mypyc.test.testutil import ( |
| 11 |
ICODE_GEN_BUILTINS, |
| 12 |
MypycDataSuite, |
| 13 |
assert_test_output, |
| 14 |
build_ir_for_single_file2, |
| 15 |
infer_ir_build_options_from_test_name, |
| 16 |
use_custom_builtins, |
| 17 |
) |
| 18 |
|
| 19 |
files = ["alwaysdefined.test"] |
| 20 |
|
| 21 |
|
| 22 |
class TestAlwaysDefined(MypycDataSuite): |
| 23 |
files = files |
| 24 |
base_path = test_temp_dir |
| 25 |
|
| 26 |
def run_case(self, testcase: DataDrivenTestCase) -> None: |
| 27 |
"""Perform a runtime checking transformation test case.""" |
| 28 |
options = infer_ir_build_options_from_test_name(testcase.name) |
| 29 |
if options is None: |
| 30 |
# Skipped test case |
| 31 |
return |
| 32 |
with use_custom_builtins(os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase): |
| 33 |
try: |
| 34 |
ir = build_ir_for_single_file2(testcase.input, options)[0] |
| 35 |
except CompileError as e: |
| 36 |
actual = e.messages |
| 37 |
else: |
| 38 |
actual = [] |
| 39 |
for cl in ir.classes: |
| 40 |
if cl.name.startswith("_"): |
| 41 |
continue |
| 42 |
actual.append( |
| 43 |
"{}: [{}]".format(cl.name, ", ".join(sorted(cl._always_initialized_attrs))) |
| 44 |
) |
| 45 |
|
| 46 |
assert_test_output(testcase, actual, "Invalid test output", testcase.output) |