configparser: allow default_section=None - #16262
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
|
Diff from mypy_primer, showing the effect of this PR on open source code: setuptools (https://github.com/pypa/setuptools)
+ setuptools/config/expand.py:371: error: Unused "type: ignore" comment [unused-ignore]
+ setuptools/config/expand.py:372: error: Cannot assign to a method [method-assign]
+ setuptools/config/expand.py:372: error: Incompatible types in assignment (expression has type "type[str]", variable has type "Callable[[str], str]") [assignment]
+ setuptools/config/expand.py:375: error: Argument 1 to "pop" of "dict" has incompatible type "str | None"; expected "str" [arg-type]
|
| def __getitem__(self, key: _SectionName) -> SectionProxy: ... | ||
| def __setitem__(self, key: _SectionName, value: _Section) -> None: ... | ||
| def __delitem__(self, key: _SectionName) -> None: ... | ||
| def __iter__(self) -> Iterator[str]: ... |
There was a problem hiding this comment.
A problem with this is that if default_section is given as None, you will get None in __iter__.
|
You're right, and it goes further than >>> p = ConfigParser(default_section=None)
>>> p.read_string("[DEFAULT]\na = 1\n[sec]\nb = 2\n")
>>> list(p)
[None, 'DEFAULT', 'sec']
>>> len(p), None in p, p[None]
(3, True, <Section: None>)
Modelling this properly would mean So I don't think this is worth doing. I'd suggest closing this PR and #12700 as "not modelled"; setuptools already carries a |
Closes #12700
Widen
RawConfigParser.default_sectionand thedefault_sectionparameter ofRawConfigParser.__init__tostr | None, since passingNoneworks at runtime to disable the default section and is relied on in the wild (e.g. setuptools).Agent used: Claude Code