Skip to content

Add __set_name__ method to property - #16804

Merged
JelleZijlstra merged 2 commits into
python:mainfrom
icb08:patch-1
Nov 14, 2026
Merged

JelleZijlstra merged 2 commits into
python:mainfrom
icb08:patch-1

Conversation

@icb08

@icb08 icb08 commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

set_name is a built-in method of the property built-in, present from 3.10+

__set_name__ is a built-in method of the property built-in, present from 3.10+
@github-actions

This comment has been minimized.

@icb08

icb08 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Original stub test allow lists had:

builtins.property.__set_name__  # Doesn't actually exist

That comments a bit misleading? CPython has implemented a public property.__set_name__ built-in method since Python v3.10.0?

Proof in Python:

assert hasattr(property, "__set_name__")
assert property.__set_name__(property(), object, "x") is None

from sys import version_info
if version_info >= "3.13":
   x = property()
   x.__set_name__(object, "y")
   assert x.__name__ == "y"

Proof in Python Source (v3.10.0 tag commit):

static PyObject *
property_set_name(PyObject *self, PyObject *args) {
    if (PyTuple_GET_SIZE(args) != 2) {
        PyErr_Format(
                PyExc_TypeError,
                "__set_name__() takes 2 positional arguments but %d were given",
                PyTuple_GET_SIZE(args));
        return NULL;
    }

    propertyobject *prop = (propertyobject *)self;
    PyObject *name = PyTuple_GET_ITEM(args, 1);

    Py_XINCREF(name);
    Py_XSETREF(prop->prop_name, name);

    Py_RETURN_NONE;
}

static PyMethodDef property_methods[] = {
    {"getter", property_getter, METH_O, getter_doc},
    {"setter", property_setter, METH_O, setter_doc},
    {"deleter", property_deleter, METH_O, deleter_doc},
    {"__set_name__", property_set_name, METH_VARARGS, set_name_doc},
    {0}
};

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

pytest-autoprofile (https://gitlab.com/TTsangSC/pytest-autoprofile)
+ src/pytest_autoprofile/importers.py:343: error: Unused "type: ignore" comment  [unused-ignore]

@JelleZijlstra

Copy link
Copy Markdown
Member

Yes, I'm not sure what that comment was talking about.

@JelleZijlstra
JelleZijlstra merged commit dbca2d7 into python:main Nov 14, 2026
88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants