Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions plotly/matplotlylib/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 0 additions & 8 deletions plotly/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
65 changes: 65 additions & 0 deletions tests/test_core/test_warnings_format.py
Original file line number Diff line number Diff line change
@@ -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"
)