-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[refactor] Better import order by using __all__ #1510
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks this LGTM. But please wait on @rexwangcc or other Py experts to take a pass on this..
python/taichi/__init__.py
Outdated
return task.run([]) | ||
|
||
|
||
__all__ = [s for s in dir() if not s.startswith('_')] + ['settings'] | ||
__all__ = [s for s in dir() if not s.startswith('_')] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, this is a package, and __all__
of a package should define the modules it loads upon a wildcard import? (https://docs.python.org/3/tutorial/modules.html#importing-from-a-package). But I'm just voicing my wondering,
maybe this is the only option without breaking tons of things in the current situation...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought __all__
here is only work for from taichi import *
and our user should never use that..? Anyway, let's cover that usage too. I think in the future we can use this for user shortcuts like:
__all__ = [s for s in dir() if not s.startswith('_')] | |
ti = __import__('taichi') | |
__all__ = ['Vector', 'Matrix', ..., 'ti'] |
So that users can directly use Vector([2, 3])
if they use from taichi import *
.
Thanks for making the PR! I'm not a 🐍 expert, just trying to give my 2 cents here: When I was advocating "getting rid of
While there are various good and practical ways to address My only concern is that using |
Both: # __init__.py
from .util import a, b, c, d or # __init__.py
from .util import *
# util.py
__all__ = ['a', 'b', 'c', 'd'] solve the problem. Why I choose 2? Because I think it's convinent to manage exported-name inplace in |
Codecov Report
@@ Coverage Diff @@
## master #1510 +/- ##
==========================================
- Coverage 66.20% 66.15% -0.05%
==========================================
Files 38 40 +2
Lines 5403 5404 +1
Branches 975 976 +1
==========================================
- Hits 3577 3575 -2
- Misses 1658 1661 +3
Partials 168 168
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your latest argument makes sense to me. (I still think using less *
can help track the provenance better w/o an IDE, but let's see how this goes) This looks the most smooth approach to the current code organization among all potential solutions, LGTM! Thanks for doing this
Related issue = #
[Click here for the format server]
@rexwangcc As you said,
from xxx import *
can be a bad anti-pattern unless we use__all__
, so here I am.This PR is stage 1, I will dig further into
taichi.lang
only after @yuanming-hu merge all other PRs aboutlang
to pvc.