Skip to content

Commit 6e745ef

Browse files
committed
Add sentinel modules for mypyc compilation testing
- Add graphql._sentinel (interpreted fallback) - Add graphql_mypyc._sentinel (compiled version) - Include benchmark functions: fibonacci, sum_squares - SENTINEL_VALUE differentiates compiled vs interpreted
1 parent 951a5f2 commit 6e745ef

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

src/graphql/_sentinel.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Sentinel module for mypyc compilation testing (interpreted version).
2+
3+
This is the interpreted fallback version. When graphql_mypyc is installed
4+
and the import hook is active, this module is replaced with the compiled version.
5+
"""
6+
7+
from __future__ import annotations
8+
9+
__all__ = ["SENTINEL_VALUE", "add_numbers", "fibonacci", "is_compiled", "sum_squares"]
10+
11+
SENTINEL_VALUE: str = "interpreted"
12+
13+
14+
def is_compiled() -> bool:
15+
"""Return True if this module was compiled with mypyc."""
16+
return False
17+
18+
19+
def add_numbers(a: int, b: int) -> int:
20+
"""Simple function to test mypyc compilation."""
21+
return a + b
22+
23+
24+
def fibonacci(n: int) -> int:
25+
"""Compute fibonacci number recursively."""
26+
if n <= 1:
27+
return n
28+
return fibonacci(n - 1) + fibonacci(n - 2)
29+
30+
31+
def sum_squares(n: int) -> int:
32+
"""Sum of squares from 1 to n."""
33+
total = 0
34+
for i in range(1, n + 1):
35+
total += i * i
36+
return total

src/graphql_mypyc/_sentinel.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Sentinel module for mypyc compilation testing.
2+
3+
This module is compiled with mypyc to verify the compilation infrastructure
4+
works correctly. It will be removed once real modules are compiled.
5+
"""
6+
7+
from __future__ import annotations
8+
9+
__all__ = ["SENTINEL_VALUE", "add_numbers", "fibonacci", "is_compiled", "sum_squares"]
10+
11+
# A constant value that can be checked
12+
SENTINEL_VALUE: str = "mypyc-compiled"
13+
14+
15+
def is_compiled() -> bool:
16+
"""Return True if this module was compiled with mypyc.
17+
18+
Checks if the module is loaded from a .so extension file.
19+
"""
20+
return __file__.endswith(".so")
21+
22+
23+
def add_numbers(a: int, b: int) -> int:
24+
"""Simple function to test mypyc compilation.
25+
26+
This is a simple pure function that benefits from mypyc compilation.
27+
"""
28+
return a + b
29+
30+
31+
def fibonacci(n: int) -> int:
32+
"""Compute fibonacci number recursively.
33+
34+
This is intentionally slow to show mypyc speedup.
35+
"""
36+
if n <= 1:
37+
return n
38+
return fibonacci(n - 1) + fibonacci(n - 2)
39+
40+
41+
def sum_squares(n: int) -> int:
42+
"""Sum of squares from 1 to n.
43+
44+
Loop-based computation that benefits from mypyc.
45+
"""
46+
total = 0
47+
for i in range(1, n + 1):
48+
total += i * i
49+
return total

0 commit comments

Comments
 (0)