diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2024-02-10 08:34:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-10 08:34:23 (GMT) |
commit | e19103a346f0277c44a43dfaebad9a5aa468bf1e (patch) | |
tree | be8b2171da023bf9f748a64be22e45a008fe8ec0 | |
parent | b2d9d134dcb5633deebebf2b0118cd4f7ca598a2 (diff) | |
download | cpython-e19103a346f0277c44a43dfaebad9a5aa468bf1e.zip cpython-e19103a346f0277c44a43dfaebad9a5aa468bf1e.tar.gz cpython-e19103a346f0277c44a43dfaebad9a5aa468bf1e.tar.bz2 |
gh-114552: Update `__dir__` method docs: it allows returning an iterable (#114662)
-rw-r--r-- | Doc/reference/datamodel.rst | 6 | ||||
-rw-r--r-- | Lib/test/test_builtin.py | 8 |
2 files changed, 11 insertions, 3 deletions
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 0a1c1d5..885ee82 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -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. diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index fcddd14..7a3ab22 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -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): |