mlco2 / mlco2/codecarbon

save_to_* DeprecationWarning has the wrong stacklevel and never reaches users

Open
#1,323 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.9k
Forks
323
Avg merge
1d 12h
Merged PRs (30d)
12

Description

The deprecation warning for the save_to_* constructor parameters is emitted with a stacklevel that points inside CodeCarbon itself, so Python's default ignore::DeprecationWarning filter drops it for every caller that is not a top-level __main__ script. Users who pass save_to_file=True from a library module are never told to migrate to output_methods=.

Reproduction

# mylib.py
from codecarbon import EmissionsTracker
tracker = EmissionsTracker(save_to_file=True)   # deprecated

python -c "import mylib" prints nothing. Under warnings.simplefilter("always") the warning is recorded, but its filename is codecarbon/emissions_tracker.py, not the calling module.

Root cause

codecarbon/emissions_tracker.py:227 warns with stacklevel=2 from _resolve_output_methods, which is called from BaseEmissionsTracker.__init__ (emissions_tracker.py:569). Frame 2 is therefore __init__ itself. A fixed stacklevel=3 is not sufficient either: OfflineEmissionsTracker.__init__ (emissions_tracker.py:1313) adds another frame, so the correct level depends on the entry point.

Secondary: CodeCarbon's own CLI passes deprecated flags, so once the attribution is corrected the CLI warns users about a choice the CLI made — codecarbon/cli/monitor.py:71 (save_to_logger=False, which is also the default and therefore purely redundant), codecarbon/cli/main.py:425 (save_to_api=api) and codecarbon/cli/main.py:476 (save_to_file=False). Today these are hidden by the same default filter, but they hard-fail under -W error::DeprecationWarning or a suite with filterwarnings = error.

Expected vs actual

Expected: the warning is attributed to the user code that constructed the tracker, so it surfaces under the default filter, and CodeCarbon's own CLI does not trigger it.

Actual: the warning is attributed to CodeCarbon internals, is silently dropped for most users, and the CLI trips its own deprecation check.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in codecarbon/emissions_tracker.py at _resolve_output_methods and the BaseEmissionsTracker and OfflineEmissionsTracker constructors, then inspect the CLI call sites in codecarbon/cli/monitor.py and codecarbon/cli/main.py. Done means warnings identify the user’s construction site and CodeCarbon’s CLI no longer emits deprecated-parameter warnings, including under warnings treated as errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.