Skip to content

Commit 0647903

Browse files
authored
Support configparser.UNNAMED_SECTION (#13542) (#13544)
1 parent 8c64939 commit 0647903

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

stdlib/configparser.pyi

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ else:
7777
"MAX_INTERPOLATION_DEPTH",
7878
]
7979

80+
if sys.version_info >= (3, 13):
81+
class _UNNAMED_SECTION: ...
82+
UNNAMED_SECTION: _UNNAMED_SECTION
83+
84+
_SectionName: TypeAlias = str | _UNNAMED_SECTION
85+
# A list of sections can only include an unnamed section if the parser was initialized with
86+
# allow_unnamed_section=True. Any prevents users from having to use explicit
87+
# type checks if allow_unnamed_section is False (the default).
88+
_SectionNameList: TypeAlias = list[Any]
89+
else:
90+
_SectionName: TypeAlias = str
91+
_SectionNameList: TypeAlias = list[str]
92+
8093
_Section: TypeAlias = Mapping[str, str]
8194
_Parser: TypeAlias = MutableMapping[str, _Section]
8295
_ConverterCallback: TypeAlias = Callable[[str], Any]
@@ -87,17 +100,17 @@ DEFAULTSECT: Final = "DEFAULT"
87100
MAX_INTERPOLATION_DEPTH: Final = 10
88101

89102
class Interpolation:
90-
def before_get(self, parser: _Parser, section: str, option: str, value: str, defaults: _Section) -> str: ...
91-
def before_set(self, parser: _Parser, section: str, option: str, value: str) -> str: ...
92-
def before_read(self, parser: _Parser, section: str, option: str, value: str) -> str: ...
93-
def before_write(self, parser: _Parser, section: str, option: str, value: str) -> str: ...
103+
def before_get(self, parser: _Parser, section: _SectionName, option: str, value: str, defaults: _Section) -> str: ...
104+
def before_set(self, parser: _Parser, section: _SectionName, option: str, value: str) -> str: ...
105+
def before_read(self, parser: _Parser, section: _SectionName, option: str, value: str) -> str: ...
106+
def before_write(self, parser: _Parser, section: _SectionName, option: str, value: str) -> str: ...
94107

95108
class BasicInterpolation(Interpolation): ...
96109
class ExtendedInterpolation(Interpolation): ...
97110

98111
if sys.version_info < (3, 13):
99112
class LegacyInterpolation(Interpolation):
100-
def before_get(self, parser: _Parser, section: str, option: str, value: str, vars: _Section) -> str: ...
113+
def before_get(self, parser: _Parser, section: _SectionName, option: str, value: str, vars: _Section) -> str: ...
101114

102115
class RawConfigParser(_Parser):
103116
_SECT_TMPL: ClassVar[str] # undocumented
@@ -220,11 +233,11 @@ class RawConfigParser(_Parser):
220233
def __iter__(self) -> Iterator[str]: ...
221234
def __contains__(self, key: object) -> bool: ...
222235
def defaults(self) -> _Section: ...
223-
def sections(self) -> list[str]: ...
224-
def add_section(self, section: str) -> None: ...
225-
def has_section(self, section: str) -> bool: ...
226-
def options(self, section: str) -> list[str]: ...
227-
def has_option(self, section: str, option: str) -> bool: ...
236+
def sections(self) -> _SectionNameList: ...
237+
def add_section(self, section: _SectionName) -> None: ...
238+
def has_section(self, section: _SectionName) -> bool: ...
239+
def options(self, section: _SectionName) -> list[str]: ...
240+
def has_option(self, section: _SectionName, option: str) -> bool: ...
228241
def read(self, filenames: StrOrBytesPath | Iterable[StrOrBytesPath], encoding: str | None = None) -> list[str]: ...
229242
def read_file(self, f: Iterable[str], source: str | None = None) -> None: ...
230243
def read_string(self, string: str, source: str = "<string>") -> None: ...
@@ -234,26 +247,26 @@ class RawConfigParser(_Parser):
234247
# These get* methods are partially applied (with the same names) in
235248
# SectionProxy; the stubs should be kept updated together
236249
@overload
237-
def getint(self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None) -> int: ...
250+
def getint(self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None) -> int: ...
238251
@overload
239252
def getint(
240-
self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T = ...
253+
self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T = ...
241254
) -> int | _T: ...
242255
@overload
243-
def getfloat(self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None) -> float: ...
256+
def getfloat(self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None) -> float: ...
244257
@overload
245258
def getfloat(
246-
self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T = ...
259+
self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T = ...
247260
) -> float | _T: ...
248261
@overload
249-
def getboolean(self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None) -> bool: ...
262+
def getboolean(self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None) -> bool: ...
250263
@overload
251264
def getboolean(
252-
self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T = ...
265+
self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T = ...
253266
) -> bool | _T: ...
254267
def _get_conv(
255268
self,
256-
section: str,
269+
section: _SectionName,
257270
option: str,
258271
conv: Callable[[str], _T],
259272
*,
@@ -263,29 +276,31 @@ class RawConfigParser(_Parser):
263276
) -> _T: ...
264277
# This is incompatible with MutableMapping so we ignore the type
265278
@overload # type: ignore[override]
266-
def get(self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None) -> str | MaybeNone: ...
279+
def get(self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None) -> str | MaybeNone: ...
267280
@overload
268281
def get(
269-
self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T
282+
self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T
270283
) -> str | _T | MaybeNone: ...
271284
@overload
272285
def items(self, *, raw: bool = False, vars: _Section | None = None) -> ItemsView[str, SectionProxy]: ...
273286
@overload
274-
def items(self, section: str, raw: bool = False, vars: _Section | None = None) -> list[tuple[str, str]]: ...
275-
def set(self, section: str, option: str, value: str | None = None) -> None: ...
287+
def items(self, section: _SectionName, raw: bool = False, vars: _Section | None = None) -> list[tuple[str, str]]: ...
288+
def set(self, section: _SectionName, option: str, value: str | None = None) -> None: ...
276289
def write(self, fp: SupportsWrite[str], space_around_delimiters: bool = True) -> None: ...
277-
def remove_option(self, section: str, option: str) -> bool: ...
278-
def remove_section(self, section: str) -> bool: ...
290+
def remove_option(self, section: _SectionName, option: str) -> bool: ...
291+
def remove_section(self, section: _SectionName) -> bool: ...
279292
def optionxform(self, optionstr: str) -> str: ...
280293
@property
281294
def converters(self) -> ConverterMapping: ...
282295

283296
class ConfigParser(RawConfigParser):
284297
# This is incompatible with MutableMapping so we ignore the type
285298
@overload # type: ignore[override]
286-
def get(self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None) -> str: ...
299+
def get(self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None) -> str: ...
287300
@overload
288-
def get(self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T) -> str | _T: ...
301+
def get(
302+
self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T
303+
) -> str | _T: ...
289304

290305
if sys.version_info < (3, 12):
291306
class SafeConfigParser(ConfigParser): ... # deprecated alias
@@ -349,38 +364,38 @@ class Error(Exception):
349364
def __init__(self, msg: str = "") -> None: ...
350365

351366
class NoSectionError(Error):
352-
section: str
353-
def __init__(self, section: str) -> None: ...
367+
section: _SectionName
368+
def __init__(self, section: _SectionName) -> None: ...
354369

355370
class DuplicateSectionError(Error):
356-
section: str
371+
section: _SectionName
357372
source: str | None
358373
lineno: int | None
359-
def __init__(self, section: str, source: str | None = None, lineno: int | None = None) -> None: ...
374+
def __init__(self, section: _SectionName, source: str | None = None, lineno: int | None = None) -> None: ...
360375

361376
class DuplicateOptionError(Error):
362-
section: str
377+
section: _SectionName
363378
option: str
364379
source: str | None
365380
lineno: int | None
366-
def __init__(self, section: str, option: str, source: str | None = None, lineno: int | None = None) -> None: ...
381+
def __init__(self, section: _SectionName, option: str, source: str | None = None, lineno: int | None = None) -> None: ...
367382

368383
class NoOptionError(Error):
369-
section: str
384+
section: _SectionName
370385
option: str
371-
def __init__(self, option: str, section: str) -> None: ...
386+
def __init__(self, option: str, section: _SectionName) -> None: ...
372387

373388
class InterpolationError(Error):
374-
section: str
389+
section: _SectionName
375390
option: str
376-
def __init__(self, option: str, section: str, msg: str) -> None: ...
391+
def __init__(self, option: str, section: _SectionName, msg: str) -> None: ...
377392

378393
class InterpolationDepthError(InterpolationError):
379-
def __init__(self, option: str, section: str, rawval: object) -> None: ...
394+
def __init__(self, option: str, section: _SectionName, rawval: object) -> None: ...
380395

381396
class InterpolationMissingOptionError(InterpolationError):
382397
reference: str
383-
def __init__(self, option: str, section: str, rawval: object, reference: str) -> None: ...
398+
def __init__(self, option: str, section: _SectionName, rawval: object, reference: str) -> None: ...
384399

385400
class InterpolationSyntaxError(InterpolationError): ...
386401

@@ -403,9 +418,6 @@ class MissingSectionHeaderError(ParsingError):
403418
def __init__(self, filename: str, lineno: int, line: str) -> None: ...
404419

405420
if sys.version_info >= (3, 13):
406-
class _UNNAMED_SECTION: ...
407-
UNNAMED_SECTION: _UNNAMED_SECTION
408-
409421
class MultilineContinuationError(ParsingError):
410422
lineno: int
411423
line: str

0 commit comments

Comments
 (0)