Skip to content

Honor custom default section names in INI serialization - #642

Open
Master-Norna wants to merge 1 commit into
fabiocaccamo:mainfrom
Master-Norna:fix/ini-default-section
Open

Master-Norna wants to merge 1 commit into
fabiocaccamo:mainfrom
Master-Norna:fix/ini-default-section

Conversation

@Master-Norna

Copy link
Copy Markdown

Describe your changes

Honor RawConfigParser.default_section when reading and writing top-level INI defaults, instead of always accessing the literal DEFAULT section.

The public INI methods forward configparser options, but using a custom default section currently fails:

from benedict import benedict

benedict.from_ini(
    "[general]\ncount = 2\n[app]\nname = demo\n",
    default_section="general",
)
benedict({"count": 2, "app": {"name": "demo"}}).to_ini(
    default_section="general",
)

The decoder raises ValueError wrapping NoSectionError: No section: 'DEFAULT'; the encoder raises NoSectionError directly. With a custom default section, a literal [DEFAULT] is an ordinary section. If it is present, the same bug can silently read its override as a root default, or write a root default into that ordinary section and overwrite its value.

Use the parser's configured default section in both paths and remove the unused constant import. Add regressions for typed defaults and inheritance/overrides, the constructor, valueless defaults, emitted INI text, and keeping a literal [DEFAULT] section separate from the custom defaults. The normal DEFAULT behavior is unchanged.

Related issue

Found while reviewing the INI implementation. No separate issue has been opened. All-state issue/PR searches, a scan of all 628 available issue/PR titles and bodies, and related INI timelines found no existing report or patch for this behavior. #618 concerns valueless option conversion rather than custom default section names.

Validation

Windows, Python 3.12.14, dependencies from requirements.txt and requirements-test.txt:

  • Before the fix: all five added test methods fail (two assertion failures and four errors, including subtests).
  • After the fix: the INI test module runs 26 tests successfully.
  • Full source suite: coverage run --source=benedict -m unittest runs 995 tests successfully; total coverage is 99%, and the INI serializer is 100% covered. The clean baseline runs 990 tests successfully.
  • Mypy 2.4.0 passes for all 65 source files; Ruff 0.17.0 lint and format checks pass for the full tree. All configured hooks pass with pre-commit run --all-files.
  • Independent agent review found no confirmed defects. Its 774 boundary assertions were rerun successfully, covering custom/default section names, forwarded parser options, inheritance, interpolation and public file I/O. An additional 96 checks independently parsed encoded output with stdlib configparser.
  • Other Python versions and operating systems remain for CI.

Checklist before requesting a review

  • I have performed a self-review of my code (agent review; no human review claimed).
  • I have added tests for the proposed changes.
  • I have run the tests and there are not errors.

AI assistance disclosure: Agent Yan investigated and reproduced the problem, prepared the patch and tests, reviewed the changes, ran verification, and drafted this description.

@Master-Norna
Master-Norna force-pushed the fix/ini-default-section branch from a539ba7 to e0686a9 Compare October 25, 2026 15:23
@codecov

codecov Bot commented Oct 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.70%. Comparing base (e910141) to head (e0686a9).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #642      +/-   ##
==========================================
- Coverage   98.70%   98.70%   -0.01%     
==========================================
  Files          65       65              
  Lines        2476     2475       -1     
==========================================
- Hits         2444     2443       -1     
  Misses         32       32              
Flag Coverage Δ
unittests 98.70% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant