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

bpo-38787: Clarify docs for PyType_GetModule and warn against common mistake #20215

Merged
merged 4 commits into from
Aug 27, 2020

Conversation

encukou
Copy link
Member

@encukou encukou commented May 19, 2020

This should make PyType_GetModule, and PyType_FromModuleAndSpec's module argument clearer to people who didn't read the PEP.

Thanks @vstinner and @shihai1991 for asking clarifying questions. Hopefully this will help others.

https://bugs.python.org/issue38787

Automerge-Triggered-By: @encukou

may not return the intended result.
``Py_TYPE(self)`` may be a *subclass* of the intended class, and subclasses
are not necessarily defined in the same module as their superclass.
See :c:type:`PyCMethod` to get the defining class.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "defining class" is something new to me. PyCMethod mentions it without explining it. Would it be possible to define it somewhere? And then link to it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think the explanation should be in PyCMethod docs, or in METH_METHOD | METH_FASTCALL | METH_KEYWORDS docs?
For the other conventions, the docs for the flags have more detail. But I haven't found a way to link to METH_METHOD | METH_FASTCALL | METH_KEYWORDS.

encukou and others added 2 commits May 19, 2020 15:39
Co-authored-by: Victor Stinner <vstinner@python.org>
@vstinner
Copy link
Member

I suggest to add a short section about subclassing and defining class at the bottom of https://docs.python.org/dev/c-api/type.html and then add links to this section.

Something similar to https://docs.python.org/dev/library/os.html#file-descriptor-operations which defines what a file descriptor is.

@encukou
Copy link
Member Author

encukou commented May 19, 2020

What do you think the section should say? Does it need more detail than:

  • The defining class of a method is the class in which the method is defined.
  • The module passed to PyType_FromModuleAndSpec is not inherited by subclasses.

A whole section seems like overkill to me.

@vstinner
Copy link
Member

What do you think the section should say?

Explain PyType_GetModule(Py_TYPE(self)) issue. Explain how to get the defining class. Maybe add pseudo-code of a function which gets the defining class, in comparison with bad code which uses Py_TYPE(self).

@vstinner
Copy link
Member

Ah, maybe also point the right section of the PEP 573 for more information?

@shihai1991
Copy link
Member

Ah, maybe also point the right section of the PEP 573 for more information?

+1 IMHO, we don't need explain too much details in docs ; )

@encukou
Copy link
Member Author

encukou commented May 19, 2020

Ah, maybe also point the right section of the PEP 573 for more information?

No. The PEP is an immutable document about the design and discussion. It should be superseded by the documentation.

OK. I meant this as a quick fix, but it looks like I'll need more time to write this up.

@vstinner
Copy link
Member

According to the length of the following PEP sections, it seems like there are few things to say about defining classes :-)

@encukou
Copy link
Member Author

encukou commented Jun 2, 2020

I'm now writing a HOWTO on multi-phase init, heap types and avoiding static state, so that everything is explained properly in context. However, this full overview will take a long time to put together.

I agree that the docs in this PR are very terse and technical, but I believe they are an improvement. Consider not blocking the PR on the long explanation.

@encukou
Copy link
Member Author

encukou commented Aug 26, 2020

@vstinner: I've submitted a a longer document with a HOWTO and explanations as PEP 630. I don't think it belongs in the reference docs, though.

Would it be OK to add this PR's change to the documentation?

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This change enhances the doc.

@encukou encukou merged commit d9a966a into python:master Aug 27, 2020
@miss-islington
Copy link
Contributor

Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

@encukou encukou deleted the pep573-clarify-docs branch August 27, 2020 13:36
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 27, 2020
…mistake (pythonGH-20215)

(cherry picked from commit d9a966a)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Aug 27, 2020
@bedevere-bot
Copy link

GH-21984 is a backport of this pull request to the 3.9 branch.

@vstinner
Copy link
Member

Thanks @encukou for this PR and the PEP 630!

miss-islington added a commit that referenced this pull request Sep 4, 2020
…ommon mistake (GH-20215) (GH-21984)

(cherry picked from commit d9a966a)


Co-authored-by: Petr Viktorin <encukou@gmail.com>

Automerge-Triggered-By: @Mariatta
xzy3 pushed a commit to xzy3/cpython that referenced this pull request Oct 18, 2020
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 skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants