Skip to content

Commit 2b2c690

Browse files
committed
ci: apply black formatting + fix ruff errors + repair NameError
- Run black on files that diverged from the 25.9.0 pre-commit config. - Fix ruff UP006/UP035 (typing.Dict/List -> dict/list) in the small root-level test fixtures. - emitfunc.emit_method_call: the guarded-direct path (added by the allow_interpreted_subclasses commit) referenced a no-longer-defined `method` variable. Use method_decl through the new helpers to match the direct call path. Fixes type check and 5 run-classes tests.
1 parent bd88c2f commit 2b2c690

11 files changed

Lines changed: 54 additions & 48 deletions

mypyc/codegen/emitclass.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,12 @@ def emit_line() -> None:
364364
generate_methods_table(cl, methods_name, setup_name if generate_full else None, emitter)
365365
emit_line()
366366

367-
flags = ["Py_TPFLAGS_DEFAULT", "Py_TPFLAGS_HEAPTYPE", "Py_TPFLAGS_BASETYPE", "CPy_TPFLAGS_MYPYC_COMPILED"]
367+
flags = [
368+
"Py_TPFLAGS_DEFAULT",
369+
"Py_TPFLAGS_HEAPTYPE",
370+
"Py_TPFLAGS_BASETYPE",
371+
"CPy_TPFLAGS_MYPYC_COMPILED",
372+
]
368373
if generate_full:
369374
flags.append("Py_TPFLAGS_HAVE_GC")
370375
if cl.has_method("__call__"):

mypyc/codegen/emitfunc.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,9 +644,7 @@ def emit_method_call(self, dest: str, op_obj: Value, name: str, op_args: list[Va
644644
obj_args = (
645645
[]
646646
if method_decl.kind == FUNC_STATICMETHOD
647-
else [f"(PyObject *)Py_TYPE({obj})"]
648-
if method_decl.kind == FUNC_CLASSMETHOD
649-
else [obj]
647+
else [f"(PyObject *)Py_TYPE({obj})"] if method_decl.kind == FUNC_CLASSMETHOD else [obj]
650648
)
651649
args = ", ".join(obj_args + [self.reg(arg) for arg in op_args])
652650
mtype = native_function_type_from_decl(method_decl, self.emitter)
@@ -665,8 +663,7 @@ def emit_method_call(self, dest: str, op_obj: Value, name: str, op_args: list[Va
665663
and class_ir.is_method_final_among_compiled(name)
666664
)
667665
if use_guarded_direct:
668-
lib = self.emitter.get_group_prefix(method.decl)
669-
direct_call = f"{lib}{NATIVE_PREFIX}{method.cname(self.names)}({args})"
666+
direct_call = f"{self.emitter.native_function_call(method_decl)}({args})"
670667
self.emit_line(f"if (Py_TYPE({obj})->tp_flags & CPy_TPFLAGS_MYPYC_COMPILED) {{")
671668
self.emit_line(f"{dest}{direct_call};")
672669
self.emit_line("} else {")

mypyc/codegen/emitmodule.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -899,10 +899,10 @@ def generate_shared_lib_init(self, emitter: Emitter) -> None:
899899
)
900900
if self.context.group_deps:
901901
emitter.emit_line(
902-
'static PyObject *_mypyc_fromlist = NULL; '
903-
'if (!_mypyc_fromlist) { '
902+
"static PyObject *_mypyc_fromlist = NULL; "
903+
"if (!_mypyc_fromlist) { "
904904
'_mypyc_fromlist = Py_BuildValue("(s)", "*"); '
905-
'if (!_mypyc_fromlist) return -1; }'
905+
"if (!_mypyc_fromlist) return -1; }"
906906
)
907907
emitter.emit_line("PyObject *tmp;")
908908
emitter.emit_line("PyObject *caps;")
@@ -920,7 +920,7 @@ def generate_shared_lib_init(self, emitter: Emitter) -> None:
920920
'caps = PyObject_GetAttrString(tmp, "exports");',
921921
"Py_DECREF(tmp);",
922922
"if (!caps) return -1;",
923-
'struct export_table_{g} *pexports_{g} = '
923+
"struct export_table_{g} *pexports_{g} = "
924924
'(struct export_table_{g} *)PyCapsule_GetPointer(caps, "{lib}.exports");'.format(
925925
g=egroup, lib=shared_lib_name(group)
926926
),

mypyc/codegen/emitwrapper.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,7 @@ def generate_set_del_item_wrapper_inner(
706706
native_args = ", ".join(f"arg_{arg.name}" for arg in args)
707707
emitter.emit_line(
708708
"{}val = {}({});".format(
709-
emitter.ctype_spaced(fn.ret_type),
710-
emitter.native_function_call(fn.decl),
711-
native_args,
709+
emitter.ctype_spaced(fn.ret_type), emitter.native_function_call(fn.decl), native_args
712710
)
713711
)
714712
emitter.emit_error_check("val", fn.ret_type, "goto fail;")
@@ -946,9 +944,7 @@ def emit_call(self, not_implemented_handler: str = "") -> None:
946944
else:
947945
if not_implemented_handler and not isinstance(ret_type, RInstance):
948946
# The return value type may overlap with NotImplemented.
949-
emitter.emit_line(
950-
f"PyObject *retbox = {self.target_native_call}({native_args});"
951-
)
947+
emitter.emit_line(f"PyObject *retbox = {self.target_native_call}({native_args});")
952948
emitter.emit_lines(
953949
"if (retbox == Py_NotImplemented) {",
954950
not_implemented_handler,

mypyc/irbuild/env_class.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ def finalize_env_class(builder: IRBuilder, prefix: str = "") -> None:
7676
# environment class. Comprehension scopes have no arguments to add.
7777
if not builder.fn_info.is_comprehension_scope:
7878
if builder.fn_info.is_nested:
79-
add_args_to_env(builder, local=False, base=builder.fn_info.callable_class, prefix=prefix)
79+
add_args_to_env(
80+
builder, local=False, base=builder.fn_info.callable_class, prefix=prefix
81+
)
8082
else:
8183
add_args_to_env(builder, local=False, base=builder.fn_info, prefix=prefix)
8284

@@ -101,7 +103,11 @@ def instantiate_env_class(builder: IRBuilder) -> Value:
101103
# Top-level functions and comprehension scopes store env reg directly.
102104
builder.fn_info._curr_env_reg = curr_env_reg
103105
# Comprehension scopes link to parent env if it exists.
104-
if builder.fn_info.is_nested and builder.fn_infos[-2]._env_class is not None and builder.fn_infos[-2]._curr_env_reg is not None:
106+
if (
107+
builder.fn_info.is_nested
108+
and builder.fn_infos[-2]._env_class is not None
109+
and builder.fn_infos[-2]._curr_env_reg is not None
110+
):
105111
builder.add(
106112
SetAttr(
107113
curr_env_reg,
@@ -167,7 +173,9 @@ def load_outer_envs(builder: IRBuilder, base: ImplicitClass) -> None:
167173

168174
# Load the first outer environment. This one is special because it gets saved in the
169175
# FuncInfo instance's prev_env_reg field.
170-
if (index > 1 or (index == 1 and builder.fn_infos[1].contains_nested)) and builder.fn_infos[index]._env_class is not None:
176+
if (index > 1 or (index == 1 and builder.fn_infos[1].contains_nested)) and builder.fn_infos[
177+
index
178+
]._env_class is not None:
171179
# outer_env = builder.fn_infos[index].environment
172180
outer_env = builder.symtables[index]
173181
if isinstance(base, GeneratorClass):
@@ -234,7 +242,9 @@ def add_vars_to_env(builder: IRBuilder, prefix: str = "") -> None:
234242
env_for_func: FuncInfo | ImplicitClass = builder.fn_info
235243
if builder.fn_info.is_generator:
236244
env_for_func = builder.fn_info.generator_class
237-
elif (builder.fn_info.is_nested or builder.fn_info.in_non_ext) and not builder.fn_info.is_comprehension_scope:
245+
elif (
246+
builder.fn_info.is_nested or builder.fn_info.in_non_ext
247+
) and not builder.fn_info.is_comprehension_scope:
238248
env_for_func = builder.fn_info.callable_class
239249

240250
if builder.fn_info.fitem in builder.free_variables:

mypyc/irbuild/expression.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,7 @@ def transform_name_expr(builder: IRBuilder, expr: NameExpr) -> Value:
206206
return builder.py_get_attr(builder.class_body_obj, expr.name, expr.line)
207207
else:
208208
return builder.primitive_op(
209-
dict_get_item_op,
210-
[builder.class_body_obj, builder.load_str(expr.name)],
211-
expr.line,
209+
dict_get_item_op, [builder.class_body_obj, builder.load_str(expr.name)], expr.line
212210
)
213211

214212
return builder.load_global(expr)
@@ -1190,9 +1188,7 @@ def transform_dictionary_comprehension(builder: IRBuilder, o: DictionaryComprehe
11901188
return builder.none()
11911189

11921190
if o in builder.comp_to_fitem:
1193-
return _translate_comprehension_with_scope(
1194-
builder, o, lambda: _dict_comp_body(builder, o)
1195-
)
1191+
return _translate_comprehension_with_scope(builder, o, lambda: _dict_comp_body(builder, o))
11961192
return _dict_comp_body(builder, o)
11971193

11981194

mypyc/irbuild/function.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -848,12 +848,7 @@ def gen_glue_property(
848848

849849

850850
def gen_glue_property_setter(
851-
builder: IRBuilder,
852-
sig: FuncSignature,
853-
target: FuncIR,
854-
cls: ClassIR,
855-
base: ClassIR,
856-
line: int,
851+
builder: IRBuilder, sig: FuncSignature, target: FuncIR, cls: ClassIR, base: ClassIR, line: int
857852
) -> FuncIR:
858853
"""Generate a shadow glue method for a property setter.
859854
@@ -875,7 +870,7 @@ def gen_glue_property_setter(
875870

876871
# Extract the property name from "__mypyc_setter__<name>"
877872
# mypyc encodes e.g. "_chunks" as "__mypyc_setter___3_chunks"
878-
prop_name = target.name[len("__mypyc_setter__"):]
873+
prop_name = target.name[len("__mypyc_setter__") :]
879874
# Decode mypyc's private name mangling: "_3_foo" -> "_foo", "_3foo" -> "_foo"
880875
if prop_name.startswith("_") and len(prop_name) > 1 and prop_name[1].isdigit():
881876
end_of_digits = 2
@@ -885,7 +880,11 @@ def gen_glue_property_setter(
885880

886881
builder.primitive_op(
887882
py_setattr_op,
888-
[self_arg, builder.load_str(prop_name), builder.coerce(value_arg, object_rprimitive, line)],
883+
[
884+
self_arg,
885+
builder.load_str(prop_name),
886+
builder.coerce(value_arg, object_rprimitive, line),
887+
],
889888
line,
890889
)
891890
# Return 1 (success). If py_setattr_op failed, the Python exception is set

mypyc/irbuild/prebuildvisitor.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ def visit_func(self, func: FuncItem) -> None:
192192
super().visit_func(func)
193193
self.funcs.pop()
194194

195-
def _visit_comprehension_with_scope(
196-
self, o: GeneratorExpr | DictionaryComprehension
197-
) -> None:
195+
def _visit_comprehension_with_scope(self, o: GeneratorExpr | DictionaryComprehension) -> None:
198196
"""Visit a comprehension that contains lambdas.
199197
200198
Creates a synthetic FuncDef to represent the comprehension's scope,

test_subclass_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""Compiled base class."""
2+
23
from mypy_extensions import mypyc_attr
3-
from typing import Dict
44

55

66
@mypyc_attr(allow_interpreted_subclasses=True)
77
class Base:
8-
MAPPING: Dict[str, int] = {"a": 1, "b": 2}
8+
MAPPING: dict[str, int] = {"a": 1, "b": 2}
99
FLAG: bool = False
1010

1111
def lookup(self, key: str) -> int:

test_subclass_child.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Non-compiled child class — tests attribute override visibility."""
2+
23
from test_subclass_base import Base
34

45

@@ -17,7 +18,9 @@ def test() -> None:
1718
s = Sub()
1819
assert s.lookup("a") == 1
1920
result = s.lookup("c")
20-
assert result == 3, f"Expected 3, got {result} — compiled method doesn't see subclass override!"
21+
assert (
22+
result == 3
23+
), f"Expected 3, got {result} — compiled method doesn't see subclass override!"
2124
assert s.check_flag(), "Expected True — compiled method doesn't see subclass override!"
2225
print("Sub OK — interpreted subclass overrides work!")
2326

0 commit comments

Comments
 (0)