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

feat: add module member name sorting to Stubgen #938

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tlambert03
Copy link

hey @wjakob, thanks for the awesome package.

this relates to #899 (but may not be exactly the same thing). My stubs are getting generated out of order, and therefore have Undefined name errors for some default values when running things like ruff on the output, due to inspect.getmembers() sorting members alphabetically.

I recognize this is a tricky topic, so this PR is designed to do nothing by default, but leave a little bit of agency to the end user. It adds a new member_sort parameter to StubGen

class StubGen:
    def __init__(
        self,
        ...
        member_sort: Optional[Callable[[tuple[str, Any]], Any]] | Literal['as-defined'] = None,
    ) -> None:

if a Callable or None (the default) is passed, it is used directly in sorted(getmembers(value), key=self. member_sort)

if the literal string 'as-defined' is passed, then the module members are sorted as they are defined in the module (using the module __dict__).

let me know what you think

@tlambert03 tlambert03 changed the title feat: add member-sort feat: add module member name sorting to Stubgen Feb 17, 2025
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

Successfully merging this pull request may close these issues.

1 participant