| 1 |
from mypy.types import LiteralType, AnyType, TypeOfAny, Type |
| 2 |
from mypy.plugin import Plugin, MethodContext |
| 3 |
from typing import Callable, Optional |
| 4 |
|
| 5 |
# If radd exists, there shouldn't be an error. If it doesn't exist, then there will be an error |
| 6 |
def type_add(ctx: MethodContext) -> Type: |
| 7 |
ctx.api.fail("fail", ctx.context) |
| 8 |
return AnyType(TypeOfAny.from_error) |
| 9 |
|
| 10 |
def type_radd(ctx: MethodContext) -> Type: |
| 11 |
return LiteralType(7, fallback=ctx.api.named_generic_type('builtins.int', [])) |
| 12 |
|
| 13 |
|
| 14 |
class TestPlugin(Plugin): |
| 15 |
|
| 16 |
def get_method_hook(self, fullname: str) -> Optional[Callable[[MethodContext], Type]]: |
| 17 |
if fullname == 'builtins.int.__add__': |
| 18 |
return type_add |
| 19 |
if fullname == 'builtins.int.__radd__': |
| 20 |
return type_radd |
| 21 |
return None |
| 22 |
|
| 23 |
def plugin(version: str) -> type[TestPlugin]: |
| 24 |
return TestPlugin |