-
-
Notifications
You must be signed in to change notification settings - Fork 381
Add type stubs to the Python module #1926
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
base: main
Are you sure you want to change the base?
Conversation
Adding stubs alongside the Python files to provide type hints for external use. These are intended to cover the public interface, including all dynamically-generated attributes.
This ensures the .pyi files are copied to the build directory by Scons, and subsequently installed to the Python site-packages location. There's probably a cleaner way to do this.
Some notes: I covered a good chunk of the public API already, but I am aware of some holes here and there which I plan to fill in as time permits. I have deliberately omitted the various file conversion scripts (e.g. I still need to figure out a good way to add unit testing for the type hints. I expect that would look like liberal use of Several decisions were a little impulsive and I'm sure there are many improvements and changes needed. Some examples:
And I'm sure there's more, I just wanted to start getting feedback now rather than keep postponing this pull request forever! |
Discovered that `str` is a `Sequence[str]`, so the latter was replaced with `list[str] | tuple[str, ...]` as a workaround. Also need to include the default value to ensure empty calls are correctly type-narrowed, e.g. `Solution.species() -> list[Species]`.
Thanks for your efforts on this, @TimothyEDawson - this looks really promising! I am, however, somewhat concerned about maintaining parallel signatures in Based on what you suggest here, I ran a couple of quick tests: specifically, I was interested in whether type hints are preserved if they are added directly in the Based on successful tests, it appears that adding type hints directly to You can find my (very rudimentary) test here: https://github.com/ischoegl/cantera/tree/test-type-hints (just one commit) ... I simply copied over what you had in the |
@ischoegl yes, we're on the same page. That was the same concern I highlighted in the pull request text. There's no inherent issues with .pyx files as they're generally treated the same as .py files as far as type hints go, that was not part of the motivation for this. There's an order of resolution to type hints as defined here: https://typing.python.org/en/latest/spec/distributing.html#partial-stub-packages Because stub files take precedence over inline type hints, the path forward would essentially be to delete type hints from the stub files as they are added to the source code files. I am open to whichever approach the Cantera developers wish to take. The two options which seem best to me are:
I lean toward the first option because it gives users a working set of type hints while we work out details for the inline version. It also means I don't even need to touch any .pyx files. Maintainability is a concern, but mostly just for API changes, and any new functionality which is properly type-hinted inline won't need to be added to the .pyi files at all. At the same time, realistically it will be trivial to move the majority of these type hints inline right now. I expect there will be some edge cases which might take a while to figure out what to do, but if that's the route we want to go, I'd be happy to try it. |
Changes proposed in this pull request
There was some discussion about the approach here, but in short I consider this to be a stepping stone toward adding static typing to the Cython interface. Some advantage of starting with stub files include:
Solution
to aQuantity
orSolutionArray
, trivial to type-hint, and may (?) be the correct way to do so.There are some disadvantages, with the largest perhaps being the substantial increase in the amount of code to be maintained going forward. My hope is that the majority of the stub file content is gradually replaced with type hints within the source code, and I'm aware that I already have some redundant type hints in here.
If applicable, fill in the issue number this pull request is fixing
Potentially closes Cantera/enhancements#85.
Checklist
scons build
&scons test
) and unit tests address code coverage