-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Import and initialization optimizations #2368
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4ee88bc
Lazy imports of graph object hierarchy for Python 3.7+
jonmmease cb70a40
lazy submodule imports for plotly and plotly.io modules
jonmmease ceb9523
Don't force import pandas and numpy if we're only checking whether an…
jonmmease 5974cf5
Don't auto-import all trace type classes
jonmmease ffc277d
Don't construct validators up front for every object when object is c…
jonmmease 181e427
Test fixes
jonmmease aec7e9a
Add optional validation using go.validate object as callable of conte…
jonmmease 1084ba0
Delay additional imports
jonmmease 7e50c44
dynamic to static docstrings for Figure io methods
jonmmease fb8eac5
Update packages/python/plotly/_plotly_utils/importers.py
jonmmease f7e672b
Use module-level __dir__ function to restore IPython tab completion w…
jonmmease 20fc393
Don't fail hierarchy test when ipywidgets not installed
jonmmease f979814
cut commented print statements
jonmmease 7600eaf
Test fix, FigureWidget is not expected to be importable when ipywidge…
jonmmease File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import importlib | ||
|
||
|
||
def relative_import(parent_name, rel_modules=(), rel_classes=()): | ||
""" | ||
Helper function to import submodules lazily in Pythong 3.7+ | ||
|
||
Parameters | ||
---------- | ||
rel_modules: list of str | ||
list of submodules to import, of the form .submodule | ||
rel_classes: list of str | ||
list of submodule classes/variables to import, of the form ._submodule.Foo | ||
|
||
Returns | ||
------- | ||
tuple | ||
Tuple that should be assigned to __all__, __getattr__ in the caller | ||
""" | ||
module_names = {rel_module.split(".")[-1]: rel_module for rel_module in rel_modules} | ||
class_names = {rel_path.split(".")[-1]: rel_path for rel_path in rel_classes} | ||
|
||
def __getattr__(import_name): | ||
# In Python 3.7+, lazy import submodules | ||
|
||
# Check for submodule | ||
if import_name in module_names: | ||
# print(parent_name, import_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove commented out print statements |
||
rel_import = module_names[import_name] | ||
return importlib.import_module(rel_import, parent_name) | ||
|
||
# Check for submodule class | ||
if import_name in class_names: | ||
# print(parent_name, import_name) | ||
rel_path_parts = class_names[import_name].split(".") | ||
rel_module = ".".join(rel_path_parts[:-1]) | ||
class_name = import_name | ||
class_module = importlib.import_module(rel_module, parent_name) | ||
return getattr(class_module, class_name) | ||
|
||
raise AttributeError( | ||
"module {__name__!r} has no attribute {name!r}".format( | ||
name=import_name, __name__=parent_name | ||
) | ||
) | ||
|
||
__all__ = list(module_names) + list(class_names) | ||
|
||
return __all__, __getattr__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.