summaryrefslogtreecommitdiff
path: root/test/ext/mypy/test_mypy_plugin_py3k.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2021-03-26 19:45:29 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2021-04-05 19:23:52 -0400
commit606096ae01c71298da4d3fda3f62c730d9985105 (patch)
tree8e745f10d60683edbf1da49a557bebc9994fb7ba /test/ext/mypy/test_mypy_plugin_py3k.py
parent4e0f90424afe03547c21ec3b6365755bb5288075 (diff)
downloadsqlalchemy-606096ae01c71298da4d3fda3f62c730d9985105.tar.gz
Adjust for mypy incremental behaviors
Applied a series of refactorings and fixes to accommodate for Mypy "incremental" mode across multiple files, which previously was not taken into account. In this mode the Mypy plugin has to accommodate Python datatypes expressed in other files coming in with less information than they have on a direct run. Additionally, a new decorator :func:`_orm.declarative_mixin` is added, which is necessary for the Mypy plugin to be able to definifitely identify a Declarative mixin class that is otherwise not used inside a particular Python file. discussion: With incremental / deserialized mypy runs, it appears that when we look at a base class that comes from another file, cls.info is set to a special undefined node that matches CLASSDEF_NO_INFO, and we otherwise can't touch it without crashing. Additionally, sometimes cls.defs.body is present but empty. However, it appears that both of these cases can be sidestepped, first by doing a lookup() for the type name where we get a SymbolTableNode that then has the TypeInfo we wanted when we tried touching cls.info, and then however we got the TypeInfo, if cls.defs.body is empty we can just look in the names to get at the symbols for that class; we just can't access AssignmentStmts, but that's fine because we just need the information for classes we aren't actually type checking. This work also revealed there's no easy way to detect a mixin class so we just create a new decorator to mark that. will make code look better in any case. Fixes: #6147 Change-Id: Ia8fac8acfeec931d8f280491cffc5c6cb4a1204e
Diffstat (limited to 'test/ext/mypy/test_mypy_plugin_py3k.py')
-rw-r--r--test/ext/mypy/test_mypy_plugin_py3k.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/test/ext/mypy/test_mypy_plugin_py3k.py b/test/ext/mypy/test_mypy_plugin_py3k.py
index c8d042db0..4ab16540d 100644
--- a/test/ext/mypy/test_mypy_plugin_py3k.py
+++ b/test/ext/mypy/test_mypy_plugin_py3k.py
@@ -11,8 +11,17 @@ from sqlalchemy.testing import fixtures
class MypyPluginTest(fixtures.TestBase):
__requires__ = ("sqlalchemy2_stubs",)
+ @testing.fixture(scope="function")
+ def per_func_cachedir(self):
+ for item in self._cachedir():
+ yield item
+
@testing.fixture(scope="class")
def cachedir(self):
+ for item in self._cachedir():
+ yield item
+
+ def _cachedir(self):
with tempfile.TemporaryDirectory() as cachedir:
with open(
os.path.join(cachedir, "sqla_mypy_config.cfg"), "w"
@@ -77,9 +86,11 @@ class MypyPluginTest(fixtures.TestBase):
*[(dirname,) for dirname in _incremental_dirs()], argnames="dirname"
)
@testing.requires.patch_library
- def test_incremental(self, mypy_runner, cachedir, dirname):
+ def test_incremental(self, mypy_runner, per_func_cachedir, dirname):
import patch
+ cachedir = per_func_cachedir
+
path = os.path.join(os.path.dirname(__file__), "incremental", dirname)
dest = os.path.join(cachedir, "mymodel")
os.mkdir(dest)
@@ -101,7 +112,9 @@ class MypyPluginTest(fixtures.TestBase):
if patchfile is not None:
print("Applying patchfile %s" % patchfile)
patch_obj = patch.fromfile(os.path.join(path, patchfile))
- patch_obj.apply(1, dest)
+ assert patch_obj.apply(1, dest), (
+ "pathfile %s failed" % patchfile
+ )
print("running mypy against %s/mymodel" % cachedir)
result = mypy_runner(
"mymodel",