Skip to content

Instantly share code, notes, and snippets.

@snarkmaster
Created August 6, 2018 03:35
Show Gist options
  • Save snarkmaster/85076c9793fd5639fc7212a4c04e710a to your computer and use it in GitHub Desktop.
Save snarkmaster/85076c9793fd5639fc7212a4c04e710a to your computer and use it in GitHub Desktop.
From afb9cfba2854c48e1995e1c4940d679469febd94 Mon Sep 17 00:00:00 2001
From: Alexey Spiridonov <[email protected]>
Date: Sun, 5 Aug 2018 15:36:06 -0700
Subject: [PATCH] Switch to a more complex plugin invocation API
---
mypy/plugin.py | 11 ++++++-----
mypy/semanal.py | 7 ++++++-
2 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/mypy/plugin.py b/mypy/plugin.py
index 4392e55..6f59c57 100644
--- a/mypy/plugin.py
+++ b/mypy/plugin.py
@@ -210,8 +210,9 @@ class Plugin:
) -> Optional[Callable[[ClassDefContext], None]]:
return None
- def customize_class_mro(self, ctx: ClassDefContext) -> None:
- pass
+ def get_customize_class_mro_hook(self, fullname: str
+ ) -> Optional[Callable[[ClassDefContext], None]]:
+ return None
T = TypeVar('T')
@@ -269,9 +270,9 @@ class ChainedPlugin(Plugin):
) -> Optional[Callable[[ClassDefContext], None]]:
return self._find_hook(lambda plugin: plugin.get_base_class_hook(fullname))
- def customize_class_mro(self, ctx: ClassDefContext) -> None:
- for plugin in self._plugins:
- plugin.customize_class_mro(ctx)
+ def get_customize_class_mro_hook(self, fullname: str
+ ) -> Optional[Callable[[ClassDefContext], None]]:
+ return self._find_hook(lambda plugin: plugin.get_customize_class_mro_hook(fullname))
def _find_hook(self, lookup: Callable[[Plugin], T]) -> Optional[T]:
for plugin in self._plugins:
diff --git a/mypy/semanal.py b/mypy/semanal.py
index 635bb94..daf0fa5 100644
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -1145,7 +1145,12 @@ class SemanticAnalyzerPass2(NodeVisitor[None],
self.fail_blocker('Cannot determine consistent method resolution '
'order (MRO) for "%s"' % defn.name, defn)
defn.info.mro = []
- self.plugin.customize_class_mro(ClassDefContext(defn, Expression(), self))
+ # Allow plugins to alter the MRO to handle the fact that `def mro()`
+ # on metaclasses permits MRO rewriting.
+ if defn.fullname:
+ hook = self.plugin.get_customize_class_mro_hook(defn.fullname)
+ if hook:
+ hook(ClassDefContext(defn, Expression(), self))
def update_metaclass(self, defn: ClassDef) -> None:
"""Lookup for special metaclass declarations, and update defn fields accordingly.
--
1.9.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment