Skip to content

Commit 24a7bfb

Browse files
OddBlokeambv
authored andcommitted
Make configparser.RawConfigParser use SectionProxy where appropriate (#1527)
* Make configparser.RawConfigParser.__getitem__ return a SectionProxy 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 ``` * RawConfigParser.items() returns SectionProxys not mappings Because .items() uses __getitem__ to produce second item in each tuple. * section argument to RawConfigParser.items is Optional * Add comment explaining the status of RawConfigParser.items 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. * Correctly implement RawConfigParser.items overloading * RawConfigParser.items(str) returns a List not an Iterable
1 parent 6f848e8 commit 24a7bfb

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

‎stdlib/3/configparser.pyi‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# reading configparser.py.
33

44
import sys
5-
from typing import (MutableMapping, Mapping, Dict, Sequence, List, Union,
6-
Iterable, Iterator, Callable, Any, IO, overload, Optional,
7-
Pattern, TypeVar)
5+
from typing import (AbstractSet, MutableMapping, Mapping, Dict, Sequence, List,
6+
Union, Iterable, Iterator, Callable, Any, IO, overload,
7+
Optional, Pattern, TypeVar)
88
# Types only used in type comments only
99
from typing import Optional, Tuple # noqa
1010

@@ -70,7 +70,7 @@ class RawConfigParser(_parser):
7070

7171
def __len__(self) -> int: ...
7272

73-
def __getitem__(self, section: str) -> _section: ...
73+
def __getitem__(self, section: str) -> SectionProxy: ...
7474

7575
def __setitem__(self, section: str, options: _section) -> None: ...
7676

@@ -112,8 +112,11 @@ class RawConfigParser(_parser):
112112
def get(self, section: str, option: str, *, raw: bool = ..., vars: _section = ..., fallback: str = ...) -> str: # type: ignore
113113
...
114114

115-
# This is incompatible with Mapping so we ignore the type.
116-
def items(self, section: str = ..., raw: bool = ..., vars: _section = ...) -> Iterable[Tuple[str, _section]]: ... # type: ignore
115+
@overload
116+
def items(self, *, raw: bool = ..., vars: _section = ...) -> AbstractSet[Tuple[str, SectionProxy]]: ...
117+
118+
@overload
119+
def items(self, section: str, raw: bool = ..., vars: _section = ...) -> List[Tuple[str, str]]: ...
117120

118121
def set(self, section: str, option: str, value: str) -> None: ...
119122

0 commit comments

Comments
 (0)