]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-114552: Update `__dir__` method docs: it allows returning an iterable ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 10 Feb 2024 08:50:31 +0000 (09:50 +0100)
committerGitHub <noreply@github.com>
Sat, 10 Feb 2024 08:50:31 +0000 (08:50 +0000)
gh-114552: Update `__dir__` method docs: it allows returning an iterable (GH-114662)
(cherry picked from commit e19103a346f0277c44a43dfaebad9a5aa468bf1e)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Doc/reference/datamodel.rst
Lib/test/test_builtin.py

index fcb65b8919bfc22f0089f4919786db055ddb03e1..a9ce0f44949b0537f4f28147c1f4b40b3153523f 100644 (file)
@@ -1983,8 +1983,8 @@ access (use of, assignment to, or deletion of ``x.name``) for class instances.
 
 .. method:: object.__dir__(self)
 
-   Called when :func:`dir` is called on the object. A sequence must be
-   returned. :func:`dir` converts the returned sequence to a list and sorts it.
+   Called when :func:`dir` is called on the object. An iterable must be
+   returned. :func:`dir` converts the returned iterable to a list and sorts it.
 
 
 Customizing module attribute access
@@ -2004,7 +2004,7 @@ not found on a module object through the normal lookup, i.e.
 the module ``__dict__`` before raising an :exc:`AttributeError`. If found,
 it is called with the attribute name and the result is returned.
 
-The ``__dir__`` function should accept no arguments, and return a sequence of
+The ``__dir__`` function should accept no arguments, and return an iterable of
 strings that represents the names accessible on module. If present, this
 function overrides the standard :func:`dir` search on a module.
 
index 4d03c46382e393e82b125cdd80fb72c9cab8d0e8..211dd89ea4851ced660e75dded8e5465b00e7123 100644 (file)
@@ -586,6 +586,14 @@ class BuiltinTest(unittest.TestCase):
         self.assertIsInstance(res, list)
         self.assertTrue(res == ["a", "b", "c"])
 
+        # dir(obj__dir__iterable)
+        class Foo(object):
+            def __dir__(self):
+                return {"b", "c", "a"}
+        res = dir(Foo())
+        self.assertIsInstance(res, list)
+        self.assertEqual(sorted(res), ["a", "b", "c"])
+
         # dir(obj__dir__not_sequence)
         class Foo(object):
             def __dir__(self):