Skip to content

Commit

Permalink
Use feature flag in INCOMPLETE_FEATURES
Browse files Browse the repository at this point in the history
  • Loading branch information
jairov4 committed Nov 24, 2024
1 parent 317228f commit 6a434b4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class BuildType:
PRECISE_TUPLE_TYPES: Final = "PreciseTupleTypes"
NEW_GENERIC_SYNTAX: Final = "NewGenericSyntax"
INLINE_TYPEDDICT: Final = "InlineTypedDict"
INCOMPLETE_FEATURES: Final = frozenset((PRECISE_TUPLE_TYPES, INLINE_TYPEDDICT))
MYPYC_VALUE_TYPES: Final = "MypycValueTypes"
INCOMPLETE_FEATURES: Final = frozenset((PRECISE_TUPLE_TYPES, INLINE_TYPEDDICT, MYPYC_VALUE_TYPES))
COMPLETE_FEATURES: Final = frozenset((TYPE_VAR_TUPLE, UNPACK, NEW_GENERIC_SYNTAX))


Expand Down
7 changes: 6 additions & 1 deletion mypyc/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from mypy.errors import CompileError
from mypy.fscache import FileSystemCache
from mypy.main import process_options
from mypy.options import Options
from mypy.options import MYPYC_VALUE_TYPES, Options
from mypy.util import write_junit_xml
from mypyc.codegen import emitmodule
from mypyc.common import RUNTIME_C_FILES, shared_lib_name
Expand Down Expand Up @@ -418,6 +418,11 @@ def mypyc_build(
paths, only_compile_paths, compiler_options, fscache
)

# Enable value types option via mypy options
compiler_options.experimental_value_types = (
MYPYC_VALUE_TYPES in options.enable_incomplete_feature
)

# We generate a shared lib if there are multiple modules or if any
# of the modules are in package. (Because I didn't want to fuss
# around with making the single module code handle packages.)
Expand Down
11 changes: 10 additions & 1 deletion mypyc/codegen/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ def emit_inc_ref(self, dest: str, rtype: RType, *, rare: bool = False) -> None:
elif isinstance(rtype, RTuple):
for i, item_type in enumerate(rtype.types):
self.emit_inc_ref(f"{dest}.f{i}", item_type)
elif isinstance(rtype, RInstanceValue):
for i, (attr, attr_type) in enumerate(rtype.class_ir.all_attributes().items()):
self.emit_inc_ref(f"{dest}.{self.attr(attr)}", attr_type)
elif not rtype.is_unboxed:
# Always inline, since this is a simple op
self.emit_line("CPy_INCREF(%s);" % dest)
Expand Down Expand Up @@ -1089,7 +1092,7 @@ def emit_box(
attr_name = self.attr(attr)
self.emit_line(f"{temp_dest}->{attr_name} = {src}.{attr_name};", ann="box")
if attr_type.is_refcounted:
self.emit_inc_ref(temp_dest, attr_type)
self.emit_inc_ref(f"{temp_dest}->{attr_name}", attr_type)

self.emit_line(f"{declaration}{dest} = (PyObject *){temp_dest};")
else:
Expand Down Expand Up @@ -1131,6 +1134,9 @@ def emit_gc_visit(self, target: str, rtype: RType) -> None:
elif isinstance(rtype, RTuple):
for i, item_type in enumerate(rtype.types):
self.emit_gc_visit(f"{target}.f{i}", item_type)
elif isinstance(rtype, RInstanceValue):
for i, (attr, attr_type) in enumerate(rtype.class_ir.all_attributes().items()):
self.emit_gc_visit(f"{target}.{self.attr(attr)}", attr_type)
elif self.ctype(rtype) == "PyObject *":
# The simplest case.
self.emit_line(f"Py_VISIT({target});")
Expand All @@ -1155,6 +1161,9 @@ def emit_gc_clear(self, target: str, rtype: RType) -> None:
elif isinstance(rtype, RTuple):
for i, item_type in enumerate(rtype.types):
self.emit_gc_clear(f"{target}.f{i}", item_type)
elif isinstance(rtype, RInstanceValue):
for i, (attr, attr_type) in enumerate(rtype.class_ir.all_attributes().items()):
self.emit_gc_clear(f"{target}.{self.attr(attr)}", attr_type)
elif self.ctype(rtype) == "PyObject *" and self.c_undefined_value(rtype) == "NULL":
# The simplest case.
self.emit_line(f"Py_CLEAR({target});")
Expand Down
2 changes: 1 addition & 1 deletion mypyc/irbuild/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def build_type_map(
is_abstract=cdef.info.is_abstract,
is_final_class=cdef.info.is_final,
is_ext_class=is_extension_class(cdef),
is_value_type=is_value_type(cdef),
is_value_type=is_value_type(cdef) and options.experimental_value_types,
)

if class_ir.is_value_type:
Expand Down
3 changes: 3 additions & 0 deletions mypyc/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ def __init__(
# will assume the return type of the method strictly, which can lead to
# more optimization opportunities.
self.strict_dunders_typing = strict_dunder_typing
# Enable value types for the generated code. This option is experimental until
# the feature reference get removed from INCOMPLETE_FEATURES.
self.experimental_value_types = True # overridden by the mypy command line option
3 changes: 2 additions & 1 deletion mypyc/test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from mypy import build
from mypy.errors import CompileError
from mypy.options import Options
from mypy.options import MYPYC_VALUE_TYPES, Options
from mypy.test.config import test_temp_dir
from mypy.test.data import DataDrivenTestCase
from mypy.test.helpers import assert_module_equivalence, perform_file_operations
Expand Down Expand Up @@ -199,6 +199,7 @@ def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) ->
options.preserve_asts = True
options.allow_empty_bodies = True
options.incremental = self.separate
options.enable_incomplete_feature.append(MYPYC_VALUE_TYPES)

# Avoid checking modules/packages named 'unchecked', to provide a way
# to test interacting with code we don't have types for.
Expand Down

0 comments on commit 6a434b4

Please sign in to comment.