Skip to content

Fix #352: support callable and PathLike entries in default_config_files#353

Merged
bw2 merged 2 commits intomasterfrom
fix-352-callable-default-config-files
May 5, 2026
Merged

Fix #352: support callable and PathLike entries in default_config_files#353
bw2 merged 2 commits intomasterfrom
fix-352-callable-default-config-files

Conversation

@bw2
Copy link
Copy Markdown
Owner

@bw2 bw2 commented May 4, 2026

Summary

  • Fixes handling configuration files without conventional paths #352 by allowing entries in default_config_files to be os.PathLike objects or zero-argument callables that return an open file-like object, in addition to glob path strings.
  • Callables are invoked each time the parser opens config files, the returned stream is closed by the parser after parsing, and exceptions raised during the call are wrapped in a ConfigFileParserException with the original cause preserved.
  • Each callable entry gets a unique source label so distinct sources do not collapse together in format_values() output, even when multiple callables share a __name__ (e.g. lambdas) or return streams with identical .name attributes.
  • Validates default_config_files entries up front in __init__ and updates format_help() so help text renders a readable label for each entry (path string for path-likes, function name for callables).

Test plan

  • python -m unittest tests.test_configargparse
  • black . --check
  • pydoctor ./configargparse.py --intersphinx=https://docs.python.org/3/objects.inv --docformat=google -W

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR extends ArgParser(default_config_files=...) to support non-string sources by allowing os.PathLike entries and zero-argument callables that return open file-like objects, addressing #352’s need to source config from non-filesystem locations.

Changes:

  • Add upfront validation and runtime support for default_config_files entries that are os.PathLike or callables returning streams, including unique per-entry source labels and guaranteed stream cleanup.
  • Improve robustness of config-source tracking/output: prevent format_values() from breaking on | in source names and avoid source-key collisions for multiple callables.
  • Update help/error-message behavior and add extensive unit coverage for callable/pathlike default config sources and stream-closing guarantees.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
configargparse.py Implements callable/PathLike default_config_files, unique source labels, safer cleanup, and help/values formatting updates.
tests/test_configargparse.py Adds comprehensive tests for callable/PathLike default config sources, error wrapping, and stream lifecycle behavior; updates regex expectations for error strings.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread configargparse.py Outdated
Comment thread configargparse.py
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@bw2 bw2 merged commit b2ea0e5 into master May 5, 2026
21 checks passed
@bw2 bw2 deleted the fix-352-callable-default-config-files branch May 5, 2026 10:11
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.

handling configuration files without conventional paths

2 participants