-
Notifications
You must be signed in to change notification settings - Fork 72
Fix class sorting in case of inheritance #275
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
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
147658e
Test: Inheritance Sort Issue
ax3l d017897
Fix class sorting in case of inheritance
juelg 7f6afa5
feat: Add flexible class sorting strategies
juelg 6121d54
Merge branch 'master' into fix/class-sorting
ax3l 8be6932
Merge branch 'main' into fix/class-sorting
ax3l 4fb61d3
fix flake8
ax3l c57778a
Update silver files
ax3l 4dbe096
Run: `./tests/check-demo-errors-generation.sh`
ax3l 5828bf2
Merge remote-tracking branch 'mainline/main' into fix/class-sorting
ax3l 34ab1c7
Merge remote-tracking branch 'mainline/main' into fix/class-sorting
ax3l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,17 @@ | ||
| #pragma once | ||
| #include <string> | ||
|
|
||
| namespace demo{ | ||
| namespace demo | ||
| { | ||
| // note: class stubs must not be sorted | ||
| // https://github.com/sizmailov/pybind11-stubgen/issues/231 | ||
|
|
||
| struct Base { | ||
| struct Inner{}; | ||
| std::string name; | ||
| }; | ||
|
|
||
| struct Derived : Base { | ||
| int count; | ||
| }; | ||
| struct MyBase { | ||
| struct Inner{}; | ||
| std::string name; | ||
| }; | ||
|
|
||
| struct Derived : MyBase { | ||
| int count; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are such cycles actually possible with pybind11? In my understanding of the Python type system, it enforces DAG for inheritance, which makes such cases ill-formed.
It should either raise a NameError at runtime, or a TypeError (Cannot create a consistent method resolution order (MRO)). Does pybind11 do some magic here to make this possible?
If not, maybe we should make it a hard error.
@ax3l I recall you specifically mentioned this issue as the most severe one. Was it in regards just to the definition order sorting or cyclic inheritance included?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The server issue I mentioned is the test included so far in this PR: inheritance should not be alphabetically sorted, because it breaks definition order.
While testing this PR, I noticed that "cyclic" uses like these are now broken. I can simplify that example a bit more, but the essence here is not a cyclic inheritance but a usage in a method interleaving definitions...
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that is a different issue from the definition order toposort. We can handle this in a separate PR, that would be easier. Your example does not show an inheritance cycle, but a name availability issue. When the class is not yet defined its name is not available prior to python 3.14.
There are ways to tackle that pretty easily. The universal way for all versions of Python is to just enclose the not-yet-defined name into a string literal "". This acts as a forward declaration.
There are some things to consider though.
The naive solution is to track if a method is referencing "self", the name of the class this method belongs to.
However, ideally we should track the already defined names, and if any name in the annotation context appears before it is defined, we enclose it in a forward declaration literal. That would also handle cases of unresolved names in a way that the unresolved name is still printed, but does not break Python code from evaluation. That covers the incorrect "C++ types" sneaking in which might have :: in them (that's pybind11's behavior on generating such doctrings). That would also make sure tools like black are able to succesfully format such a file, and stubgen exits normally.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would also automatically free us from the implementing this toposort logic for anything else than class bases, I believe.
Even with this PR the following should result into an error:
That, I believe, would produce:
What we will get with stringification (which is totally okay with mypy and other type checkers):
Aside from that, there is one last issue that may exist with dependency cycles is cyclic imports between modules. But I think this is not an issue for .pyi stubs, judging from how mypy treats
if TYPE_CHECKING:blocks in normal .py.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@juelg can you please allow the maintainers to push into the branch? I plan to bring this close to merge this weekend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @skarndev! The PR should already give maintainers push access to the branch.