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

Sorting is causing parent-child relationships to be defined out of order #231

Open
daltairwalter opened this issue Sep 16, 2024 · 7 comments

Comments

@daltairwalter
Copy link

When I run pybind11-stubgen, I see ouput:

class SimpleSetupArrayOutput(SimpleSetupEntity):  
    def getDataType(self) -> DATA_TYPE:  
        ...  
class SimpleSetupEntity:  
    def getName(self) -> str:  
        ... 

This is out of order because the SimpleSetupArrayOutput is a subclass of SimpleSetupEntity and this causes linting errors.  Is there a way to turn off sorting?  Or is it possible to sort and then reorder as necessary?
@sillydan1
Copy link

I have this problem as well. The generated classes look like:

class BusyError(Error):
    pass
class Error(Exception):
    pass

@ax3l
Copy link

ax3l commented Jan 7, 2025

I have the same issue with mixing classes that I define in the right order - but stubgen outputs them alphabetically, which breaks then exactly as the two examples above:
https://github.com/ECP-WarpX/impactx/blob/2c97164cc687cacc6ba058482a93c1e1f0bf7ca9/src/python/elements.cpp#L143-L277

@ax3l
Copy link

ax3l commented Jan 7, 2025

@sizmailov Can we disable sorting somehow? I am running tools like ruff on my generated files anyway :)

@ax3l
Copy link

ax3l commented Jan 7, 2025

As a quick hack, I tried to remove the sorted() for classes in printer.py but it looks like the class sorting stays the same right now. It looks like the classes are stored in a List, thus I do not yet understand where the sorting comes in.

@ax3l
Copy link

ax3l commented Jan 9, 2025

I modified the inheritance test in #238 as a demo but am not sure how to fix the issue yet :)

@ax3l
Copy link

ax3l commented Jan 30, 2025

Ok, if I append reverse=True to this line, then it changes the sorting:

for class_ in sorted(module.classes, key=lambda c: c.name):

If I just remove the sorted() altogether then it is still sorting the classes alphabetically, so there must be an earlier sort of some kind happening for module.classes.

@ax3l
Copy link

ax3l commented Jan 30, 2025

I think the issue might be that parse.py uses inspect.getmembers to get the members of a module and that one is sorted alphabetically.
After some Google & LLM queries, we could use the ast module instead to query and preserve order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants