Skip to content

Commit 7a7e6c5

Browse files
committed
feat: improve typing for decorated genfuncs
1 parent 9ae3e9a commit 7a7e6c5

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

mypyc/test-data/irbuild-basic.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3799,3 +3799,21 @@ L0:
37993799
r2.__mypyc_env__ = r0; r3 = is_error
38003800
wrapper = r2
38013801
return wrapper
3802+
3803+
[case testStaticMethodGenerator]
3804+
from typing import Iterator
3805+
class C:
3806+
@staticmethod
3807+
def staticgen() -> Iterator[int]:
3808+
yield 1
3809+
@classmethod
3810+
def classgen(cls) -> Iterator[int]:
3811+
yield 1
3812+
[out]
3813+
def C.staticgen():
3814+
r0 = CPyGen_Create(...)
3815+
return r0
3816+
def C.classgen(cls):
3817+
cls :: object
3818+
r0 = CPyGen_Create(...)
3819+
return r0

mypyc/test-data/irbuild-generics.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,3 +773,18 @@ def f(x):
773773
x :: int
774774
L0:
775775
return x
776+
777+
[case testTypePreservingDecoratorGenerator]
778+
from typing import Callable, TypeVar, Iterator
779+
780+
T = TypeVar("T", bound=Callable)
781+
def identity_decorator(f: T) -> T:
782+
return f
783+
784+
@identity_decorator
785+
def gen() -> Iterator[int]:
786+
yield 1
787+
[out]
788+
def gen():
789+
r0 = CPyGen_Create(...)
790+
return r0

mypyc/test-data/run-generators.test

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,3 +907,41 @@ def test_same_names() -> None:
907907
# matches the variable name in the input code, since internally it's generated
908908
# with a prefix.
909909
list(undefined())
910+
911+
[case testStaticMethodGeneratorRuntime]
912+
class C:
913+
@staticmethod
914+
def staticgen():
915+
yield 42
916+
917+
from testutil import run_generator
918+
assert run_generator(C.staticgen()) == ((42,), None)
919+
[out]
920+
42
921+
922+
[case testClassMethodGeneratorRuntime]
923+
class C:
924+
@classmethod
925+
def classgen(cls):
926+
yield 99
927+
928+
from testutil import run_generator
929+
assert run_generator(C.classgen()) == ((99,), None)
930+
[out]
931+
99
932+
933+
[case testTypePreservingDecoratorGeneratorRuntime]
934+
from typing import Callable, TypeVar
935+
936+
T = TypeVar("T", bound=Callable)
937+
def identity_decorator(f: T) -> T:
938+
return f
939+
940+
@identity_decorator
941+
def gen():
942+
yield 7
943+
944+
from testutil import run_generator
945+
assert run_generator(gen()) == ((7,), None)
946+
[out]
947+
7

mypyc/test-data/run-misc.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ f(5)
5858
[out]
5959
5
6060

61+
[case test_macro_splice_decorator_runtime]
62+
def log_decorator(fn):
63+
def wrapper(*args, **kwargs):
64+
print("Calling function")
65+
return fn(*args, **kwargs)
66+
return wrapper
67+
68+
@log_decorator
69+
def add(x: int, y: int) -> int:
70+
return x + y
71+
72+
@log_decorator
73+
def mul(x: int, y: int) -> int:
74+
return x * y
75+
76+
def test_decorator_runtime() -> None:
77+
print(add(2, 3))
78+
print(mul(4, 5))
79+
80+
[out]
81+
Calling function
82+
5
83+
Calling function
84+
20
85+
6186
[case testOptional]
6287
from typing import Optional
6388

0 commit comments

Comments
 (0)