File: custom_errorcode.py

package info (click to toggle)
mypy 1.19.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,412 kB
  • sloc: python: 114,754; ansic: 13,343; cpp: 11,380; makefile: 257; sh: 28
file content (24 lines) | stat: -rw-r--r-- 763 bytes parent folder | download | duplicates (2)
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