Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 089cae5

Browse files
[3.11] gh-114552: Update __dir__ method docs: it allows returning an iterable (GH-114662) (#115235)
gh-114552: Update `__dir__` method docs: it allows returning an iterable (GH-114662) (cherry picked from commit e19103a) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 115f72b commit 089cae5

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
@@ -1966,8 +1966,8 @@ access (use of, assignment to, or deletion of ``x.name``) for class instances.
19661966

19671967
.. method:: object.__dir__(self)
19681968

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

19721972

19731973
Customizing module attribute access
@@ -1987,7 +1987,7 @@ not found on a module object through the normal lookup, i.e.
19871987
the module ``__dict__`` before raising an :exc:`AttributeError`. If found,
19881988
it is called with the attribute name and the result is returned.
19891989

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

Lib/test/test_builtin.py

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

580+
# dir(obj__dir__iterable)
581+
class Foo(object):
582+
def __dir__(self):
583+
return {"b", "c", "a"}
584+
res = dir(Foo())
585+
self.assertIsInstance(res, list)
586+
self.assertEqual(sorted(res), ["a", "b", "c"])
587+
580588
# dir(obj__dir__not_sequence)
581589
class Foo(object):
582590
def __dir__(self):

0 commit comments

Comments
 (0)