a :ref:`generic class <generic-classes>`.
:attr:`~class.__static_attributes__`
- A tuple containing names of attributes of this class which are accessed
+ A tuple containing names of attributes of this class which are assigned
through ``self.X`` from any function in its body.
:attr:`__firstlineno__`
TypeError: split() got an unexpected keyword argument 'max_split'. Did you mean 'maxsplit'?
* Classes have a new :attr:`~class.__static_attributes__` attribute, populated by the compiler,
- with a tuple of names of attributes of this class which are accessed
+ with a tuple of names of attributes of this class which are assigned
through ``self.X`` from any function in its body. (Contributed by Irit Katriel
in :gh:`115775`.)
self.assertEqual(end_col, 20)
-class TestExpectedAttributes(unittest.TestCase):
+class TestStaticAttributes(unittest.TestCase):
def test_basic(self):
class C:
def f(self):
self.a = self.b = 42
+ # read fields are not included
+ self.f()
+ self.arr[3]
self.assertIsInstance(C.__static_attributes__, tuple)
self.assertEqual(sorted(C.__static_attributes__), ['a', 'b'])
}
static int
-compiler_add_static_attribute_to_class(struct compiler *c, PyObject *attr)
+compiler_maybe_add_static_attribute_to_class(struct compiler *c, expr_ty e)
{
+ assert(e->kind == Attribute_kind);
+ expr_ty attr_value = e->v.Attribute.value;
+ if (attr_value->kind != Name_kind ||
+ e->v.Attribute.ctx != Store ||
+ !_PyUnicode_EqualToASCIIString(attr_value->v.Name.id, "self"))
+ {
+ return SUCCESS;
+ }
Py_ssize_t stack_size = PyList_GET_SIZE(c->c_stack);
for (Py_ssize_t i = stack_size - 1; i >= 0; i--) {
PyObject *capsule = PyList_GET_ITEM(c->c_stack, i);
assert(u);
if (u->u_scope_type == COMPILER_SCOPE_CLASS) {
assert(u->u_static_attributes);
- RETURN_IF_ERROR(PySet_Add(u->u_static_attributes, attr));
+ RETURN_IF_ERROR(PySet_Add(u->u_static_attributes, e->v.Attribute.attr));
break;
}
}
ADDOP(c, loc, NOP);
return SUCCESS;
}
- if (e->v.Attribute.value->kind == Name_kind &&
- _PyUnicode_EqualToASCIIString(e->v.Attribute.value->v.Name.id, "self"))
- {
- RETURN_IF_ERROR(compiler_add_static_attribute_to_class(c, e->v.Attribute.attr));
- }
+ RETURN_IF_ERROR(compiler_maybe_add_static_attribute_to_class(c, e));
VISIT(c, expr, e->v.Attribute.value);
loc = LOC(e);
loc = update_start_location_to_match_attr(c, loc, e);