From 282269e484cdece7aa024639f763927015cfdac5 Mon Sep 17 00:00:00 2001 From: Andrey Lebedev Date: Tue, 14 Mar 2023 00:38:41 +0200 Subject: [PATCH] Make sure mypy treats implementations as subtype of their interface Fixes #91 --- src/mypy_zope/plugin.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/mypy_zope/plugin.py b/src/mypy_zope/plugin.py index fcf0999..220ce75 100644 --- a/src/mypy_zope/plugin.py +++ b/src/mypy_zope/plugin.py @@ -3,6 +3,7 @@ from typing import Type as PyType from typing import cast +from mypy.typestate import type_state from mypy.types import ( Type, Instance, @@ -722,6 +723,13 @@ def _apply_interface(self, impl: TypeInfo, iface: TypeInfo) -> None: if promote not in impl._promote: impl._promote.append(promote) + # Remember implementation as a subtype of an interface. HACK: we are + # writing to a global variable here, so potentially this might be a + # memory leak. Needs testing with a large codebase. + asmpt = (Instance(impl, []), promote) + type_state.get_assumptions(False).append(asmpt) + type_state.get_assumptions(True).append(asmpt) + def plugin(version: str) -> PyType[Plugin]: return ZopeInterfacePlugin