X Tutup
The Wayback Machine - https://web.archive.org/web/20221209231202/https://github.com/python/cpython/issues/97699
Skip to content
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

object doesn't have an __enter__/__exit__ yet docs say it does #97699

Open
jamesbraza opened this issue Oct 1, 2022 · 8 comments
Open

object doesn't have an __enter__/__exit__ yet docs say it does #97699

jamesbraza opened this issue Oct 1, 2022 · 8 comments
Labels
docs Documentation in the Doc dir

Comments

@jamesbraza
Copy link

jamesbraza commented Oct 1, 2022

Documentation

Here's the current docs in the Python data model on object.__enter__:

object.__enter__(self)

And object.__exit__:

object.__exit__(self, exc_type, exc_value, traceback)

However, with Python 3.10.3, we see neither of these methods belong to object:

>>> object.__enter__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'object' has no attribute '__enter__'. Did you mean: '__ne__'?
>>> object.__exit__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'object' has no attribute '__exit__'. Did you mean: '__init__'?

I think the docs could be:

  • More clear that __enter__ and __exit__ are not methods on object
  • Mention where they come from (just being defined within your object to match the interface)

Thank you for your consideration!

@stevendaprano
Copy link
Member

stevendaprano commented Oct 1, 2022

This is a misreading of the documentation. object itself, the base class of all objects, does not and never has had __enter__ and __exit__ methods.

The entire page uses "object" to refer to some generic object, e.g. the section on Emulating numeric types uses "object" to refer to something that emulates a numeric value. In this section, "object" refers to any arbitrary object whose type defines the two __enter__ and __exit__ methods, which makes it a context manager.

In hindsight we should have called the base class Object, to avoid ambiguity between arbitrary objects or instances, and the base class object.

In this case I suggest we change the documentation to use "instance" instead of "object" for the entire page, not just the context manager section.

@TeamSpen210
Copy link

TeamSpen210 commented Oct 2, 2022

Would it be a good idea to also add positional-only markers, to indicate that the names are conventions and don’t necessarily need to match the docs?

@AlexWaygood
Copy link
Member

AlexWaygood commented Oct 2, 2022

In this case I suggest we change the documentation to use "instance" instead of "object" for the entire page, not just the context manager section.

That might require updating a lot of links across the rest of CPython's documentation.

@stevendaprano
Copy link
Member

stevendaprano commented Oct 2, 2022

@AlexWaygood
Copy link
Member

AlexWaygood commented Oct 2, 2022

Yes, I agree that it might make the data model documentation less confusing if we referred to dunders as instance.__await__, instance.__enter__, etc.

My point is that a lot of links to these methods from other parts of the documentation might be broken if the data model docs were reworked in this way. For example, there are currently six other docs files in the CPython repo that link to the datamodel docs on object.__class_getitem__. I think we might have to update all of those links if we started calling it instance.__class_getitem__ instead.

@pochmann
Copy link
Contributor

pochmann commented Oct 2, 2022

a lot of links to these methods from other parts of the documentation might be broken

A lot of links from other parts of the internet, too, for example from Stack Overflow. Is there no way to have anchors for both or to redirect the old links?

@AlexWaygood
Copy link
Member

AlexWaygood commented Oct 2, 2022

Is there no way to have anchors for both or to redirect the old links?

I'm not sure. @CAM-Gerlach might know more :)

But if we're talking about updating the whole page for the datamodel docs so that all dunders are referred to as instance.__dunder__, we'd presumably have to implement those redirects for every single dunder method.

@CAM-Gerlach
Copy link
Member

CAM-Gerlach commented Oct 5, 2022

A lot of links from other parts of the internet, too, for example from Stack Overflow. Is there no way to have anchors for both or to redirect the old links?

We can redirect external links by creating a ref target label that matches the old anchor pointing to the relevant object, which is only one line per method. That won't for internal links using :meth:/:func:, though; those can be updated here, but it will break other docs sites linking to them via intersphinx (though it will generate a warning with -n, and visibly display as unlinked in the rendered docs).

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

6 participants
X Tutup