Repository navigation
fix: show the save_to_* deprecation to users, attributed to their code - #1346
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1346 +/- ##
==========================================
+ Coverage 93.80% 93.82% +0.02%
==========================================
Files 51 51
Lines 5710 5734 +24
==========================================
+ Hits 5356 5380 +24
Misses 354 354 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
b5cf269 to
55855fb
Compare
Verdict: 🔧 Request changesThe title and description mention a dynamic stacklevel and moving the CLI to Must fix:
Switching to |
|
Made the changes in 99b6c55: CLI moved to |
|
Merged master to resolve conflicts in db90765. |
6f713eb to
8a7e29c
Compare
|
Rebased on
Generated by Claude Code |
The warning is raised inside _resolve_output_methods, so it is attributed to emissions_tracker.py and Python's default filter -- which only shows DeprecationWarning from __main__ -- drops it for every user. FutureWarning exists for exactly this: deprecations aimed at end users that must always be shown. It makes stacklevel irrelevant to visibility. (skip_file_prefixes= would be the stdlib answer, but it is 3.12+ and requires-python is >=3.10.) Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…tput_methods Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
_configured_output_methods re-read the hierarchical config, bypassing the error handling of _external_config(). Pass the dict monitor already has. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JfRKgUd281URY6fMyNAQmh
The save_to_* deprecation is now a FutureWarning shown by default, so copying these snippets would print it. Keep CSV in each list to match the previous behaviour (save_to_file defaults to True), and mention the warning in the deprecation note. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JfRKgUd281URY6fMyNAQmh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JfRKgUd281URY6fMyNAQmh
8a7e29c to
6dbb9a4
Compare
benoit-cty
left a comment
There was a problem hiding this comment.
Great, it makes our code cleaner.
Codecov flagged the 'return 2' fallback, reached only when every frame is inside codecarbon, as the one uncovered line of the patch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Description
The
save_to_*deprecation is now aFutureWarning(shown by default per PEP 565), and itsstacklevelis computed by a new_caller_stacklevel()helper, which walks the stack to the first frame outside thecodecarbonpackage (skipping thecontextlibframe added by@suppress). The warning now points at the user'sEmissionsTracker(...)/OfflineEmissionsTracker(...)line instead ofemissions_tracker.py. Incodecarbon/cli/main.py,monitornow passesoutput_methods=built from configuration, withOutputMethod.APIadded for--apiand removed for--no-api, preservingsave_to_api=api's add/remove behaviour and making--apiwork even whenoutput_methodsis set in the config;detectpassesoutput_methods=[].codecarbon/cli/monitor.pydrops thesave_to_logger=Falseargument, which was previously hard-coded regardless of configuration.Related Issue
Fixes #1323
Motivation and Context
The deprecation warning was raised inside
_resolve_output_methods, so it was attributed toemissions_tracker.py, and Python's default filter only showsDeprecationWarningfrom__main__, which dropped the warning for every user.FutureWarningis the correct category for deprecations aimed at end users that must always be shown, and it also makesstacklevelaccuracy matter for pointing at the right line.Behaviour change worth flagging: dropping
save_to_logger=Falsefromcodecarbon/cli/monitor.pyis not a no-op. Previouslymonitoralways forced the logger off regardless of configuration; now, since the argument is no longer passed, the tracker falls back to readingsave_to_loggerfrom the user's config file. So running offlinecodecarbon monitor -- cmdwithsave_to_logger=truein.codecarbon.confignow enables the logger output, where before it was always disabled. This matches how the tracker reads every othersave_to_*key from config when the CLI doesn't override it, so the change is intended and makesmonitorconsistent with the rest of the config-driven output methods, but it is a behaviour change for anyone who hadsave_to_logger=truein their config and relied onmonitorsilently ignoring it.How Has This Been Tested?
test_deprecation_warning_survives_the_default_filternow also assertsw.filename == __file__for both tracker classes.output_methodsinstead ofsave_to_api; a new parametrisedtest_monitor_api_flag_adds_to_configured_output_methodspins the add/remove behaviour against configured outputs, including a case withsave_to_logger: "true"in config confirming the logger is now picked up.Screenshots (if appropriate):
N/A
Types of changes
AI Usage Disclosure
Checklist: