Skip to content

fix: show the save_to_* deprecation to users, attributed to their code - #1346

Open
davidberenstein1957 wants to merge 3 commits into
masterfrom
fix/deprecation-warning-stacklevel
Open

davidberenstein1957 wants to merge 3 commits into
masterfrom
fix/deprecation-warning-stacklevel

Conversation

@davidberenstein1957

@davidberenstein1957 davidberenstein1957 commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Description

The save_to_* deprecation is now a FutureWarning (shown by default per PEP 565), and its stacklevel is computed by a new _caller_stacklevel() helper, which walks the stack to the first frame outside the codecarbon package (skipping the contextlib frame added by @suppress). The warning now points at the user's EmissionsTracker(...) / OfflineEmissionsTracker(...) line instead of emissions_tracker.py. In codecarbon/cli/main.py, monitor now passes output_methods= built from configuration, with OutputMethod.API added for --api and removed for --no-api, preserving save_to_api=api's add/remove behaviour and making --api work even when output_methods is set in the config; detect passes output_methods=[]. codecarbon/cli/monitor.py drops the save_to_logger=False argument, which was the default anyway.

Related Issue

Fixes #1323

Motivation and Context

The deprecation warning was raised inside _resolve_output_methods, so it was attributed to emissions_tracker.py, and Python's default filter only shows DeprecationWarning from __main__, which dropped the warning for every user. FutureWarning is the correct category for deprecations aimed at end users that must always be shown, and it also makes stacklevel accuracy matter for pointing at the right line.

How Has This Been Tested?

  • test_deprecation_warning_survives_the_default_filter now also asserts w.filename == __file__ for both tracker classes.
  • CLI tests assert on output_methods instead of save_to_api; a new parametrised test_monitor_api_flag_adds_to_configured_output_methods pins the add/remove behaviour against configured outputs.

Screenshots (if appropriate):

N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

AI Usage Disclosure

  • 🟥 AI-vibecoded: You cannot explain the logic. Car analogy : the car drive by itself, you are outside it and just tell it where to go.
  • 🟠 AI-generated: Car analogy : the car drive by itself, you are inside and give instructions.
  • ⭐ AI-assisted. Car analogy : you drive the car, AI help you find your way.
  • ♻️ No AI used. Car analogy : you drive the car.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the docs/how-to/contributing.md document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.42857% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.72%. Comparing base (e5e46ab) to head (db90765).

Files with missing lines Patch % Lines
codecarbon/emissions_tracker.py 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1346      +/-   ##
==========================================
+ Coverage   91.70%   91.72%   +0.02%     
==========================================
  Files          49       49              
  Lines        5157     5183      +26     
==========================================
+ Hits         4729     4754      +25     
- Misses        428      429       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@davidberenstein1957
davidberenstein1957 marked this pull request as ready for review August 13, 2026 05:05
@davidberenstein1957
davidberenstein1957 requested a review from a team as a code owner August 13, 2026 05:05
@davidberenstein1957
davidberenstein1957 force-pushed the fix/deprecation-warning-stacklevel branch from 3958b57 to b5cf269 Compare August 19, 2026 13:24
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>
@davidberenstein1957
davidberenstein1957 force-pushed the fix/deprecation-warning-stacklevel branch from b5cf269 to 55855fb Compare August 19, 2026 14:21
@github-actions github-actions Bot added size/S and removed size/M labels Aug 19, 2026
@benoit-cty

Copy link
Copy Markdown
Contributor

🤖 This review comment was written and posted by Claude Opus 5.5 (AI assistant), at the request of @benoit-cty. Findings were checked by reading the code and running tests locally (merged with current master where relevant), but please double-check before acting on them.

Verdict: 🔧 Request changes

The title and description mention a dynamic stacklevel and moving the CLI to output_methods=. The single commit in this PR only changes DeprecationWarning to FutureWarning and keeps stacklevel=2.

Must fix:

  1. The CLI now shows users a warning they can't act on. FutureWarning is shown by default, but the CLI itself still passes the deprecated arguments:

    • codecarbon/cli/main.py:425 passes save_to_api= on every online monitor run
    • codecarbon/cli/main.py:476 (detect) passes save_to_file=False
    • codecarbon/cli/monitor.py:75 passes save_to_logger=False for monitor -- cmd

    I verified that codecarbon detect now prints emissions_tracker.py:572: FutureWarning: The save_to_* parameters are deprecated... on every run. Please migrate these call sites to output_methods= in this same PR.

  2. The warning still points at the library, not the caller, so save_to_* DeprecationWarning has the wrong stacklevel and never reaches users #1323 isn't fixed (emissions_tracker.py ~L223-230).

    • Both EmissionsTracker(...) and OfflineEmissionsTracker(...) report emissions_tracker.py:572.
    • Options:
      • compute the stacklevel by walking frames until you leave the codecarbon package
      • on Python 3.12+, use warnings.warn(..., skip_file_prefixes=(os.path.dirname(codecarbon.__file__),)), with the frame-walk as a fallback for older versions
    • The test only checks that a FutureWarning is raised. Please also assert w[0].filename == __file__, so the attribution is locked in.
  3. Update the title and description to match what the code does.

Switching to FutureWarning for a user-facing deprecation is reasonable (PEP 565). It just needs points 1 and 2 to go with it.

…tput_methods

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@davidberenstein1957 davidberenstein1957 changed the title fix: attribute save_to_* deprecation to caller fix: show the save_to_* deprecation to users, attributed to their code Sep 23, 2026
@github-actions github-actions Bot added size/M and removed size/S labels Sep 23, 2026
@davidberenstein1957

Copy link
Copy Markdown
Collaborator Author

Made the changes in 99b6c55: CLI moved to output_methods=, warning now points at the caller (filename asserted in the test), title and description updated.

Resolve conflict in codecarbon/cli/monitor.py: keep master's conditional
log_level forwarding via tracker_args and the PR's removal of the
deprecated save_to_logger argument.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@davidberenstein1957

Copy link
Copy Markdown
Collaborator Author

Merged master to resolve conflicts in db90765.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

save_to_* DeprecationWarning has the wrong stacklevel and never reaches users

2 participants