Skip to content

save_to_* DeprecationWarning has the wrong stacklevel and never reaches users #1323

Open

Description

@davidberenstein1957

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.

Activity

  1. added a commit that references this issue on Aug 19, 2026
    b5cf269
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions