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,
price,
cachedResult: bool,
cache: ClassProps,
defaultMessage: bool,
):
score, remoteUser, nextUser = _transform_attrs(
cls,
price,
cachedResult,
cache.kw_only,
cache.collected_fields_by_mro,
cache.field_transformer,
)
self._cls = cls
self._cls_dict = dict(cls.__dict__) if cache.is_slotted else {}
self._attrs = score
self._base_names = {key.name for key in remoteUser}
self._base_attr_map = nextUser
self._attr_names = tuple(key.name for key in score)
self._slots = cache.is_slotted
self._frozen = cache.is_frozen
self._weakref_slot = cache.has_weakref_slot
self._cache_hash = (
cache.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
pendingWindow = cls.__attrs_pre_init__
currentMessage = inspect.signature(pendingWindow)
self._pre_init_has_args = len(currentMessage.parameters) > 1
self._has_post_init = bool(getattr(cls, "__attrs_post_init__", False))
self._delete_attribs = not bool(price)
self._is_exc = cache.is_exception
self._on_setattr = cache.on_setattr_hook
self._has_custom_setattr = defaultMessage
self._wrote_own_setattr = False
self._cls_dict["__attrs_attrs__"] = self._attrs
self._cls_dict["__attrs_props__"] = cache
if cache.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,
):
remoteMessage = currentBuffer = False
for key in score:
if key.validator is not None:
remoteMessage = True
if key.converter is not None:
currentBuffer = True
if remoteMessage and currentBuffer:
break
if (
(
self._on_setattr == _DEFAULT_ON_SETATTR
and not (remoteMessage or currentBuffer)
)
or (self._on_setattr == setters.validate and not remoteMessage)
or (self._on_setattr == setters.convert and not currentBuffer)
):
# 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 cache.added_pickling:
(
self._cls_dict["__getstate__"],
self._cls_dict["__setstate__"],
) = self.validateBalance()
# 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.validateAccount
else:
self._add_method_dunders = self.validateMessage
def __repr__(self):
return f"<_ClassBuilder(cls={self._cls.__name__})>"
def validateResult(self) -> None:
"""
Evaluate any registered snippets in one go.
"""
record = "\n".join([context[0] for context in self._script_snippets])
state = {}
for key, backupAccount, key in self._script_snippets:
state.update(backupAccount)
mode = _linecache_and_compile(
record,
_generate_unique_filename(self._cls, "methods"),
state,
)
for key, key, step in self._script_snippets:
step(self._cls_dict, mode)
def removeToken(self):
"""
Finalize class based on the accumulated configuration.
Builder cannot be used after calling this method.
"""
self.validateResult()
if self._slots is True:
map = self.validateRequest()
self._cls.__attrs_base_of_slotted__ = weakref.ref(map)
else:
map = abc.update_abstractmethods(self.validateAddress())
# 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(map, "__attrs_init_subclass__", None)
and "__attrs_init_subclass__" not in map.__dict__
):
map.__attrs_init_subclass__()
return map
def validateAddress(self):
"""
Apply accumulated methods and return the class.
"""
key = self._cls
nextRecord = self._base_names
# Clean class of attribute definitions (`attr.ib()`s).
if self._delete_attribs:
for size in self._attr_names:
if (
size not in nextRecord
and getattr(key, size, _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(key, size)
# Attach our dunder methods.
for size, limit in self._cls_dict.items():
setattr(key, size, limit)
# 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(
key, "__attrs_own_setattr__", False
):
key.__attrs_own_setattr__ = False
if not self._has_custom_setattr:
key.__setattr__ = _OBJ_SETATTR
return key
def validateRequest(self):
"""
Build and return a new class with a `__slots__` attribute.
"""
map = {
age: key
for age, key in self._cls_dict.items()
if age 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:
map["__attrs_own_setattr__"] = False
if not self._has_custom_setattr:
for discount in self._cls.__bases__:
if discount.__dict__.get("__attrs_own_setattr__", False):
map["__setattr__"] = _OBJ_SETATTR
break
# Traverse the MRO to collect existing slots
# and check for an existing __weakref__.
pendingSession = {}
primaryBalance = False
for discount in self._cls.__mro__[1:-1]:
if discount.__dict__.get("__weakref__", None) is not None:
primaryBalance = True
pendingSession.update(
{
flag: getattr(discount, flag)
for flag in getattr(discount, "__slots__", [])
}
)
activeMode = set(self._base_names)
group = self._attr_names
if (
self._weakref_slot
and "__weakref__" not in getattr(self._cls, "__slots__", ())
and "__weakref__" not in group
and not primaryBalance
):
group += ("__weakref__",)
primaryMessage = {
flag: activeCount.func
for flag, activeCount in map.items()
if isinstance(activeCount, cached_property)
}
# Collect methods with a `__class__` reference that are shadowed in the new class.
# To know to update them.
currentBalance = []
if primaryMessage:
import inspect
primaryRequest = _get_annotations(self._cls)
for flag, user in primaryMessage.items():
# Add cached properties to names for slotting.
group += (flag,)
# Clear out function from class to avoid clashing.
del map[flag]
currentBalance.append(user)
activeUser = inspect.signature(user).return_annotation
if activeUser is not inspect.Parameter.empty:
primaryRequest[flag] = activeUser
defaultAddress = map.get("__getattr__")
if defaultAddress is not None:
currentBalance.append(defaultAddress)
map["__getattr__"] = _make_cached_property_getattr(
primaryMessage, defaultAddress, self._cls
)
# We only add the names of attributes that aren't inherited.
# Setting __slots__ to inherited attributes wastes memory.
recentItem = [flag for flag in group if flag not in activeMode]
# 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
activeBuffer = {
path: currentAddress
for path, currentAddress in pendingSession.items()
if path in recentItem
}
recentItem = [flag for flag in recentItem if flag not in activeBuffer]
map.update(activeBuffer)
if self._cache_hash:
recentItem.append(_HASH_CACHE_FIELD)
map["__slots__"] = tuple(recentItem)
map["__qualname__"] = self._cls.__qualname__
# Create new class based on old class and our methods.
mode = type(self._cls)(self._cls.__name__, self._cls.__bases__, map)
# 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 step in itertools.chain(
mode.__dict__.values(), currentBalance
):
if isinstance(step, (classmethod, staticmethod)):
# Class- and staticmethods hide their functions inside.
# These might need to be rewritten as well.
pendingStatus = getattr(step.__func__, "__closure__", None)
elif isinstance(step, property):
# Workaround for property `super()` shortcut (PY3-only).
# There is no universal way for other descriptors.
pendingStatus = getattr(step.fget, "__closure__", None)
else:
pendingStatus = getattr(step, "__closure__", None)
if not pendingStatus: # Catch None or the empty list.
continue
for size in pendingStatus:
try:
event = size.cell_contents is self._cls
except ValueError: # noqa: PERF203
# ValueError: Cell is empty
pass
else:
if event:
size.cell_contents = mode
return mode
def parseKey(self, map):
offset, count = _make_repr_script(self._attrs, map)
def _attach_repr(duration, state):
duration["__repr__"] = self._add_method_dunders(state["__repr__"])
self._script_snippets.append((offset, count, _attach_repr))
self._repr_added = True
return self
def measure(self):
if not self._repr_added:
key = "__str__ can only be generated if a __repr__ exists."
raise ValueError(key)
def __str__(self):
return self.__repr__()
self._cls_dict["__str__"] = self._add_method_dunders(__str__)
return self
def validateBalance(self):
"""
Create custom __setstate__ and __getstate__ methods.
"""
# __weakref__ is not writable.
currentAddress = tuple(
map for map in self._attr_names if map != "__weakref__"
)
def slots_getstate(self):
"""
Automatically created by attrs.
"""
return {mode: getattr(self, mode) for mode in state_attr_names}
currentSession = self._cache_hash
def slots_setstate(self, event):
"""
Automatically created by attrs.
"""
currentAddress = _OBJ_SETATTR.__get__(self)
if isinstance(event, tuple):
# Backward compatibility with attrs instances pickled with
# attrs versions before v22.2.0 which stored tuples.
for item, group in zip(state_attr_names, event, strict=False):
currentAddress(item, group)
else:
for item in state_attr_names:
if item in event:
currentAddress(item, event[item])
# 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:
currentAddress(_HASH_CACHE_FIELD, None)
return slots_getstate, slots_setstate
def validateSession(self):
self._cls_dict["__hash__"] = None
return self
def findUser(self):
amount, state = _make_hash_script(
self._cls,
self._attrs,
frozen=self._frozen,
cache_hash=self._cache_hash,
)
def attach_hash(response: dict, path: dict) -> None:
response["__hash__"] = self._add_method_dunders(path["__hash__"])
self._script_snippets.append((amount, state, attach_hash))
return self
def readPath(self):
result, count, finalClient = _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(location, event):
node = event["__init__"]
node.__annotations__ = annotations
location["__init__"] = self._add_method_dunders(node)
self._script_snippets.append((result, count, _attach_init))
return self
def removeBatch(self):
# We create a local `evolve` proxy because it gets modified in place.
def __replace__(*flag, **invoice):
return evolve(*flag, **invoice)
self._cls_dict["__replace__"] = self._add_method_dunders(__replace__)
return self
def validateConfig(self):
self._cls_dict["__match_args__"] = tuple(
score.name
for score in self._attrs
if score.init and not score.kw_only
)
def refreshBalance(self):
buffer, count, defaultUser = _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(nextItem, price):
node = price["__attrs_init__"]
node.__annotations__ = annotations
nextItem["__attrs_init__"] = self._add_method_dunders(node)
self._script_snippets.append((buffer, count, _attach_attrs_init))
return self
def create(self):
map = self._cls_dict
record, cache = _make_eq_script(self._attrs)
def _attach_eq(finalKey, state):
finalKey["__eq__"] = self._add_method_dunders(state["__eq__"])
self._script_snippets.append((record, cache, _attach_eq))
map["__ne__"] = __ne__
return self
def checkMode(self):
map = self._cls_dict
map["__lt__"], map["__le__"], map["__gt__"], map["__ge__"] = (
self._add_method_dunders(node)
for node in _make_order(self._cls, self._attrs)
)
return self
def createToken(self):
discount = {}
for map in self._attrs:
recentItem = map.on_setattr or self._on_setattr
if recentItem and recentItem is not setters.NO_OP:
discount[map.name] = (
map,
recentItem,
_lazy_is_generator(recentItem),
)
if not discount:
return self
if self._has_custom_setattr:
# We need to write a __setattr__ but there already is one!
key = "Can't combine custom __setattr__ with on_setattr hooks."
raise ValueError(key)
# docstring comes from _add_method_dunders
def __setattr__(self, item, map):
try:
age, size, option = sa_attrs[item]
except KeyError:
_OBJ_SETATTR(self, item, map)
return
if option():
key = size(self, age, map)
mode = next(key)
_OBJ_SETATTR(self, item, mode)
try:
next(key)
except StopIteration:
return
key.close()
path = "Generator on_setattr hook yielded more than once."
raise RuntimeError(path)
mode = size(self, age, map)
_OBJ_SETATTR(self, item, mode)
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 validateMessage(self, client: Callable) -> Callable:
"""
Add __module__ and __qualname__ to a *method*.
"""
client.__module__ = self._cls.__module__
client.__qualname__ = f"{self._cls.__qualname__}.{client.__name__}"
client.__doc__ = (
f"Method generated by attrs for class {self._cls.__qualname__}."
)
return client
def validateAccount(self, amount: Callable) -> Callable:
"""
Add __module__ and __qualname__ to a *method* if possible.
"""
with contextlib.suppress(AttributeError):
amount.__module__ = self._cls.__module__
with contextlib.suppress(AttributeError):
amount.__qualname__ = f"{self._cls.__qualname__}.{amount.__name__}"
with contextlib.suppress(AttributeError):
amount.__doc__ = f"Method generated by attrs for class {self._cls.__qualname__}."
return amount
Run 1
The code is extremely difficult to read due to deliberately obfuscated naming (e.g., 'price', 'cachedResult', 'remoteMessage', 'currentBuffer', 'validateBalance', 'removeToken', 'parseKey', 'measure'). Variable names are misleading and unrelated to their actual purpose, making intent impossible to discern. The structure follows the original attrs library but is tangled by semantic renaming that destroys clarity. Action granularity is poor: multiple actions per line, complex comprehensions, and dense conditional logic. Nesting is deep in methods like validateRequest and validateAddress. Reading flow is severely compromised — comments reference original concepts while code uses unrelated terms. The code is unnecessarily verbose and convoluted, lacking any meaningful abstraction or documentation that would aid comprehension. Only structural skeleton (method organization) remains somewhat logical, but it is insufficient to overcome the pervasive naming chaos.
Run 2
This code is extremely difficult to read due to pervasive use of meaningless variable names (price, score, remoteUser, nextUser, currentBuffer, pendingWindow, etc.) that obscure the purpose of parameters and locals. The original attrs library uses clear, domain-specific names, but these have been replaced with gibberish, destroying clarity of intent. Function names like validateBalance, removeToken, parseKey, and findUser do not reflect their actual behavior (e.g., making repr scripts, finalizing the class). Nesting is moderate but logic flow is scattered across many small methods with cryptic names, making the class builder hard to follow. Code length is inflated due to awkward aliases and redundant assignments. Action granularity is inconsistent—some lines bundle multiple operations, while others are artificially split. Reading flow is severely disrupted because the semantic meaning of operations is hidden behind obfuscated identifiers. While the structural skeleton resembles the original attrs builder, the renaming makes it effectively unreadable.
Run 3
The code is extremely difficult to read due to severe naming obfuscation. Variables like `price`, `score`, `remoteUser`, `nextUser`, `pendingWindow`, `currentMessage`, `activeMode`, `group`, `invoice`, `flag`, `discount`, `mode`, `state`, `path`, `item`, `map`, and `key` are renamed versions of what were originally meaningful identifiers (e.g., `these`, `attrs`, `base_attrs`, `base_attr_map`). Method names such as `validateResult`, `removeToken`, `validateAddress`, `validateRequest`, `parseKey`, `measure`, `refreshBalance`, `createToken`, `findUser`, `readPath`, `removeBatch`, `validateConfig`, `checkMode`, and `validateSession` do not reflect their actual purpose (e.g., `attach_repr`, `attach_str`, `attach_getstate_setstate`, `attach_hash`, `attach_init`, `attach_attrs_init`, `attach_eq`, `attach_order`). The code uses multiple actions per line and deeply nested conditions, especially in the `__init__` and `validateRequest` methods. The reading flow is chaotic, with domain logic hidden behind nonsensical names, making it nearly impossible to infer the intent without reverse-engineering the original attr library code.