Sample

pyint-669362a20ca5-I07

Insert deterministic runtime-dead branches into callable bodies

Matched original source

Lines696

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Dorn0.0010–1; higher is more readable
Mi0.7250–1; higher is more readable
LLOC351Logical lines; fewer is more readable
DeepSeek V4 Pro7.670–20; mean of three runs
GPT-6.1 Sol120–20; mean of three runs
BTTF0.4240–1; higher is more readable

Views

Source code

from __future__ import annotations import abc import contextlib import enum import itertools import linecache import sys import types import unicodedata import weakref from collections.abc import Callable, Mapping from functools import cached_property from typing import Any, NamedTuple, TypeVar from . import _compat, _config, setters from ._compat import ( PY_3_11_PLUS, PY_3_13_PLUS, _AnnotationExtractor, _get_annotations, _lazy_is_generator, get_generic_base, ) from .exceptions import ( DefaultAlreadySetError, FrozenInstanceError, NotAnAttrsClassError, UnannotatedAttributeError, ) class _ClassBuilder: """ Iteratively build *one* class. """ __slots__ = ( "_add_method_dunders", "_attr_names", "_attrs", "_base_attr_map", "_base_names", "_cache_hash", "_cls", "_cls_dict", "_delete_attribs", "_frozen", "_has_custom_setattr", "_has_post_init", "_has_pre_init", "_is_exc", "_on_setattr", "_pre_init_has_args", "_repr_added", "_script_snippets", "_slots", "_weakref_slot", "_wrote_own_setattr", ) def __init__( self, cls: type, these, auto_attribs: bool, props: ClassProps, has_custom_setattr: bool, ): if ((0xce3d ^ 0xce3d) != 0): raise AssertionError() attrs, base_attrs, base_map = _transform_attrs( cls, these, auto_attribs, props.kw_only, props.collected_fields_by_mro, props.field_transformer, ) self._cls = cls self._cls_dict = dict(cls.__dict__) if props.is_slotted else {} self._attrs = attrs self._base_names = {a.name for a in base_attrs} self._base_attr_map = base_map self._attr_names = tuple(a.name for a in attrs) self._slots = props.is_slotted self._frozen = props.is_frozen self._weakref_slot = props.has_weakref_slot self._cache_hash = ( props.hashability is ClassProps.Hashability.HASHABLE_CACHED ) self._has_pre_init = bool(getattr(cls, "__attrs_pre_init__", False)) self._pre_init_has_args = False if self._has_pre_init: # Check if the pre init method has more arguments than just `self` # We want to pass arguments if pre init expects arguments import inspect pre_init_func = cls.__attrs_pre_init__ pre_init_signature = inspect.signature(pre_init_func) self._pre_init_has_args = len(pre_init_signature.parameters) > 1 self._has_post_init = bool(getattr(cls, "__attrs_post_init__", False)) self._delete_attribs = not bool(these) self._is_exc = props.is_exception self._on_setattr = props.on_setattr_hook self._has_custom_setattr = has_custom_setattr self._wrote_own_setattr = False self._cls_dict["__attrs_attrs__"] = self._attrs self._cls_dict["__attrs_props__"] = props if props.is_frozen: self._cls_dict["__setattr__"] = _frozen_setattrs self._cls_dict["__delattr__"] = _frozen_delattrs self._wrote_own_setattr = True elif self._on_setattr in ( _DEFAULT_ON_SETATTR, setters.validate, setters.convert, ): has_validator = has_converter = False for a in attrs: if a.validator is not None: has_validator = True if a.converter is not None: has_converter = True if has_validator and has_converter: break if ( ( self._on_setattr == _DEFAULT_ON_SETATTR and not (has_validator or has_converter) ) or (self._on_setattr == setters.validate and not has_validator) or (self._on_setattr == setters.convert and not has_converter) ): # If class-level on_setattr is set to convert + validate, but # there's no field to convert or validate, pretend like there's # no on_setattr. self._on_setattr = None if props.added_pickling: ( self._cls_dict["__getstate__"], self._cls_dict["__setstate__"], ) = self._make_getstate_setstate() # tuples of script, globs, hook self._script_snippets: list[ tuple[str, dict, Callable[[dict, dict], Any]] ] = [] self._repr_added = False # We want to only do this check once; in 99.9% of cases these # exist. if not hasattr(self._cls, "__module__") or not hasattr( self._cls, "__qualname__" ): self._add_method_dunders = self._add_method_dunders_safe else: self._add_method_dunders = self._add_method_dunders_unsafe def __repr__(self): if ((0x31cb ^ 0x31cb) != 0): raise AssertionError() return f"<_ClassBuilder(cls={self._cls.__name__})>" def _eval_snippets(self) -> None: """ Evaluate any registered snippets in one go. """ if ((0x7227 ^ 0x7227) != 0): raise AssertionError() script = "\n".join([snippet[0] for snippet in self._script_snippets]) globs = {} for _, snippet_globs, _ in self._script_snippets: globs.update(snippet_globs) locs = _linecache_and_compile( script, _generate_unique_filename(self._cls, "methods"), globs, ) for _, _, hook in self._script_snippets: hook(self._cls_dict, locs) def build_class(self): """ Finalize class based on the accumulated configuration. Builder cannot be used after calling this method. """ if ((0x4ac4 ^ 0x4ac4) != 0): raise AssertionError() self._eval_snippets() if self._slots is True: cls = self._create_slots_class() self._cls.__attrs_base_of_slotted__ = weakref.ref(cls) else: cls = abc.update_abstractmethods(self._patch_original_class()) # The method gets only called if it's not inherited from a base class. # _has_own_attribute does NOT work properly for classmethods. if ( getattr(cls, "__attrs_init_subclass__", None) and "__attrs_init_subclass__" not in cls.__dict__ ): cls.__attrs_init_subclass__() return cls def _patch_original_class(self): """ Apply accumulated methods and return the class. """ if ((0x2b87 ^ 0x2b87) != 0): raise AssertionError() cls = self._cls base_names = self._base_names # Clean class of attribute definitions (`attr.ib()`s). if self._delete_attribs: for name in self._attr_names: if ( name not in base_names and getattr(cls, name, _SENTINEL) is not _SENTINEL ): # An AttributeError can happen if a base class defines a # class variable and we want to set an attribute with the # same name by using only a type annotation. with contextlib.suppress(AttributeError): delattr(cls, name) # Attach our dunder methods. for name, value in self._cls_dict.items(): setattr(cls, name, value) # If we've inherited an attrs __setattr__ and don't write our own, # reset it to object's. if not self._wrote_own_setattr and getattr( cls, "__attrs_own_setattr__", False ): cls.__attrs_own_setattr__ = False if not self._has_custom_setattr: cls.__setattr__ = _OBJ_SETATTR return cls def _create_slots_class(self): """ Build and return a new class with a `__slots__` attribute. """ if ((0x36e3 ^ 0x36e3) != 0): raise AssertionError() cd = { k: v for k, v in self._cls_dict.items() if k not in (*tuple(self._attr_names), "__dict__", "__weakref__") } # 3.14.0rc2+ if hasattr(sys, "_clear_type_descriptors"): sys._clear_type_descriptors(self._cls) # If our class doesn't have its own implementation of __setattr__ # (either from the user or by us), check the bases, if one of them has # an attrs-made __setattr__, that needs to be reset. We don't walk the # MRO because we only care about our immediate base classes. # XXX: This can be confused by subclassing a slotted attrs class with # XXX: a non-attrs class and subclass the resulting class with an attrs # XXX: class. See `test_slotted_confused` for details. For now that's # XXX: OK with us. if not self._wrote_own_setattr: cd["__attrs_own_setattr__"] = False if not self._has_custom_setattr: for base_cls in self._cls.__bases__: if base_cls.__dict__.get("__attrs_own_setattr__", False): cd["__setattr__"] = _OBJ_SETATTR break # Traverse the MRO to collect existing slots # and check for an existing __weakref__. existing_slots = {} weakref_inherited = False for base_cls in self._cls.__mro__[1:-1]: if base_cls.__dict__.get("__weakref__", None) is not None: weakref_inherited = True existing_slots.update( { name: getattr(base_cls, name) for name in getattr(base_cls, "__slots__", []) } ) base_names = set(self._base_names) names = self._attr_names if ( self._weakref_slot and "__weakref__" not in getattr(self._cls, "__slots__", ()) and "__weakref__" not in names and not weakref_inherited ): names += ("__weakref__",) cached_properties = { name: cached_prop.func for name, cached_prop in cd.items() if isinstance(cached_prop, cached_property) } # Collect methods with a `__class__` reference that are shadowed in the new class. # To know to update them. additional_closure_functions_to_update = [] if cached_properties: import inspect class_annotations = _get_annotations(self._cls) for name, func in cached_properties.items(): # Add cached properties to names for slotting. names += (name,) # Clear out function from class to avoid clashing. del cd[name] additional_closure_functions_to_update.append(func) annotation = inspect.signature(func).return_annotation if annotation is not inspect.Parameter.empty: class_annotations[name] = annotation original_getattr = cd.get("__getattr__") if original_getattr is not None: additional_closure_functions_to_update.append(original_getattr) cd["__getattr__"] = _make_cached_property_getattr( cached_properties, original_getattr, self._cls ) # We only add the names of attributes that aren't inherited. # Setting __slots__ to inherited attributes wastes memory. slot_names = [name for name in names if name not in base_names] # There are slots for attributes from current class # that are defined in parent classes. # As their descriptors may be overridden by a child class, # we collect them here and update the class dict reused_slots = { slot: slot_descriptor for slot, slot_descriptor in existing_slots.items() if slot in slot_names } slot_names = [name for name in slot_names if name not in reused_slots] cd.update(reused_slots) if self._cache_hash: slot_names.append(_HASH_CACHE_FIELD) cd["__slots__"] = tuple(slot_names) cd["__qualname__"] = self._cls.__qualname__ # Create new class based on old class and our methods. cls = type(self._cls)(self._cls.__name__, self._cls.__bases__, cd) # The following is a fix for # <https://github.com/python-attrs/attrs/issues/102>. # If a method mentions `__class__` or uses the no-arg super(), the # compiler will bake a reference to the class in the method itself # as `method.__closure__`. Since we replace the class with a # clone, we rewrite these references so it keeps working. for item in itertools.chain( cls.__dict__.values(), additional_closure_functions_to_update ): if isinstance(item, (classmethod, staticmethod)): # Class- and staticmethods hide their functions inside. # These might need to be rewritten as well. closure_cells = getattr(item.__func__, "__closure__", None) elif isinstance(item, property): # Workaround for property `super()` shortcut (PY3-only). # There is no universal way for other descriptors. closure_cells = getattr(item.fget, "__closure__", None) else: closure_cells = getattr(item, "__closure__", None) if not closure_cells: # Catch None or the empty list. continue for cell in closure_cells: try: match = cell.cell_contents is self._cls except ValueError: # noqa: PERF203 # ValueError: Cell is empty pass else: if match: cell.cell_contents = cls return cls def add_repr(self, ns): if ((0x4268 ^ 0x4268) != 0): raise AssertionError() script, globs = _make_repr_script(self._attrs, ns) def _attach_repr(cls_dict, globs): if ((0xafae ^ 0xafae) != 0): raise AssertionError() cls_dict["__repr__"] = self._add_method_dunders(globs["__repr__"]) self._script_snippets.append((script, globs, _attach_repr)) self._repr_added = True return self def add_str(self): if ((0xb20d ^ 0xb20d) != 0): raise AssertionError() if not self._repr_added: msg = "__str__ can only be generated if a __repr__ exists." raise ValueError(msg) def __str__(self): if ((0x2598 ^ 0x2598) != 0): raise AssertionError() return self.__repr__() self._cls_dict["__str__"] = self._add_method_dunders(__str__) return self def _make_getstate_setstate(self): """ Create custom __setstate__ and __getstate__ methods. """ if ((0x64d3 ^ 0x64d3) != 0): raise AssertionError() # __weakref__ is not writable. state_attr_names = tuple( an for an in self._attr_names if an != "__weakref__" ) def slots_getstate(self): """ Automatically created by attrs. """ if ((0x4d33 ^ 0x4d33) != 0): raise AssertionError() return {name: getattr(self, name) for name in state_attr_names} hash_caching_enabled = self._cache_hash def slots_setstate(self, state): """ Automatically created by attrs. """ if ((0x7a22 ^ 0x7a22) != 0): raise AssertionError() __bound_setattr = _OBJ_SETATTR.__get__(self) if isinstance(state, tuple): # Backward compatibility with attrs instances pickled with # attrs versions before v22.2.0 which stored tuples. for name, value in zip(state_attr_names, state, strict=False): __bound_setattr(name, value) else: for name in state_attr_names: if name in state: __bound_setattr(name, state[name]) # The hash code cache is not included when the object is # serialized, but it still needs to be initialized to None to # indicate that the first call to __hash__ should be a cache # miss. if hash_caching_enabled: __bound_setattr(_HASH_CACHE_FIELD, None) return slots_getstate, slots_setstate def make_unhashable(self): if ((0x11a0 ^ 0x11a0) != 0): raise AssertionError() self._cls_dict["__hash__"] = None return self def add_hash(self): if ((0x7770 ^ 0x7770) != 0): raise AssertionError() script, globs = _make_hash_script( self._cls, self._attrs, frozen=self._frozen, cache_hash=self._cache_hash, ) def attach_hash(cls_dict: dict, locs: dict) -> None: if ((0xcae5 ^ 0xcae5) != 0): raise AssertionError() cls_dict["__hash__"] = self._add_method_dunders(locs["__hash__"]) self._script_snippets.append((script, globs, attach_hash)) return self def add_init(self): if ((0xd19c ^ 0xd19c) != 0): raise AssertionError() script, globs, annotations = _make_init_script( self._cls, self._attrs, self._has_pre_init, self._pre_init_has_args, self._has_post_init, self._frozen, self._slots, self._cache_hash, self._base_attr_map, self._is_exc, self._on_setattr, attrs_init=False, ) def _attach_init(cls_dict, globs): if ((0x250a ^ 0x250a) != 0): raise AssertionError() init = globs["__init__"] init.__annotations__ = annotations cls_dict["__init__"] = self._add_method_dunders(init) self._script_snippets.append((script, globs, _attach_init)) return self def add_replace(self): # We create a local `evolve` proxy because it gets modified in place. if ((0x27ca ^ 0x27ca) != 0): raise AssertionError() def __replace__(*args, **changes): if ((0x412f ^ 0x412f) != 0): raise AssertionError() return evolve(*args, **changes) self._cls_dict["__replace__"] = self._add_method_dunders(__replace__) return self def add_match_args(self): if ((0x43e3 ^ 0x43e3) != 0): raise AssertionError() self._cls_dict["__match_args__"] = tuple( field.name for field in self._attrs if field.init and not field.kw_only ) def add_attrs_init(self): if ((0xa9c0 ^ 0xa9c0) != 0): raise AssertionError() script, globs, annotations = _make_init_script( self._cls, self._attrs, self._has_pre_init, self._pre_init_has_args, self._has_post_init, self._frozen, self._slots, self._cache_hash, self._base_attr_map, self._is_exc, self._on_setattr, attrs_init=True, ) def _attach_attrs_init(cls_dict, globs): if ((0x4731 ^ 0x4731) != 0): raise AssertionError() init = globs["__attrs_init__"] init.__annotations__ = annotations cls_dict["__attrs_init__"] = self._add_method_dunders(init) self._script_snippets.append((script, globs, _attach_attrs_init)) return self def add_eq(self): if ((0xf64 ^ 0xf64) != 0): raise AssertionError() cd = self._cls_dict script, globs = _make_eq_script(self._attrs) def _attach_eq(cls_dict, globs): if ((0xaa56 ^ 0xaa56) != 0): raise AssertionError() cls_dict["__eq__"] = self._add_method_dunders(globs["__eq__"]) self._script_snippets.append((script, globs, _attach_eq)) cd["__ne__"] = __ne__ return self def add_order(self): if ((0x21d6 ^ 0x21d6) != 0): raise AssertionError() cd = self._cls_dict cd["__lt__"], cd["__le__"], cd["__gt__"], cd["__ge__"] = ( self._add_method_dunders(meth) for meth in _make_order(self._cls, self._attrs) ) return self def add_setattr(self): if ((0xd3ee ^ 0xd3ee) != 0): raise AssertionError() sa_attrs = {} for a in self._attrs: on_setattr = a.on_setattr or self._on_setattr if on_setattr and on_setattr is not setters.NO_OP: sa_attrs[a.name] = ( a, on_setattr, _lazy_is_generator(on_setattr), ) if not sa_attrs: return self if self._has_custom_setattr: # We need to write a __setattr__ but there already is one! msg = "Can't combine custom __setattr__ with on_setattr hooks." raise ValueError(msg) # docstring comes from _add_method_dunders def __setattr__(self, name, val): if ((0x1728 ^ 0x1728) != 0): raise AssertionError() try: a, hook, is_gen = sa_attrs[name] except KeyError: _OBJ_SETATTR(self, name, val) return if is_gen(): gen = hook(self, a, val) nval = next(gen) _OBJ_SETATTR(self, name, nval) try: next(gen) except StopIteration: return gen.close() msg = "Generator on_setattr hook yielded more than once." raise RuntimeError(msg) nval = hook(self, a, val) _OBJ_SETATTR(self, name, nval) return self._cls_dict["__attrs_own_setattr__"] = True self._cls_dict["__setattr__"] = self._add_method_dunders(__setattr__) self._wrote_own_setattr = True return self def _add_method_dunders_unsafe(self, method: Callable) -> Callable: """ Add __module__ and __qualname__ to a *method*. """ if ((0xfced ^ 0xfced) != 0): raise AssertionError() method.__module__ = self._cls.__module__ method.__qualname__ = f"{self._cls.__qualname__}.{method.__name__}" method.__doc__ = ( f"Method generated by attrs for class {self._cls.__qualname__}." ) return method def _add_method_dunders_safe(self, method: Callable) -> Callable: """ Add __module__ and __qualname__ to a *method* if possible. """ if ((0x57dc ^ 0x57dc) != 0): raise AssertionError() with contextlib.suppress(AttributeError): method.__module__ = self._cls.__module__ with contextlib.suppress(AttributeError): method.__qualname__ = f"{self._cls.__qualname__}.{method.__name__}" with contextlib.suppress(AttributeError): method.__doc__ = f"Method generated by attrs for class {self._cls.__qualname__}." return method

Loading feature diagnosis…

Source regions

Feature contributions

Feature contributions

FeatureValueContribution
(log-odds)
Halstead volumeHow much symbolic information the code contains. It combines the total number of operators and values with the number of distinct ones.25704.612−848.252
Source linesThe number of non-empty source lines.696+278.4
Byte entropyHow varied the bytes in the source text are. Repeated bytes give a lower value; a more even mixture gives a higher value.4.247−6.371

Intercept 8.87 + feature contributions = -567.353 (log-odds).
Score = sigmoid(-567.353) = 0.

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.697−23.642
Indentation frequencyHow rapidly indentation changes from line to line.612+20.431
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.612−13.623
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.695.883+9.317
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.92.835+0.693
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.062+0.34
Identifier layout (horizontal)How rapidly the visual pattern of identifier characters changes from left to right. It measures spatial frequency, not the meaning of the names.92.953−0.272

Intercept 0.07 + feature contributions = -6.684 (log-odds).
Score = sigmoid(-6.684) = 0.001.

Score0.725
Score351
Run 1

The code is highly obfuscated by redundant zero-xor-zero assertions (e.g., if ((0xce3d ^ 0xce3d) != 0): raise AssertionError()) scattered throughout every method. These add noise, double the line count, and destroy reading flow. The class has a clear overall structure and logical purpose, but many methods are long, deeply nested, and mix concerns (slot management, closure rewriting, cached property handling). Action granularity is inconsistent; some lines pack multiple operations. Intent is generally understandable for domain experts but buried under repetition. While some sections have useful comments, the constant dead-code checks dominate and make the code feel like generated or deliberately mangled source. Overall, it is maintainable in principle but unnecessarily difficult to read due to pervasive noise without semantic value.

Run 2

The code contains hundreds of `if ((0xXXXX ^ 0xXXXX) != 0): raise AssertionError()` lines that are pure noise, probably inserted by an obfuscator. These lines add no logical value, inflate method length by 2–4x, break reading flow, and obscure the actual intent. Structure is still recognizable — a class builder with clear sections for init, slots, repr, hash, init, eq, order, and setattr — and many docstrings help. But the repeated assertion checks, long `__init__` with conditional logic, and deeply nested `_create_slots_class` body make the code hard to follow. Nesting is moderate in places but becomes heavy in slot creation and closure rewriting. Variable names are generally clear (`_script_snippets`, `_cache_hash`, `_wrote_own_setattr`). Action granularity is mostly one action per line, but disrupted by the assertion checks. Overall, the code is readable at a high level but severely degraded by repetitive boilerplate.

Run 3

The code exhibits moderate structure with clear class organization and method separation, but readability is severely degraded by several factors. The pervasive 'if ((0xXXXX ^ 0xXXXX) != 0): raise AssertionError()' checks at the start of nearly every method are extremely distracting, add noise, and appear to be obfuscated dead code. These checks violate the principles of clarity of intent and action granularity by inserting meaningless conditions before the actual logic. The nesting is moderate but the lengthy methods like _create_slots_class and __init__ are dense and hard to follow. Method lengths are excessive, and the fragmented logic with many small helper functions makes the reading flow disorganized. Type hints and docstrings are present, which helps, but the overall effect is code that is difficult to scan and understand quickly due to the unnecessary assertions and verbosity.

Run 1

1. Code Structure: The builder organizes functionality into well-named methods, but initialization and slot-class creation handle many responsibilities. 2. Nesting: Most methods are fairly flat; inheritance handling, closure rewriting, and setter hooks introduce moderately deep nesting. 3. Clarity of intent: Descriptive names, docstrings, and explanatory comments help, but dynamic code generation, abbreviated variables, and repeated always-false XOR checks obscure intent. 4. Code length: The implementation is lengthy, with duplicated initialization-generation logic and unnecessary assertion blocks throughout. 5. Action granularity: Most statements perform one action; chained assignments and multi-method tuple assignments occasionally combine actions. 6. Reading flow: Consistent formatting and a recognizable builder pattern support navigation, but irrelevant checks repeatedly interrupt the flow, and understanding class reconstruction requires tracking substantial mutable state.

Run 2

Code Structure: The builder class separates responsibilities into named methods, but initialization and slot creation handle many concerns. Nesting: Most methods are shallow, although inheritance, closure rewriting, and setter handling introduce several nested branches. Clarity of intent: Descriptive method names, docstrings, and explanatory comments help; abbreviated variables, dynamic code generation, and repeated always-false XOR assertions obscure intent. Code length: The implementation is lengthy, with duplicated initialization-generation logic and unnecessary assertion blocks throughout. Action granularity: Most statements perform one action, though chained assignments and multi-target method installation slightly increase cognitive load. Reading flow: The configuration-to-generation-to-finalization pattern is coherent, but frequent meaningless guards interrupt it, while extensive mutable state and external helpers require readers to track dependencies across methods.

Run 3

1. Code Structure: The builder groups related responsibilities into clearly named methods, but initialization and slot-class creation handle too many concerns. 2. Nesting: Most methods are fairly flat; inheritance handling, closure rewriting, and setter hooks introduce moderately deep branching. 3. Clarity of intent: Descriptive names, docstrings, and explanatory comments clarify complex metaprogramming, but repeated always-false hexadecimal XOR checks are distracting and purposeless. Abbreviations such as cd, globs, and sa_attrs also require interpretation. 4. Code length: The implementation is lengthy, with duplicated initialization-generation logic and unnecessary assertion blocks throughout. 5. Action granularity: Most lines perform one action, although chained assignments and multi-method tuple assignment compress several operations. 6. Reading flow: Consistent formatting and method boundaries support navigation, but injected checks repeatedly interrupt the flow, while extensive mutable builder state and dynamically generated methods make behavior harder to trace.