https://github.com/python/cpython/commit/089cae5158f0fed73f44d7d21ff34bbfb24f1e36 commit: 089cae5158f0fed73f44d7d21ff34bbfb24f1e36 branch: 3.11 author: Miss Islington (bot) <[email protected]> committer: AlexWaygood <[email protected]> date: 2024-02-10T08:54:43Z summary:
[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 e19103a346f0277c44a43dfaebad9a5aa468bf1e) Co-authored-by: Nikita Sobolev <[email protected]> files: M Doc/reference/datamodel.rst M Lib/test/test_builtin.py diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 0419ee7e21a50a..c91e9cf43b11e2 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1966,8 +1966,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 @@ -1987,7 +1987,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 a8f5b76d1bd6bf..2b4fb0ebde4217 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -577,6 +577,14 @@ def __dir__(self): 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): _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: [email protected]
