Skip to content

fix(cli): handle missing [codecarbon] section and write api_endpoint via ConfigParser - #1433

Open
sohammishra864-wq wants to merge 2 commits into
mlco2:masterfrom
sohammishra864-wq:fix/cli-config-section
Open

sohammishra864-wq wants to merge 2 commits into
mlco2:masterfrom
sohammishra864-wq:fix/cli-config-section

Conversation

@sohammishra864-wq

@sohammishra864-wq sohammishra864-wq commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Description

  • get_config() now returns {} instead of None when the config file has no [codecarbon] section.
  • get_api_endpoint() now sets the default api_endpoint through ConfigParser instead of raw-appending a line, so it always lands under [codecarbon].

Related Issue

Fixes #1432

Motivation and Context

show_config() crashed with TypeError on a config file that had no [codecarbon] section. get_api_endpoint() wrote the key under the last section of multi-section files, so it was never found and got appended again on every call.

How Has This Been Tested?

Added two tests in tests/cli/test_cli_utils.py. Both fail on master and pass with this change. pytest tests/cli: 76 passed.

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
  • 🟠 AI-generated
  • ⭐ AI-assisted
  • ♻️ No AI used

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.

get_api_endpoint now writes the default endpoint via ConfigParser so it lands under [codecarbon] instead of the last section. Fixes mlco2#1432.
@sohammishra864-wq
sohammishra864-wq requested a review from a team as a code owner September 29, 2026 14:42

@davidberenstein1957 davidberenstein1957 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #1432 cleanly: get_config returning {} and writing api_endpoint through ConfigParser puts the key under [codecarbon] where the reader expects it. One thing worth knowing: config.write rewrites the whole file, so comments in an existing .codecarbon.config are dropped. Acceptable for a config file we own, but a line in the PR description would help.

@codecov

codecov Bot commented Oct 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.07%. Comparing base (7929a4f) to head (cf448b8).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1433   +/-   ##
=======================================
  Coverage   92.07%   92.07%           
=======================================
  Files          49       49           
  Lines        5199     5200    +1     
=======================================
+ Hits         4787     4788    +1     
  Misses        412      412           

☔ 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.

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.

CLI: get_config() returns None without [codecarbon] section; get_api_endpoint() appends api_endpoint under wrong section

2 participants