Created
March 10, 2023 17:07
-
-
Save ks-mw/c5a476deb28886689f6d01b45e657e8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from mypy.plugin import Plugin, ClassDefContext | |
class MyPlugin(Plugin): | |
def get_base_class_hook(self, fullname: str) -> None: | |
if fullname == "..." or "..." in fullname: | |
# debug print: | |
print( "hook added" ) | |
return self.my_hook | |
return None | |
OR PERHAPS: | |
def get_class_decorator_hook(self, fullname: str) -> None: | |
if fullname == "..." or "..." in fullname: | |
return self.my_hook | |
return None | |
def my_hook(self, ctx: ClassDefContext) -> None: | |
if ctx.cls.info...: # Some condition that is forbidden | |
ctx.api.fail( | |
ctx=ctx.cls.info, | |
msg="some error message" ) | |
# Perhaps you might need something like that: | |
for decorator in ctx.cls.decorators: | |
if decorator.callee.fullname == "...": | |
... | |
def plugin(version: str): | |
return MyPlugin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment