-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathcustom_errorcode.py
More file actions
24 lines (16 loc) · 763 Bytes
/
custom_errorcode.py
File metadata and controls
24 lines (16 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from __future__ import annotations
from typing import Callable
from mypy.errorcodes import ErrorCode
from mypy.plugin import FunctionContext, Plugin
from mypy.types import AnyType, Type, TypeOfAny
CUSTOM_ERROR = ErrorCode(code="custom", description="", category="Custom")
class CustomErrorCodePlugin(Plugin):
def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type] | None:
if fullname.endswith(".main"):
return self.emit_error
return None
def emit_error(self, ctx: FunctionContext) -> Type:
ctx.api.fail("Custom error", ctx.context, code=CUSTOM_ERROR)
return AnyType(TypeOfAny.from_error)
def plugin(version: str) -> type[CustomErrorCodePlugin]:
return CustomErrorCodePlugin