refactor: deprecate kwargs in st.plotly_chart and add config dict instead#12291
Conversation
🎉 Snyk checks have passed. No issues have been found so far.✅ security/snyk check is complete. No issues have been found. (View Details) ✅ license/snyk check is complete. No issues have been found. (View Details) |
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the st.plotly_chart function to deprecate the use of **kwargs and introduces a new config parameter for passing configuration options directly to Plotly's plot() function. This change improves the API's clarity and follows best practices by making the configuration parameter explicit rather than relying on generic keyword arguments.
Key changes:
- Added a new
configparameter of typedict | Noneto the function signature - Added deprecation warning for
**kwargsusage - Updated parameter documentation to reflect the new
configparameter and deprecation
Co-authored-by: Copilot <[email protected]>
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
| "box", | ||
| "lasso", | ||
| ), | ||
| config: dict | None = None, |
There was a problem hiding this comment.
The config parameter requires a more specific type annotation. Replace dict | None with dict[str, Any] | None to specify the dictionary's key-value types. The Python Guide mandates adding proper typing annotations to every new function parameter, and generic dict typing does not meet this requirement.
| config: dict | None = None, | |
| config: dict[str, Any] | None = None, |
Spotted by Diamond (based on custom rule: Python Guide)
Is this helpful? React 👍 or 👎 to let us know.
| if config is None: | ||
| config = dict(kwargs.get("config", {})) | ||
| # Copy over some kwargs to config dict. Plotly does the same in plot(). | ||
| # TODO: Remove this once we remove the kwargs support. | ||
| config.setdefault("showLink", kwargs.get("show_link", False)) | ||
| config.setdefault("linkText", kwargs.get("link_text", False)) |
There was a problem hiding this comment.
Issue: When a config parameter is provided, deprecated kwargs like show_link and link_text are silently ignored rather than being merged into the config.
This creates inconsistent behavior where the same kwargs work when config=None but have no effect when a config is provided. For proper backward compatibility during the deprecation period, the kwargs should still be processed and merged into any user-provided config dict.
Consider modifying the code to merge the kwargs into the config regardless of whether config was provided by the user:
config = dict(config or {})
# Always merge kwargs into config for backward compatibility
config.setdefault("showLink", kwargs.get("show_link", False))
config.setdefault("linkText", kwargs.get("link_text", False))| if config is None: | |
| config = dict(kwargs.get("config", {})) | |
| # Copy over some kwargs to config dict. Plotly does the same in plot(). | |
| # TODO: Remove this once we remove the kwargs support. | |
| config.setdefault("showLink", kwargs.get("show_link", False)) | |
| config.setdefault("linkText", kwargs.get("link_text", False)) | |
| config = dict(config or {}) | |
| # Copy over some kwargs to config dict. Plotly does the same in plot(). | |
| # TODO: Remove this once we remove the kwargs support. | |
| config.setdefault("showLink", kwargs.get("show_link", False)) | |
| config.setdefault("linkText", kwargs.get("link_text", False)) |
Spotted by Diamond
Is this helpful? React 👍 or 👎 to let us know.
|
Hey @zyfy29, thanks for the contribution! Looks good from product side overall, I just left a few small comments above and we should also make sure this is raising a deprecation warning if a |
|
Hi @zyfy29 , Thanks for submitting this PR! I left one inline comment, but also,
I think some test coverage is needed to handle the new case of Also, if @jrieke wants us to provide users with a warning if they try to pass I have run the CI, and it looks like there are some failures that need to be addressed. Thanks again for your submission! Let me know if you need any more details on getting this PR merge-ready. |
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
|
Thank you for the suggestions! I've updated my pr and added a unit test case. I noticed that some e2e test cases using the |
|
Sorry, I submitted 6a1c15c from copilot but forgot the unit tests. Now It should be OK. |
sfc-gh-lwilby
left a comment
There was a problem hiding this comment.
Thanks @zyfy29 this test coverage looks good to me now.
| config = dict(kwargs.get("config", {})) | ||
| # Copy over some kwargs to config dict. Plotly does the same in plot(). | ||
| config.setdefault("showLink", kwargs.get("show_link", False)) | ||
| config.setdefault("linkText", kwargs.get("link_text", False)) |
There was a problem hiding this comment.
Just double-checking: we already want to fully remove support for kwargs in this PR? The alternative option would be to show a deprecation warning but keep supporting kwargs in addition to config for some time? cc @sfc-gh-jrieke
There was a problem hiding this comment.
Oh no, definitely show a deprecation warning for now but keep kwargs support. I think we can already get rid of copying showLink and linkText to config, since those params don't seem to be used.
| config.setdefault("showLink", kwargs.get("show_link", False)) | ||
| config.setdefault("linkText", kwargs.get("link_text", False)) | ||
|
|
||
| config = config or {} |
There was a problem hiding this comment.
I think we need to do something like: config = config or dict(kwargs.get("config", {})) here to still support kwargs for now. See comment from @sfc-gh-jrieke above.
There was a problem hiding this comment.
I took another look at the adoption numbers and I think it's actually fine as is: config is the most used kwarg, which we will support here via the explicit config parameter. Besides that, usage of other parameters is almost non-existent and hasn't been supported anyways (other than showLink and linkText)
Describe your changes
mark the
**kwargsofst.plotly_chartas deprecated, and add a newconfigargument to directly pass toplot()GitHub Issue Link (if applicable)
**kwargsinst.plotly_chartand addconfiginstead #12280Testing Plan
This is a refactor and doesn't break the existing e2e tests for
st.plotly_chart, so no new tests are needed.Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.