Skip to content
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 optional feature handling to preserve signature of ui.plotly etc. #1266

Merged
merged 1 commit into from
Jul 26, 2023

Conversation

falkoschindler
Copy link
Contributor

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 no go.Figure. And in pyplot.py raising the ImportError is enough to prevent the remaining code from being executed.

@falkoschindler falkoschindler added this to the 1.3.6 milestone Jul 26, 2023
@falkoschindler falkoschindler added the enhancement New feature or request label Jul 26, 2023
Copy link
Member

@rodja rodja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. It even looks cleaner 🧹

@falkoschindler falkoschindler merged commit 196ad18 into main Jul 26, 2023
1 of 5 checks passed
@falkoschindler falkoschindler deleted the optional-features branch July 26, 2023 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants