Skip to content

Conversation

@hyongtao-code
Copy link
Contributor

@hyongtao-code hyongtao-code commented Dec 18, 2025

_contextvars.Context.items(), .keys(), and .values() return iterator objects, but their docstrings incorrectly claim they return lists.

Taking items() as an example, its call chain is _contextvars_Context_items_impl()_PyHamt_NewIterItems().
The comment on _contextvars_Context_items_impl states that it returns a list, but the comment on _PyHamt_NewIterItems indicates that it returns an iterator.

Update the Argument Clinic docstrings in Python/context.c and regenerate Python/clinic/context.c.h. No behavior change.

Script to reproduce

import contextvars

ctx = contextvars.copy_context()

print("items() type:", type(ctx.items()))
print("keys()  type:", type(ctx.keys()))
print("values() type:", type(ctx.values()))

print("items() is list?", isinstance(ctx.items(), list))
print("keys()  is list?", isinstance(ctx.keys(), list))
print("values() is list?", isinstance(ctx.values(), list))

print("\nDocstrings say 'list'?")
print("items doc contains 'list':", "list" in (ctx.items.__doc__ or ""))
print("keys  doc contains 'list':", "list" in (ctx.keys.__doc__ or ""))
print("values doc contains 'list':", "list" in (ctx.values.__doc__ or ""))

Result without the patch

d:\MyCode\cpython\PCbuild\amd64>python_d.exe py_context.py
items() type: <class 'items'>
keys()  type: <class 'keys'>
values() type: <class 'values'>
items() is list? False
keys()  is list? False
values() is list? False

Docstrings say 'list'?
items doc contains 'list': True
keys  doc contains 'list': True
values doc contains 'list': True

Result with the patch

d:\MyCode\cpython\PCbuild\amd64>python_d.exe py_context.py
items() type: <class 'items'>
keys()  type: <class 'keys'>
values() type: <class 'values'>
items() is list? False
keys()  is list? False
values() is list? False

Docstrings say 'list'?
items doc contains 'list': False  <-- !!!! here
keys  doc contains 'list': False
values doc contains 'list': False

Signed-off-by: Yongtao Huang yongtaoh2022@gmail.com

_contextvars.Context.items(), .keys(), and .values() return iterator objects,
but their docstrings incorrectly claim they return lists.

Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com>
Update the Argument Clinic docstrings in Python/context.c and regenerate
Python/clinic/context.c.h. No behavior change.

Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com>
Copy link
Member

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs also use "a list," it should be fixed too.

@hyongtao-code
Copy link
Contributor Author

Thanks for the reminder — the corresponding docs have been updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants