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
5 changes: 4 additions & 1 deletion codecarbon/emissions_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ def _resolve_output_methods(
warnings.warn(
"The save_to_* parameters are deprecated and will be removed in a "
"future version. Use output_methods=[OutputMethod.CSV, ...] instead.",
DeprecationWarning,
# FutureWarning, not DeprecationWarning: this one is aimed at
# end users, and the default filter hides DeprecationWarning
# unless it comes from __main__.
FutureWarning,
stacklevel=2,
)

Expand Down
23 changes: 21 additions & 2 deletions tests/test_emissions_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tempfile
import time
import unittest
import warnings
from pathlib import Path
from unittest import mock

Expand Down Expand Up @@ -383,7 +384,7 @@ def test_save_to_flags_map_to_output_methods_and_warn(
mocked_is_gpu_details_available,
mocked_is_nvidia_system,
):
with self.assertWarns(DeprecationWarning):
with self.assertWarns(FutureWarning):
tracker = EmissionsTracker(
output_dir=self.temp_path,
output_handlers=[],
Expand All @@ -408,7 +409,7 @@ def test_output_methods_overrides_save_to_flags(
mocked_is_gpu_details_available,
mocked_is_nvidia_system,
):
with self.assertWarns(DeprecationWarning):
with self.assertWarns(FutureWarning):
tracker = EmissionsTracker(
output_dir=self.temp_path,
output_handlers=[],
Expand Down Expand Up @@ -1108,3 +1109,21 @@ def test_cumulative_emissions_with_varying_intensity(

# Verification: If it wasn't cumulative, it would be 3.0 kWh * 300 g/kWh = 0.9 kg
self.assertLess(data3.emissions, 0.8)


def test_deprecation_warning_survives_the_default_filter():
"""The save_to_* deprecation is aimed at end users, so it must not be a
DeprecationWarning: the default filter would silently drop it."""
for tracker_cls, extra in (
(EmissionsTracker, {}),
(OfflineEmissionsTracker, {"country_iso_code": "FRA"}),
):
with warnings.catch_warnings(record=True) as recorded:
warnings.simplefilter("default")
# What Python installs by default, and what hid this warning before.
warnings.simplefilter("ignore", DeprecationWarning)
tracker_cls(save_to_file=False, **extra)

assert [
w for w in recorded if issubclass(w.category, FutureWarning)
], f"no FutureWarning raised by {tracker_cls.__name__}"
Loading