|
| 1 | +import shutil |
| 2 | +import textwrap |
| 3 | +import os |
| 4 | +import unittest |
| 5 | + |
| 6 | +import mypy.api |
| 7 | + |
| 8 | +HERE = os.path.dirname(__file__) |
| 9 | +TEST_OUTPUT_DIR = os.path.join(HERE, "test-output") |
| 10 | +MYPY_INI = """\ |
| 11 | +[mypy] |
| 12 | +follow_imports = silent |
| 13 | +plugins = marshmallow_dataclass.mypy |
| 14 | +""" |
| 15 | + |
| 16 | + |
| 17 | +class TestMypyPlugin(unittest.TestCase): |
| 18 | + maxDiff = None |
| 19 | + |
| 20 | + def setUp(self): |
| 21 | + """ |
| 22 | + Prepare a clean test directory at tests/test-output/test_mypy-{testname}. |
| 23 | + Also cd into it for the duration of the test to get simple filenames in mypy output. |
| 24 | + """ |
| 25 | + testname = self.id().split(".")[-1] |
| 26 | + self.testdir = os.path.join(TEST_OUTPUT_DIR, f"test_mypy-{testname}") |
| 27 | + if os.path.exists(self.testdir): |
| 28 | + shutil.rmtree(self.testdir) |
| 29 | + os.makedirs(self.testdir) |
| 30 | + self.old_cwd = os.getcwd() |
| 31 | + os.chdir(self.testdir) |
| 32 | + |
| 33 | + def tearDown(self): |
| 34 | + os.chdir(self.old_cwd) |
| 35 | + |
| 36 | + def mypy_test(self, contents: str, expected: str): |
| 37 | + """ |
| 38 | + Run mypy and assert output matches ``expected``. |
| 39 | +
|
| 40 | + The file with ``contents`` is always named ``main.py``. |
| 41 | + """ |
| 42 | + config_path = os.path.join(self.testdir, "mypy.ini") |
| 43 | + script_path = os.path.join(self.testdir, "main.py") |
| 44 | + with open(config_path, "w") as f: |
| 45 | + f.write(MYPY_INI) |
| 46 | + with open(script_path, "w") as f: |
| 47 | + f.write(textwrap.dedent(contents).strip()) |
| 48 | + command = [script_path, "--config-file", config_path, "--no-error-summary"] |
| 49 | + out, err, returncode = mypy.api.run(command) |
| 50 | + err_msg = "\n".join(["", f"returncode: {returncode}", "stdout:", out, "stderr:", err]) |
| 51 | + self.assertEqual(out.strip(), textwrap.dedent(expected).strip(), err_msg) |
| 52 | + |
| 53 | + def test_basic(self): |
| 54 | + self.mypy_test( |
| 55 | + """ |
| 56 | + from dataclasses import dataclass |
| 57 | + import marshmallow as ma |
| 58 | + from marshmallow_dataclass import NewType |
| 59 | +
|
| 60 | + Email = NewType("Email", str, validate=ma.validate.Email) |
| 61 | + UserID = NewType("UserID", validate=ma.validate.Length(equal=32), typ=str) |
| 62 | +
|
| 63 | + @dataclass |
| 64 | + class User: |
| 65 | + id: UserID |
| 66 | + email: Email |
| 67 | +
|
| 68 | + user = User(id="a"*32, email="[email protected]") |
| 69 | + reveal_type(user.id) |
| 70 | + reveal_type(user.email) |
| 71 | +
|
| 72 | + User(id=42, email="[email protected]") |
| 73 | + User(id="a"*32, email=["not", "a", "string"]) |
| 74 | + """, |
| 75 | + """ |
| 76 | + main.py:14: note: Revealed type is 'builtins.str' |
| 77 | + main.py:15: note: Revealed type is 'builtins.str' |
| 78 | + main.py:17: error: Argument "id" to "User" has incompatible type "int"; expected "str" |
| 79 | + main.py:18: error: Argument "email" to "User" has incompatible type "List[str]"; expected "str" |
| 80 | + """, |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + unittest.main() |
0 commit comments