-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathattrhook2.py
More file actions
37 lines (25 loc) · 1.14 KB
/
attrhook2.py
File metadata and controls
37 lines (25 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from __future__ import annotations
from typing import Callable
from mypy.plugin import AttributeContext, Plugin
from mypy.types import AnyType, Type, TypeOfAny
class AttrPlugin(Plugin):
def get_attribute_hook(self, fullname: str) -> Callable[[AttributeContext], Type] | None:
if fullname == "m.Magic.magic_field":
return magic_field_callback
if fullname == "m.Magic.nonexistent_field":
return nonexistent_field_callback
if fullname == "m.Magic.no_assignment_field":
return no_assignment_field_callback
return None
def magic_field_callback(ctx: AttributeContext) -> Type:
return ctx.api.named_generic_type("builtins.str", [])
def nonexistent_field_callback(ctx: AttributeContext) -> Type:
ctx.api.fail("Field does not exist", ctx.context)
return AnyType(TypeOfAny.from_error)
def no_assignment_field_callback(ctx: AttributeContext) -> Type:
if ctx.is_lvalue:
ctx.api.fail(f"Cannot assign to field", ctx.context)
return AnyType(TypeOfAny.from_error)
return ctx.default_attr_type
def plugin(version: str) -> type[AttrPlugin]:
return AttrPlugin