Skip to content

Instantly share code, notes, and snippets.

@ks-mw
Created March 10, 2023 17:07
Show Gist options
  • Save ks-mw/c5a476deb28886689f6d01b45e657e8d to your computer and use it in GitHub Desktop.
Save ks-mw/c5a476deb28886689f6d01b45e657e8d to your computer and use it in GitHub Desktop.
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