]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-114552: Update `__dir__` method docs: it allows returning an iterable (#114662)
authorNikita Sobolev <mail@sobolevn.me>
Sat, 10 Feb 2024 08:34:23 +0000 (11:34 +0300)
committerGitHub <noreply@github.com>
Sat, 10 Feb 2024 08:34:23 +0000 (08:34 +0000)
Doc/reference/datamodel.rst
Lib/test/test_builtin.py

index 0a1c1d58558e94f28cf633a878fd660fe0bc9f5c..885ee825c1229654a48260695dbb153a206e1b2d 100644 (file)
@@ -1988,8 +1988,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
@@ -2009,7 +2009,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 fcddd147bac63ef72659f1e50600b19e59332800..7a3ab2274a58f2057de388d49282743b950a7c7c 100644 (file)
@@ -611,6 +611,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):