Define __all__ for qiskit#7274
Conversation
|
I'm actually pretty against this. For one, If we were to do this, we should actually define |
mtreinish
left a comment
There was a problem hiding this comment.
This doesn't do what you think it will and is not a good idea. qiskit.circuit is a subpackage and trying to delete it like this doesn't actually behave consistently and will potentially cause weird issues down the line as the qiskit module getattr will differ from sys modules list.
The issue here is new python developers and we'll need to teach them and learn to avoid * imports (as this is a common thing with that antipattern and why pylint yells at you about it) and name overloading. Everything is new if you've never used python or qiskit before and I get it can be frsutrating, but this is something we have to fix with documentation and learning material. If people are blindly copying and pasting chunks of code without context and hitting errors that's not really something we should try to guard against in the library.
I think Shall I move in the |
|
Sure, it doesn't hurt to have Some of this is an A/B problem - sure, The Python docs have this to say about star-imports and
|
Pull Request Test Coverage Report for Build 1509200825
💛 - Coveralls |
| import pkgutil | ||
| import sys | ||
| import warnings | ||
| import os |
There was a problem hiding this comment.
is this imported by mistake?
There was a problem hiding this comment.
We used to check some environment variables here about suppressing packaging warnings - looks like import os got left after those were removed in #5619. Pylint won't moan about unused imports in __init__ by default I think, because most names in an initialiser won't be used.
|
I defined an Other option is I slightly prefer the first one, but no strong opinion. |
|
Defining On a minor note: technically this is a public API change and subject to the deprecation policy. However, realistically the only way to detect star-imports is super hacky, and the people who might be bitten by stuff like this (hopefully) aren't writing dependent libraries anyway. |
I added an |
| import pkgutil | ||
| import sys | ||
| import warnings | ||
| import os |
There was a problem hiding this comment.
We used to check some environment variables here about suppressing packaging warnings - looks like import os got left after those were removed in #5619. Pylint won't moan about unused imports in __init__ by default I think, because most names in an initialiser won't be used.
| "AncillaRegister", | ||
| "BasicAer", | ||
| "ClassicalRegister", | ||
| "_config", |
There was a problem hiding this comment.
| "_config", |
We probably don't want to be exporting a private name by default, unless there's a really good reason to.
There was a problem hiding this comment.
It's still usable, but at the very least I'd think something like that ought to be through qualified access, so we're fine not to include it in __all__.
Co-authored-by: Jake Lishman <jake@binhbar.com>
jakelishman
left a comment
There was a problem hiding this comment.
I think we can merge this PR for 0.19 so I'm keen to do it sooner rather than later to marginally mitigate the insane CI crunch that's doubtless coming for us on Monday. I'm not keen on removing even the private names from qualified access in an unrelated PR this soon before release, though, so let's leave them in, and we can remove them in a separate PR if they're deemed necessary for removal.
jakelishman
left a comment
There was a problem hiding this comment.
This trimmed down version that simply defines __all__ to the public classes and functions we actually want to export is fine by me.
While it's out-of-scope for this PR, I think it may be worth considering promoting Clbit and Qubit to the top-level namespace as well, since they're now more core than ClassicalRegister and QuantumRegister. But that's for another time.
Many users tend to copy example code from several places in their first attempts to use Qiskit with the following sequence:
If the quantum circuit to draw is not called
circuit, the following counter intuitive error pops:Even if the local variable exists, the namespace seems like polluted somehow.
This happens enough to be quite a common question/confusion (here, here, here)
This PR
removessetscircuitfromqiskit/__init__.pynamespace__all__inqiskit/__init__.py.