8000 [3.12] gh-114552: Update `__dir__` method docs: it allows returning a… · python/cpython@7cc2058 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7cc2058

Browse files
[3.12] gh-114552: Update __dir__ method docs: it allows returning an iterable (GH-114662) (#115234)
gh-114552: Update `__dir__` method docs: it allows returning an iterable (GH-114662) (cherry picked from commit e19103a) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
1 parent 321ec5e commit 7cc2058

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

Doc/reference/datamodel.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,8 +1983,8 @@ access (use of, assignment to, or deletion of ``x.name``) for class instances.
19831983

19841984
.. method:: object.__dir__(self)
19851985

1986-
Called when :func:`dir` is called on the object. A sequence must be
1987-
returned. :func:`dir` converts the returned sequence to a list and sorts it.
1986+
Called when :func:`dir` is called on the object. An iterable must be
1987+
returned. :func:`dir` converts the returned iterable to a list and sorts it.
19881988

19891989

19901990
Customizing module attribute access
@@ -2004,7 +2004,7 @@ not found on a module object through the normal lookup, i.e.
20042004
the module ``__dict__`` before raising an :exc:`AttributeError`. If found,
20052005
it is called with the attribute name and the result is returned.
20062006

2007-
The ``__dir__`` function should accept no arguments, and return a sequence of
2007+
The ``__dir__`` function should accept no arguments, and return an iterable of
20082008
strings that represents the names accessible on module. If present, this
20092009
function overrides the standard :func:`dir` search on a module.
20102010

Lib/test/test_builtin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,14 @@ def __dir__(self):
586586
self.assertIsInstance(res, list)
587587
self.assertTrue(res == ["a", "b", "c"])
588588

589+
# dir(obj__dir__iterable)
590+
class Foo(object):
591+
def __dir__(self):
592+
return {"b", "c", "a"}
593+
res = dir(Foo())
594+
self.assertIsInstance(res, list)
595+
self.assertEqual(sorted(res), ["a", "b", "c"])
596+
589597
# dir(obj__dir__not_sequence)
590598
class Foo(object):
591599
def __dir__(self):

0 commit comments

Comments
 (0)
0