Debian Codegraph @

test-data/unit/plugins/custom_errorcode.py

raw
1 from __future__ import annotations
2
3 from typing import Callable
4
5 from mypy.errorcodes import ErrorCode
6 from mypy.plugin import FunctionContext, Plugin
7 from mypy.types import AnyType, Type, TypeOfAny
8
9 CUSTOM_ERROR = ErrorCode(code="custom", description="", category="Custom")
10
11
12 class CustomErrorCodePlugin(Plugin):
13 def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type] | None:
14 if fullname.endswith(".main"):
15 return self.emit_error
16 return None
17
18 def emit_error(self, ctx: FunctionContext) -> Type:
19 ctx.api.fail("Custom error", ctx.context, code=CUSTOM_ERROR)
20 return AnyType(TypeOfAny.from_error)
21
22
23 def plugin(version: str) -> type[CustomErrorCodePlugin]:
24 return CustomErrorCodePlugin