Skip to content

Commit c146b15

Browse files
DinoVmeta-codesync[bot]
authored andcommitted
All files static, support opt-out in PyreflyCompiler
Summary: Makes the Pyrefly compiler treat all modules as strict and adds an opt-out. Extends testing to make sure that works. Reviewed By: alexmalyshev Differential Revision: D95993330 fbshipit-source-id: 0814d310f5d2929154e64a94e72fa778cfd9e8aa
1 parent 5adcb6f commit c146b15

3 files changed

Lines changed: 56 additions & 15 deletions

File tree

cinderx/PythonLib/cinderx/compiler/static/pyrefly_compiler.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22

3-
from cinderx.compiler.errors import ErrorSink
4-
from cinderx.compiler.static import Compiler, StaticCodeGenBase
3+
import ast
4+
import sys
5+
56
from cinderx.compiler.static.pyrefly_type_binder import (
67
PyreflyTypeBinder,
78
PyreflyTypeInfo,
89
)
910
from cinderx.compiler.static.type_binder import TypeBinder
11+
from cinderx.compiler.strict.compiler import Compiler
12+
from cinderx.compiler.strict.flag_extractor import Flags
1013
from cinderx.compiler.symbols import SymbolVisitor
1114

1215

1316
class PyreflyCompiler(Compiler):
1417
def __init__(
1518
self,
16-
code_generator: type[StaticCodeGenBase],
17-
error_sink: ErrorSink | None = None,
1819
type_info: PyreflyTypeInfo | None = None,
20+
non_static_modules: set[str] | None = None,
1921
):
20-
super().__init__(code_generator, error_sink)
22+
super().__init__(sys.path, "", [], [])
2123
assert type_info is not None
2224
self.type_info = type_info
25+
self.non_static_modules = non_static_modules
26+
27+
def get_flags(
28+
self, module_name: str, pyast: ast.Module, override_flags: Flags
29+
) -> Flags:
30+
if self.non_static_modules is not None:
31+
if module_name in self.non_static_modules:
32+
return Flags().merge(override_flags)
33+
34+
return Flags(is_static=True).merge(override_flags)
35+
36+
return super().get_flags(module_name, pyast, override_flags)
2337

2438
def make_type_binder(
2539
self,

cinderx/PythonLib/cinderx/compiler/strict/compiler.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ def _setup_logging(self) -> logging.Logger:
185185
logger.setLevel(logging.DEBUG)
186186
return logger
187187

188+
def get_flags(
189+
self, module_name: str, pyast: ast.Module, override_flags: Flags
190+
) -> Flags:
191+
return FlagExtractor().get_flags(pyast).merge(override_flags)
192+
188193
def load_compiled_module_from_source(
189194
self,
190195
source: str | bytes,
@@ -197,7 +202,7 @@ def load_compiled_module_from_source(
197202
pyast = ast.parse(source)
198203
# pyre-fixme[6]: For 1st argument expected `str` but got `Union[bytes, str]`.
199204
symbols = symtable.symtable(source, filename, "exec")
200-
flags = FlagExtractor().get_flags(pyast).merge(override_flags)
205+
flags = self.get_flags(name, pyast, override_flags)
201206

202207
if not flags.is_static and not flags.is_strict:
203208
code = self._compile_basic(name, pyast, filename, optimize)

cinderx/PythonLib/test_cinderx/test_compiler/test_static/pyrefly_binder.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,46 @@
44
import unittest
55
from types import CodeType
66

7-
from cinderx.compiler.static import StaticCodeGenerator
87
from cinderx.compiler.static.pyrefly_compiler import PyreflyCompiler
98
from cinderx.compiler.static.pyrefly_type_binder import PyreflyTypeInfo
109

1110
from .common import StaticTestBase
1211

1312

13+
EMPTY_TYPE_INFO = PyreflyTypeInfo(
14+
{
15+
"type_table": [],
16+
"locations": [],
17+
}
18+
)
19+
20+
1421
class PyreBinderTests(StaticTestBase):
1522
def test_simple(self):
16-
code = self.compile_one(
23+
self.compile_one(
1724
"x = 1 + 2",
18-
PyreflyTypeInfo(
19-
{
20-
"type_table": [],
21-
"locations": [],
22-
}
23-
),
25+
EMPTY_TYPE_INFO,
26+
)
27+
28+
def test_force_static(self):
29+
compiler = PyreflyCompiler(non_static_modules=set(), type_info=EMPTY_TYPE_INFO)
30+
code, strict, static = compiler.load_compiled_module_from_source(
31+
"def f(x: int): return x.bit_length()", "foo.py", "foo", 0
32+
)
33+
self.assertTrue(static)
34+
self.assertFalse(strict)
35+
self.assertIn("<fixed-modules>", code.co_names)
36+
37+
def test_non_force_static(self):
38+
compiler = PyreflyCompiler(
39+
non_static_modules={"foo"}, type_info=EMPTY_TYPE_INFO
40+
)
41+
code, strict, static = compiler.load_compiled_module_from_source(
42+
"def f(x: int): return x.bit_length()", "foo.py", "foo", 0
2443
)
44+
self.assertFalse(static)
45+
self.assertFalse(strict)
46+
self.assertNotIn("<fixed-modules>", code.co_names)
2547

2648
def compile_one(
2749
self,
@@ -32,7 +54,7 @@ def compile_one(
3254
ast_optimizer_enabled: bool = True,
3355
enable_patching: bool = False,
3456
) -> CodeType:
35-
compiler = PyreflyCompiler(StaticCodeGenerator, type_info=type_info)
57+
compiler = PyreflyCompiler(type_info=type_info)
3658
tree = ast.parse(self.clean_code(code))
3759
return compiler.compile(
3860
modname,

0 commit comments

Comments
 (0)