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

Skip to content

Must object.__dir__ return a sequence of str, or is any iterable OK? #114552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ktbarrett opened this issue Jan 25, 2024 · 7 comments
Closed

Must object.__dir__ return a sequence of str, or is any iterable OK? #114552

ktbarrett opened this issue Jan 25, 2024 · 7 comments
Labels
docs Documentation in the Doc dir

Comments

@ktbarrett
Copy link

ktbarrett commented Jan 25, 2024

@srittau

This comment was marked as resolved.

@hauntsaninja
Copy link
Contributor

hauntsaninja commented Jan 25, 2024

I think the Python docs can sometimes be a little imprecise about what "sequence" means. E.g., the actual code that dir() uses just calls PySequence_List on the result of __dir__(). PySequence_List is another good example of imprecise use of "sequence"; it's perfectly happy to take in an iterable. So everything always works with Iterable at runtime.

@srittau
Copy link
Contributor

srittau commented Jan 25, 2024

Indeed. Another alternative is to fix the Python docs to match the implementation. Maybe this is something we should try first? In any case, I think it's important to be precise here, for cases, where someone uses __dir__ directly.

@AlexWaygood
Copy link
Member

AlexWaygood commented Jan 25, 2024

I can find one use of __dir__ in the stdlib where it returns a non-sequence:

def __dir__():
return globals().keys() | {'IsolatedAsyncioTestCase'}

It's a module-level use of __dir__ rather than a a method. (But then, ModuleType is a subclass of object, just like any other class. And the datamodel docs for __dir__ as a module-level function also state that it "should return a sequence".)

@srittau
Copy link
Contributor

srittau commented Jan 25, 2024

I suggest to move this issue to python/cpython and suggest to change the docs. Returning a set from __dir__ makes a lot of sense to me.

@AlexWaygood AlexWaygood transferred this issue from python/typeshed Jan 25, 2024
@AlexWaygood AlexWaygood added the docs Documentation in the Doc dir label Jan 25, 2024
@AlexWaygood AlexWaygood changed the title Type on object.__dir__ is incorrect. Must object.__dir__ return a sequence of str, or is any iterable OK? Jan 25, 2024
@AlexWaygood
Copy link
Member

AlexWaygood commented Jan 25, 2024

I've transferred this issue to CPython and made some light edits to a few comments by participants in the discussion, so that the issue thread will make sense to CPython triagers reading it for the first time.

@sobolevn
Copy link
Member

My 2c:

  • PySequence_List already says: Return a list object with the same contents as the sequence or iterable *o*, which I guess is fine
  • I've updated __dir__ docs to mention iterable instead of sequence, because it is how runtime behaves (correctly)

sobolevn added a commit to sobolevn/cpython that referenced this issue Jan 27, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 10, 2024
… iterable (pythonGH-114662)

(cherry picked from commit e19103a)

Co-authored-by: Nikita Sobolev <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 10, 2024
… iterable (pythonGH-114662)

(cherry picked from commit e19103a)

Co-authored-by: Nikita Sobolev <[email protected]>
AlexWaygood pushed a commit that referenced this issue Feb 10, 2024
…n iterable (GH-114662) (#115234)

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]>
AlexWaygood pushed a commit that referenced this issue Feb 10, 2024
…n 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]>
fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this issue Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

5 participants