Skip to content

[configparser] Narrow RawConfigParser.values() to SectionProxy - #16238

Open
advitrocks9 wants to merge 1 commit into
python:mainfrom
advitrocks9:configparser-values-sectionproxy
Open

[configparser] Narrow RawConfigParser.values() to SectionProxy#16238
advitrocks9 wants to merge 1 commit into
python:mainfrom
advitrocks9:configparser-values-sectionproxy

Conversation

@advitrocks9

Copy link
Copy Markdown

Closes: #11547

RawConfigParser already narrows __getitem__ and items() to SectionProxy but never declared
values(), so it falls through to Mapping.values with _VT_co bound to _Section.

Deriving the class from MutableMapping[str, SectionProxy] instead would regress code that checks
clean today:

error: Dict entry 0 has incompatible type "str": "dict[str, str]"; expected "str": "SectionProxy"  [dict-item]
error: Argument 2 to "setdefault" of "MutableMapping" has incompatible type "dict[str, str]"; expected "SectionProxy"  [arg-type]

on cp.update({"a": {"x": "1"}}) and cp.setdefault("b", {"y": "2"}), both of which work at
runtime. _Parser is also the declared parameter type of the four Interpolation hooks, so
narrowing it breaks third-party subclasses.

popitem() has the same gap and is narrowable on its own, so I left it for a separate PR.
setdefault() isn't: it returns the caller's own object, not the proxy it stored.

Assisted by Claude Code.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

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.

configparser.ConfigParser.values() should return a ValuesView[configparser.SectionProxy]

1 participant