Skip to content

[Python] Add __all__ variable in the package __init__.py file for Python APIs #21185

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

Merged
merged 3 commits into from
May 7, 2025

Conversation

AAraKKe
Copy link
Contributor

@AAraKKe AAraKKe commented Apr 29, 2025

Hi team!

This PR is to address #17152. The way the package __init__.py file is done works at runtime but type checkers break because with the package being marked with py.typed all those imports are considered private.

The pyright type checker (behind the pylance language server for python in VSCode) has a good explanation about this in their docs. This makes that packages using API clients build with openapi-generator either need to have very verbose imports to import from the module the classes are defined in (turning the __init__.py import not usable) or have to disable the checker rules which can be undesired in some cases.

In order to fix it I have added the generation of the __all__ variable in the package __init__.py file that specifies the packages being exported explicitely. Type checkers now understand this and would not complaint about these imports.

The PR is against master because this has no impact in runtime so any API generate with this included or not will behave the same with the difference that type checks now would understand imports like

from myApi import ApiClient

Thanks!

CC: @cbornet, @tomplus, @krjakbrjak, @fa0311, @multani

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@AAraKKe AAraKKe changed the title [Python] Redundantly alias imports in package __init__.py file [Python] Add __all__ variable in the package __init__.py file for Python APIs Apr 29, 2025
@wing328
Copy link
Member

wing328 commented Apr 30, 2025

@AAraKKe
Copy link
Contributor Author

AAraKKe commented Apr 30, 2025

please follow step 3 to fix https://github.com/OpenAPITools/openapi-generator/actions/runs/14741549462/job/41394125780?pr=21185

Sorry I missed to add those after the latest update to remove the blank line! Should be fixed now.

Thanks for reviewing!

@AAraKKe
Copy link
Contributor Author

AAraKKe commented May 3, 2025

@wing328 is there anything else needed to merge this? Let me know and I can take a look.

@wing328
Copy link
Member

wing328 commented May 7, 2025

python 3.8 test failure can be ignored as these tests have been removed in the latest master

@wing328
Copy link
Member

wing328 commented May 7, 2025

thanks for the PR

let's give it a try

@wing328 wing328 merged commit f281371 into OpenAPITools:master May 7, 2025
93 of 105 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants