Skip to content

Commit 2866660

Browse files
committed
Updated the magic_method.py to pass the self check
1 parent 2bc7c75 commit 2866660

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1-
from mypy.types import LiteralType, AnyType, TypeOfAny
2-
from mypy.plugin import Plugin
1+
from mypy.types import LiteralType, AnyType, TypeOfAny, Type
2+
from mypy.plugin import Plugin, MethodContext
3+
from typing import Callable
34

45
# If radd exists, there shouldn't be an error. If it doesn't exist, then there will be an error
5-
def type_add(ctx, *args):
6-
lhs = ctx.type
7-
rhs = ctx.arg_types[0][0]
8-
ret = lhs.value + rhs.value
6+
def type_add(ctx: MethodContext, *args) -> Type:
97
ctx.api.fail("test", ctx.context)
108
return AnyType(TypeOfAny.from_error)
119

12-
def type_radd(ctx, *args):
10+
def type_radd(ctx: MethodContext, *args) -> Type:
1311
lhs = ctx.type
14-
rhs = ctx.arg_types[0][0]
12+
rhs = ctx.arg_types[-1][0]
13+
if not (isinstance(lhs, LiteralType) and isinstance(rhs, LiteralType)):
14+
ctx.api.fail("operands not literals", ctx.context)
15+
return AnyType(TypeOfAny.from_error)
16+
if not (isinstance(lhs.value, int) and isinstance(rhs.value, int)):
17+
ctx.api.fail("operands not literal ints", ctx.context)
18+
return AnyType(TypeOfAny.from_error)
1519
ret = lhs.value + rhs.value
1620
return LiteralType(ret, fallback=ctx.api.named_generic_type('builtins.int', []))
1721

1822

1923
class TestPlugin(Plugin):
24+
def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | None:
2025

21-
def get_method_hook(self, fullname):
2226
if fullname == 'builtins.int.__add__':
2327
return type_add
2428
if fullname == 'builtins.int.__radd__':
2529
return type_radd
2630
return None
2731

28-
def plugin(version: str):
32+
def plugin(version: str) -> type[TestPlugin]:
2933
return TestPlugin

0 commit comments

Comments
 (0)