Refactor optional feature handling to preserve signature of ui.plotly etc. #1266
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.
Recently we checked in ui.py if certain dependencies can be imported. Otherwise a dummy function with args and kwargs was created to raise an ImportError once the element constructor is called.
Unfortunately this has the disadvantage of hiding the original signature in the IDE. VSCode seems to find the dummy function only, even if the dependency exists.
This PR moves such import checks into the respective UI elements, keeping ui.py nice and clean. Modules like plotly.py try to import dependencies and update
globals.optional_features
. If the import fails, the feature is not registered.Only in the initializer we check if the feature is registered and raise an ImportError otherwise. This preserves the signature for the IDE and raises the exception exactly when needed.
But we need to make sure that the modules can be imported even if the dependency doesn't exist. In plotly.py I added
from __future__ import annotations
so that type annotations don't lead to runtime errors, even if there is nogo.Figure
. And in pyplot.py raising the ImportError is enough to prevent the remaining code from being executed.