-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Make configparser.RawConfigParser use SectionProxy where appropriate #1527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This won't be necessary once python/typeshed#1527 lands.
Also relevant to getting the types in check: python/cpython#3012 |
stdlib/3/configparser.pyi
Outdated
def items(self, *, raw: bool = ..., vars: _section = ...) -> AbstractSet[Tuple[str, SectionProxy]]: ... | ||
|
||
@overload | ||
def items(self, section: str, raw: bool = ..., vars: _section = ...) -> Iterable[Tuple[str, str]]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually returns a list. Any reason not to use List
in the annotation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No reason I can think of; updated.
I created a file
Using this branch of typeshed, mypy gives me
If I understand this correctly, using the current definition for SectionProxy causes Bottom line, it seems unfortunate that mypy doesn't know that |
@reddraggone9 That is something that needs fixing, but AFAICT this PR doesn't regress that behaviour. Indeed, without this PR, I get the following output from mypy:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebase and I'll merge.
# This is incompatible with Mapping so we ignore the type. | ||
def items(self, section: str = ..., raw: bool = ..., vars: _section = ...) -> Iterable[Tuple[str, _section]]: ... # type: ignore | ||
@overload | ||
def items(self, *, raw: bool = ..., vars: _section = ...) -> AbstractSet[Tuple[str, SectionProxy]]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, return types that are as concrete as possible, and accept types in arguments that are as generic as possible.
This reflects the code in the cpython tree and makes the following (valid) code type-check correctly: ``` from configparser import ConfigParser config = ConfigParser() config.read_dict({'section': {'key': 'false'}}) assert config['section'].getboolean('key') is False ```
Because .items() uses __getitem__ to produce second item in each tuple.
TL;DR, we're hitting python/mypy#3805 when implemented correctly as an override, and python/mypy#1855 when we try to work around that with a union.
@ambv Done. |
python/typeshed#1527 has landed in mypy master, which is what we use in tests.
python/typeshed#1527 has landed in mypy master, which is what we use in tests.
This reflects the code in the cpython tree and makes the following
(valid) code type-check correctly: