-
-
Notifications
You must be signed in to change notification settings - Fork 388
Limit Python exports #1947
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
Limit Python exports #1947
Conversation
@TimothyEDawson and @bryanwweber ... this is an implementation that should be a compromise between |
@ischoegl I actually had started working on my own implementation a while back, sorry I hadn't had time to make a pull request. You can take a look here, maybe there are some ideas you'd like: main...TimothyEDawson:cantera:fix-python-namespace-pollution Note that most of the "unnecessary" changes such as whitespace changes and changing single quotes to double quotes were due to my use of Ruff autoformatting. I'd probably revert those if I were to continue working on it. I hadn't removed the star imports at this point as it quickly gets out of hand, as seen in my type stub pull request! If I recall correctly, I think those are more important internally, where you want explicit imports of any classes or functions used within the file, whereas |
@TimothyEDawson ... ok - I didn't know. From my perspective, either of the two PRs is fine (took me about 2 hours to put together this morning, including testing/tweaking, so I'm not really vested). Regarding your alternative, @bryanwweber does not like PS: My objective was to be minimally invasive and only take care of the external interface. |
@ischoegl you couldn't have known, and that's on me. I've been swamped by work so I kept putting off working on this. I had been working on building the case for ![]() and with my branch using ![]() I had become stuck trying to figure out how to prevent |
@TimothyEDawson ... no problem. I have no preference for As an aside, your usage example is pretty much the GUI version of |
a3bac0a
to
0fd6146
Compare
0fd6146
to
d7faa8f
Compare
@TimothyEDawson ... thanks for having a look at this. I eliminated a couple of internal star imports (specifically the This still requires a review by @bryanwweber or @speth, as our workflow requires approvals from non-authors prior to merging. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1947 +/- ##
=======================================
Coverage 74.92% 74.92%
=======================================
Files 448 448
Lines 55763 55777 +14
Branches 9204 9204
=======================================
+ Hits 41780 41792 +12
- Misses 10881 10883 +2
Partials 3102 3102 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Excellent! |
I think there is a misunderstanding about the badness of wildcard/star imports; Per PEP8, the admonition against them specifically excepts the use to "republish an internal interface as part of a public API" which is essentially what we're doing due to the hybrid Cython / pure Python nature of the Cantera package. I'll also point out that star imports are used in a similar way in exemplary projects such as Numpy. The one thing that has changed since our discussion in #616 is that Numpy and some other major projects do seem to have moved toward cleaning up what is exposed in their "public interface", perhaps due to the increased use of IDEs and type checkers that do rely on this sort of introspection, so I do understand the desire to achieve a similar cleanup for Cantera. I still strongly dislike the idea of creating a requirement to maintain these extensive lists of all exported names, especially when those lists are separated from the corresponding definitions (what is this, C++? 🙄). I am certain that this will lead to confusion for anyone who tries to add a new function/class in Cantera and wonders why it's inaccessible. Further, the vast majority of the names currently in the
Regarding the submodule namespaces like |
@speth ... thanks for your feedback, but I disagree: cleaning up our interfaces is a direction that is worth pursuing.
Since Fwiw, I found one instance for |
I think we're all in agreement that cleaning up the exported interface is a worthwhile effort, I think the disagreement is over the tradeoffs of the different approaches.
I don't think it's that kludgy. How many times do we have
I think that's Ray's point, we have many fewer places to
This sounds like a case where there were previously other imports used, but those were removed over time and never got cleaned up. Seems like an easy fix 😊 |
Oh, one additional point regarding this:
Did you take a look at my |
Given what has been said, I believe that whatever we decide on needs to be in support of type hinting and language server support, as this is what new users will benefit from most, especially if they're not seasoned Python users already. If this requires a little extra work on the maintainer side, so be it. Regarding 2 cents: outside of Fwiw, combining selective imports as in Overall, I'm sitting on the fence regarding continuing here - I provided a solution, but if the decision is to go a different direction, I'm 👌 with it. |
It's pretty clear where the issue comes from 😄; the remedy is less straightforward: unless you selectively import, you'll never find out, as |
I am not saying that it isn't worth cleaning up our interface. If that was what I meant, I would not have been suggesting alternative approaches to achieving this goal.
For these uses within Cantera, where the classes come from the Cython module, does changing this to an explicit import even help? If I add
I was referring to the "explicit import" approach that was implemented in this PR, which would certainly prevent accessing
This submodule structure is absolutely an implementation detail and not something that should be relied on to be stable across Cantera versions. The existence of the
I think this is a good point, and suggest that we focus at least for now on changes that affect the external interface and how it interacts with these features / tools. There are many internal things within the Cython module that could be changed but I don't think doing so provides any benefit to end users. |
Actually yes, it does! Within the .py file, the
Right, and presumably that's exactly what we want, no? IFF
Sure, I'm just saying that this doesn't only matter for the public interface, it also matters internally. E.g. should internal modules be relying on One last point: I suspect the correct place to implement the explicit import lists will actually be within |
Changes proposed in this pull request
Per discussion #1926 (comment), type hinting should not be attached to external modules imported by Cantera. This PR prevents export of those modules, which should simplify tests requested for #1926.
See also #616
Checklist
scons build
&scons test
) and unit tests address code coverage