Skip to content

Commit 6a434b4

Browse files
committed
Use feature flag in INCOMPLETE_FEATURES
1 parent 317228f commit 6a434b4

File tree

6 files changed

+24
-5
lines changed

6 files changed

+24
-5
lines changed

mypy/options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class BuildType:
7575
PRECISE_TUPLE_TYPES: Final = "PreciseTupleTypes"
7676
NEW_GENERIC_SYNTAX: Final = "NewGenericSyntax"
7777
INLINE_TYPEDDICT: Final = "InlineTypedDict"
78-
INCOMPLETE_FEATURES: Final = frozenset((PRECISE_TUPLE_TYPES, INLINE_TYPEDDICT))
78+
MYPYC_VALUE_TYPES: Final = "MypycValueTypes"
79+
INCOMPLETE_FEATURES: Final = frozenset((PRECISE_TUPLE_TYPES, INLINE_TYPEDDICT, MYPYC_VALUE_TYPES))
7980
COMPLETE_FEATURES: Final = frozenset((TYPE_VAR_TUPLE, UNPACK, NEW_GENERIC_SYNTAX))
8081

8182

mypyc/build.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from mypy.errors import CompileError
3232
from mypy.fscache import FileSystemCache
3333
from mypy.main import process_options
34-
from mypy.options import Options
34+
from mypy.options import MYPYC_VALUE_TYPES, Options
3535
from mypy.util import write_junit_xml
3636
from mypyc.codegen import emitmodule
3737
from mypyc.common import RUNTIME_C_FILES, shared_lib_name
@@ -418,6 +418,11 @@ def mypyc_build(
418418
paths, only_compile_paths, compiler_options, fscache
419419
)
420420

421+
# Enable value types option via mypy options
422+
compiler_options.experimental_value_types = (
423+
MYPYC_VALUE_TYPES in options.enable_incomplete_feature
424+
)
425+
421426
# We generate a shared lib if there are multiple modules or if any
422427
# of the modules are in package. (Because I didn't want to fuss
423428
# around with making the single module code handle packages.)

mypyc/codegen/emit.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@ def emit_inc_ref(self, dest: str, rtype: RType, *, rare: bool = False) -> None:
526526
elif isinstance(rtype, RTuple):
527527
for i, item_type in enumerate(rtype.types):
528528
self.emit_inc_ref(f"{dest}.f{i}", item_type)
529+
elif isinstance(rtype, RInstanceValue):
530+
for i, (attr, attr_type) in enumerate(rtype.class_ir.all_attributes().items()):
531+
self.emit_inc_ref(f"{dest}.{self.attr(attr)}", attr_type)
529532
elif not rtype.is_unboxed:
530533
# Always inline, since this is a simple op
531534
self.emit_line("CPy_INCREF(%s);" % dest)
@@ -1089,7 +1092,7 @@ def emit_box(
10891092
attr_name = self.attr(attr)
10901093
self.emit_line(f"{temp_dest}->{attr_name} = {src}.{attr_name};", ann="box")
10911094
if attr_type.is_refcounted:
1092-
self.emit_inc_ref(temp_dest, attr_type)
1095+
self.emit_inc_ref(f"{temp_dest}->{attr_name}", attr_type)
10931096

10941097
self.emit_line(f"{declaration}{dest} = (PyObject *){temp_dest};")
10951098
else:
@@ -1131,6 +1134,9 @@ def emit_gc_visit(self, target: str, rtype: RType) -> None:
11311134
elif isinstance(rtype, RTuple):
11321135
for i, item_type in enumerate(rtype.types):
11331136
self.emit_gc_visit(f"{target}.f{i}", item_type)
1137+
elif isinstance(rtype, RInstanceValue):
1138+
for i, (attr, attr_type) in enumerate(rtype.class_ir.all_attributes().items()):
1139+
self.emit_gc_visit(f"{target}.{self.attr(attr)}", attr_type)
11341140
elif self.ctype(rtype) == "PyObject *":
11351141
# The simplest case.
11361142
self.emit_line(f"Py_VISIT({target});")
@@ -1155,6 +1161,9 @@ def emit_gc_clear(self, target: str, rtype: RType) -> None:
11551161
elif isinstance(rtype, RTuple):
11561162
for i, item_type in enumerate(rtype.types):
11571163
self.emit_gc_clear(f"{target}.f{i}", item_type)
1164+
elif isinstance(rtype, RInstanceValue):
1165+
for i, (attr, attr_type) in enumerate(rtype.class_ir.all_attributes().items()):
1166+
self.emit_gc_clear(f"{target}.{self.attr(attr)}", attr_type)
11581167
elif self.ctype(rtype) == "PyObject *" and self.c_undefined_value(rtype) == "NULL":
11591168
# The simplest case.
11601169
self.emit_line(f"Py_CLEAR({target});")

mypyc/irbuild/prepare.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def build_type_map(
9191
is_abstract=cdef.info.is_abstract,
9292
is_final_class=cdef.info.is_final,
9393
is_ext_class=is_extension_class(cdef),
94-
is_value_type=is_value_type(cdef),
94+
is_value_type=is_value_type(cdef) and options.experimental_value_types,
9595
)
9696

9797
if class_ir.is_value_type:

mypyc/options.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ def __init__(
3838
# will assume the return type of the method strictly, which can lead to
3939
# more optimization opportunities.
4040
self.strict_dunders_typing = strict_dunder_typing
41+
# Enable value types for the generated code. This option is experimental until
42+
# the feature reference get removed from INCOMPLETE_FEATURES.
43+
self.experimental_value_types = True # overridden by the mypy command line option

mypyc/test/test_run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from mypy import build
1717
from mypy.errors import CompileError
18-
from mypy.options import Options
18+
from mypy.options import MYPYC_VALUE_TYPES, Options
1919
from mypy.test.config import test_temp_dir
2020
from mypy.test.data import DataDrivenTestCase
2121
from mypy.test.helpers import assert_module_equivalence, perform_file_operations
@@ -199,6 +199,7 @@ def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) ->
199199
options.preserve_asts = True
200200
options.allow_empty_bodies = True
201201
options.incremental = self.separate
202+
options.enable_incomplete_feature.append(MYPYC_VALUE_TYPES)
202203

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

0 commit comments

Comments
 (0)