Skip to content

Make imports of external Python modules private#1948

Merged
speth merged 4 commits intoCantera:mainfrom
speth:private-imports
Aug 22, 2025
Merged

Make imports of external Python modules private#1948
speth merged 4 commits intoCantera:mainfrom
speth:private-imports

Conversation

@speth
Copy link
Copy Markdown
Member

@speth speth commented Aug 20, 2025

Changes proposed in this pull request

  • Import external Python modules and functions using "private" names (for example, _sys) so they don't get included as members of the cantera module.
    • The one exception to this is np, which is used so extensively that I didn't want to have to change it. This one is removed by a del in __init__.py.
  • Test that the resulting Python module does not (directly) contain any such modules or functions

This is an alternative approach to the same goal as #1947. There are a couple of things I discovered while preparing this.

  • Because Pylance / VS Code builds its list of members through static analysis rather than by actually importing the module, some clever tricks to dynamically edit the list of namespace/module members don't work. Here, I still end up with the VS Code autocomplete showing me that warnings, Path, np, and os are members of the cantera namespace, despite being deleted in __init__.py
  • The list of members obtained using the dir function, Jupyter's autocomplete (after import) and the autocomplete of from cantera import ... in IPython are correctly limited.

Checklist

  • The pull request includes a clear description of this code change
  • Commit messages have short titles and reference relevant issues
  • Build passes (scons build & scons test) and unit tests address code coverage
  • Style & formatting of contributed code follows contributing guidelines
  • The pull request is ready for review

@speth speth added the Python label Aug 20, 2025
Copy link
Copy Markdown
Member

@bryanwweber bryanwweber left a comment

Choose a reason for hiding this comment

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

Thanks @speth I think this makes a lot of sense. It seems that pyright also respects libraries that have py.typed, so if our *.pyi files do not export numpy publicly then it may resolve the problem. See: microsoft/pylance-release#4518

cc @TimothyEDawson

# This file is part of Cantera. See License.txt in the top-level directory or
# at https://cantera.org/license.txt for license and copyright information.
from __future__ import annotations
from __future__ import annotations as _annotations
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we're only supporting 3.10+, do we need this anymore?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I had been wondering the same thing, but I believe it also enables some features which are not in 3.10. Though the only one I could definitively turn up was forward references, which I do utilize in several places.

@ischoegl ischoegl mentioned this pull request Aug 20, 2025
5 tasks
@speth speth marked this pull request as ready for review August 22, 2025 02:43
Copy link
Copy Markdown
Member

@ischoegl ischoegl left a comment

Choose a reason for hiding this comment

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

Thanks, @speth … LGTM!

@speth speth merged commit 376bf74 into Cantera:main Aug 22, 2025
126 of 131 checks passed
@speth speth deleted the private-imports branch August 22, 2025 14:13
@speth speth mentioned this pull request Sep 1, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants