Skip to content

Commit ec20c95

Browse files
emaheuxPERENAbhiTheModder
authored andcommitted
typing: Improve @cancellable; add flag literals
Refine @cancellable typing to better model control flow and introduce Literal types for optional Compiler flags to improve static checking and autocomplete.
1 parent 6eb642a commit ec20c95

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

frida/_frida/__init__.pyi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,13 @@ class Compiler(Object):
754754
self,
755755
entrypoint: str,
756756
project_root: Optional[str] = None,
757+
output_format: Optional[str] = None,
758+
bundle_format: Optional[str] = None,
759+
type_check: Optional[str] = None,
757760
source_maps: Optional[str] = None,
758761
compression: Optional[str] = None,
762+
platform: Optional[str] = None,
763+
externals: Optional[Sequence[str]] = None,
759764
) -> str:
760765
"""
761766
Build an agent.
@@ -766,8 +771,13 @@ class Compiler(Object):
766771
self,
767772
entrypoint: str,
768773
project_root: Optional[str] = None,
774+
output_format: Optional[str] = None,
775+
bundle_format: Optional[str] = None,
776+
type_check: Optional[str] = None,
769777
source_maps: Optional[str] = None,
770778
compression: Optional[str] = None,
779+
platform: Optional[str] = None,
780+
externals: Optional[Sequence[str]] = None,
771781
) -> None:
772782
"""
773783
Continuously build an agent.

frida/core.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@
3333
else:
3434
from typing_extensions import Literal, TypedDict
3535

36+
if sys.version_info >= (3, 10):
37+
from typing import ParamSpec
38+
else:
39+
from typing_extensions import ParamSpec
40+
3641
if sys.version_info >= (3, 11):
37-
from typing import NotRequired
42+
from typing import NotRequired, cast
3843
else:
39-
from typing_extensions import NotRequired
44+
from typing_extensions import NotRequired, cast
4045

4146
from . import _frida
4247

@@ -72,15 +77,18 @@ def _filter_missing_kwargs(d: MutableMapping[Any, Any]) -> None:
7277
d.pop(key)
7378

7479

75-
R = TypeVar("R")
80+
P = ParamSpec("P")
81+
R = TypeVar("R", covariant=True)
7682

7783

78-
def cancellable(f: Callable[..., R]) -> Callable[..., R]:
84+
def cancellable(f: Callable[P, R]) -> Callable[P, R]:
85+
# currently there is no way to type properly the extended callable with optional cancellable parameter
86+
# ref: https://github.com/python/typing/discussions/1905#discussioncomment-11696995
7987
@functools.wraps(f)
80-
def wrapper(*args: Any, **kwargs: Any) -> R:
88+
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
8189
cancellable = kwargs.pop("cancellable", None)
8290
if cancellable is not None:
83-
with cancellable:
91+
with cast(Cancellable, cancellable):
8492
return f(*args, **kwargs)
8593

8694
return f(*args, **kwargs)
@@ -1554,6 +1562,12 @@ class CompilerDiagnostic(TypedDict):
15541562
CompilerOutputCallback = Callable[[str], None]
15551563
CompilerDiagnosticsCallback = Callable[[List[CompilerDiagnostic]], None]
15561564

1565+
CompilerOutputFormat = Literal["unescaped", "hex-bytes", "c-string"]
1566+
CompilerBundleFormat = Literal["esm", "iife"]
1567+
CompilerTypeCheck = Literal["full", "none"]
1568+
CompilerSourceMaps = Literal["included", "omitted"]
1569+
CompilerCompression = Literal["none", "terser"]
1570+
CompilerPlatform = Literal["neutral", "gum", "browser"]
15571571

15581572
class Compiler:
15591573
def __init__(self) -> None:
@@ -1567,12 +1581,12 @@ def build(
15671581
self,
15681582
entrypoint: str,
15691583
project_root: Optional[str] = None,
1570-
output_format: Optional[str] = None,
1571-
bundle_format: Optional[str] = None,
1572-
type_check: Optional[str] = None,
1573-
source_maps: Optional[str] = None,
1574-
compression: Optional[str] = None,
1575-
platform: Optional[str] = None,
1584+
output_format: Optional[CompilerOutputFormat] = None,
1585+
bundle_format: Optional[CompilerBundleFormat] = None,
1586+
type_check: Optional[CompilerTypeCheck] = None,
1587+
source_maps: Optional[CompilerSourceMaps] = None,
1588+
compression: Optional[CompilerCompression] = None,
1589+
platform: Optional[CompilerPlatform] = None,
15761590
externals: Optional[Sequence[str]] = None,
15771591
) -> str:
15781592
kwargs = {
@@ -1593,12 +1607,12 @@ def watch(
15931607
self,
15941608
entrypoint: str,
15951609
project_root: Optional[str] = None,
1596-
output_format: Optional[str] = None,
1597-
bundle_format: Optional[str] = None,
1598-
type_check: Optional[str] = None,
1599-
source_maps: Optional[str] = None,
1600-
compression: Optional[str] = None,
1601-
platform: Optional[str] = None,
1610+
output_format: Optional[CompilerOutputFormat] = None,
1611+
bundle_format: Optional[CompilerBundleFormat] = None,
1612+
type_check: Optional[CompilerTypeCheck] = None,
1613+
source_maps: Optional[CompilerSourceMaps] = None,
1614+
compression: Optional[CompilerCompression] = None,
1615+
platform: Optional[CompilerPlatform] = None,
16021616
externals: Optional[Sequence[str]] = None,
16031617
) -> None:
16041618
kwargs = {

0 commit comments

Comments
 (0)