diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b0d878b358..aefb4c603b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +### Fixed +- Fix issue where plotly was changing the global warnings format [[#5472](https://github.com/plotly/plotly.py/issues/5472)] + ## [6.5.2] - 2026-01-14 ### Fixed diff --git a/plotly/matplotlylib/renderer.py b/plotly/matplotlylib/renderer.py index d55f86ef3c2..71f5b207db0 100644 --- a/plotly/matplotlylib/renderer.py +++ b/plotly/matplotlylib/renderer.py @@ -14,14 +14,6 @@ from plotly.matplotlylib import mpltools -# Warning format -def warning_on_one_line(msg, category, filename, lineno, file=None, line=None): - return "%s:%s: %s:\n\n%s\n\n" % (filename, lineno, category.__name__, msg) - - -warnings.formatwarning = warning_on_one_line - - class PlotlyRenderer(Renderer): """A renderer class inheriting from base for rendering mpl plots in plotly. diff --git a/plotly/tools.py b/plotly/tools.py index 67f828204d7..b0875507439 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -55,14 +55,6 @@ ALTERNATIVE_HISTNORM = "probability" -# Warning format -def warning_on_one_line(message, category, filename, lineno, file=None, line=None): - return "%s:%s: %s:\n\n%s\n\n" % (filename, lineno, category.__name__, message) - - -warnings.formatwarning = warning_on_one_line - - ### mpl-related tools ### def mpl_to_plotly(fig, resize=False, strip_style=False, verbose=False): """Convert a matplotlib figure to plotly dictionary and send. diff --git a/tests/test_core/test_warnings_format.py b/tests/test_core/test_warnings_format.py new file mode 100644 index 00000000000..b68c5f7595e --- /dev/null +++ b/tests/test_core/test_warnings_format.py @@ -0,0 +1,65 @@ +"""Test that plotly doesn't modify global warnings format (Issue #5472)""" +import warnings +import sys +from unittest import TestCase + + +class TestWarningsFormat(TestCase): + """Test that importing plotly modules doesn't change warnings.formatwarning""" + + def test_import_plotly_does_not_change_warnings_format(self): + """Importing plotly should not modify the global warnings format""" + # Store the original format + original_format = warnings.formatwarning + + # Import plotly modules that previously modified warnings.formatwarning + # We need to reimport to test the current code state + import importlib + + # Force reimport of tools module to test current state + if 'plotly.tools' in sys.modules: + importlib.reload(sys.modules['plotly.tools']) + else: + import plotly.tools + + # Check that warnings.formatwarning is still the original + self.assertIs( + warnings.formatwarning, + original_format, + "Importing plotly.tools changed warnings.formatwarning globally" + ) + + def test_import_matplotlylib_renderer_does_not_change_warnings_format(self): + """Importing matplotlylib.renderer should not modify the global warnings format""" + # Store the original format + original_format = warnings.formatwarning + + import importlib + + # Force reimport of renderer module to test current state + if 'plotly.matplotlylib.renderer' in sys.modules: + importlib.reload(sys.modules['plotly.matplotlylib.renderer']) + else: + import plotly.matplotlylib.renderer + + # Check that warnings.formatwarning is still the original + self.assertIs( + warnings.formatwarning, + original_format, + "Importing plotly.matplotlylib.renderer changed warnings.formatwarning globally" + ) + + def test_warnings_format_unchanged_after_multiple_imports(self): + """Multiple imports should not change warnings format""" + original_format = warnings.formatwarning + + # Import both modules + import plotly.tools + import plotly.matplotlylib.renderer + + # Verify format is unchanged + self.assertIs( + warnings.formatwarning, + original_format, + "warnings.formatwarning was changed after importing plotly modules" + )