Skip to content

Commit 8ff6ccd

Browse files
committed
Support configparser.UNNAMED_SECTION (python#13542)
UNNAMED_SECTION was added in Python 3.13 and can be used in place of any section name. It is not actually a string though, so it needs a separate type.
1 parent bef43fc commit 8ff6ccd

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

stdlib/configparser.pyi

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

80+
if sys.version_info >= (3, 13):
81+
_SectionName: TypeAlias = str | _UNNAMED_SECTION
82+
else:
83+
_SectionName: TypeAlias = str
84+
8085
_Section: TypeAlias = Mapping[str, str]
8186
_Parser: TypeAlias = MutableMapping[str, _Section]
8287
_ConverterCallback: TypeAlias = Callable[[str], Any]
@@ -87,17 +92,17 @@ DEFAULTSECT: Final = "DEFAULT"
8792
MAX_INTERPOLATION_DEPTH: Final = 10
8893

8994
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: ...
95+
def before_get(self, parser: _Parser, section: _SectionName, option: str, value: str, defaults: _Section) -> str: ...
96+
def before_set(self, parser: _Parser, section: _SectionName, option: str, value: str) -> str: ...
97+
def before_read(self, parser: _Parser, section: _SectionName, option: str, value: str) -> str: ...
98+
def before_write(self, parser: _Parser, section: _SectionName, option: str, value: str) -> str: ...
9499

95100
class BasicInterpolation(Interpolation): ...
96101
class ExtendedInterpolation(Interpolation): ...
97102

98103
if sys.version_info < (3, 13):
99104
class LegacyInterpolation(Interpolation):
100-
def before_get(self, parser: _Parser, section: str, option: str, value: str, vars: _Section) -> str: ...
105+
def before_get(self, parser: _Parser, section: _SectionName, option: str, value: str, vars: _Section) -> str: ...
101106

102107
class RawConfigParser(_Parser):
103108
_SECT_TMPL: ClassVar[str] # undocumented
@@ -220,11 +225,11 @@ class RawConfigParser(_Parser):
220225
def __iter__(self) -> Iterator[str]: ...
221226
def __contains__(self, key: object) -> bool: ...
222227
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: ...
228+
def sections(self) -> list[_SectionName]: ...
229+
def add_section(self, section: _SectionName) -> None: ...
230+
def has_section(self, section: _SectionName) -> bool: ...
231+
def options(self, section: _SectionName) -> list[str]: ...
232+
def has_option(self, section: _SectionName, option: str) -> bool: ...
228233
def read(self, filenames: StrOrBytesPath | Iterable[StrOrBytesPath], encoding: str | None = None) -> list[str]: ...
229234
def read_file(self, f: Iterable[str], source: str | None = None) -> None: ...
230235
def read_string(self, string: str, source: str = "<string>") -> None: ...
@@ -234,26 +239,26 @@ class RawConfigParser(_Parser):
234239
# These get* methods are partially applied (with the same names) in
235240
# SectionProxy; the stubs should be kept updated together
236241
@overload
237-
def getint(self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None) -> int: ...
242+
def getint(self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None) -> int: ...
238243
@overload
239244
def getint(
240-
self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T = ...
245+
self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T = ...
241246
) -> int | _T: ...
242247
@overload
243-
def getfloat(self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None) -> float: ...
248+
def getfloat(self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None) -> float: ...
244249
@overload
245250
def getfloat(
246-
self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T = ...
251+
self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T = ...
247252
) -> float | _T: ...
248253
@overload
249-
def getboolean(self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None) -> bool: ...
254+
def getboolean(self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None) -> bool: ...
250255
@overload
251256
def getboolean(
252-
self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T = ...
257+
self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T = ...
253258
) -> bool | _T: ...
254259
def _get_conv(
255260
self,
256-
section: str,
261+
section: _SectionName,
257262
option: str,
258263
conv: Callable[[str], _T],
259264
*,
@@ -263,29 +268,31 @@ class RawConfigParser(_Parser):
263268
) -> _T: ...
264269
# This is incompatible with MutableMapping so we ignore the type
265270
@overload # type: ignore[override]
266-
def get(self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None) -> str | MaybeNone: ...
271+
def get(self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None) -> str | MaybeNone: ...
267272
@overload
268273
def get(
269-
self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T
274+
self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T
270275
) -> str | _T | MaybeNone: ...
271276
@overload
272277
def items(self, *, raw: bool = False, vars: _Section | None = None) -> ItemsView[str, SectionProxy]: ...
273278
@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: ...
279+
def items(self, section: _SectionName, raw: bool = False, vars: _Section | None = None) -> list[tuple[str, str]]: ...
280+
def set(self, section: _SectionName, option: str, value: str | None = None) -> None: ...
276281
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: ...
282+
def remove_option(self, section: _SectionName, option: str) -> bool: ...
283+
def remove_section(self, section: _SectionName) -> bool: ...
279284
def optionxform(self, optionstr: str) -> str: ...
280285
@property
281286
def converters(self) -> ConverterMapping: ...
282287

283288
class ConfigParser(RawConfigParser):
284289
# This is incompatible with MutableMapping so we ignore the type
285290
@overload # type: ignore[override]
286-
def get(self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None) -> str: ...
291+
def get(self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None) -> str: ...
287292
@overload
288-
def get(self, section: str, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T) -> str | _T: ...
293+
def get(
294+
self, section: _SectionName, option: str, *, raw: bool = False, vars: _Section | None = None, fallback: _T
295+
) -> str | _T: ...
289296

290297
if sys.version_info < (3, 12):
291298
class SafeConfigParser(ConfigParser): ... # deprecated alias
@@ -349,38 +356,38 @@ class Error(Exception):
349356
def __init__(self, msg: str = "") -> None: ...
350357

351358
class NoSectionError(Error):
352-
section: str
353-
def __init__(self, section: str) -> None: ...
359+
section: _SectionName
360+
def __init__(self, section: _SectionName) -> None: ...
354361

355362
class DuplicateSectionError(Error):
356-
section: str
363+
section: _SectionName
357364
source: str | None
358365
lineno: int | None
359-
def __init__(self, section: str, source: str | None = None, lineno: int | None = None) -> None: ...
366+
def __init__(self, section: _SectionName, source: str | None = None, lineno: int | None = None) -> None: ...
360367

361368
class DuplicateOptionError(Error):
362-
section: str
369+
section: _SectionName
363370
option: str
364371
source: str | None
365372
lineno: int | None
366-
def __init__(self, section: str, option: str, source: str | None = None, lineno: int | None = None) -> None: ...
373+
def __init__(self, section: _SectionName, option: str, source: str | None = None, lineno: int | None = None) -> None: ...
367374

368375
class NoOptionError(Error):
369-
section: str
376+
section: _SectionName
370377
option: str
371-
def __init__(self, option: str, section: str) -> None: ...
378+
def __init__(self, option: str, section: _SectionName) -> None: ...
372379

373380
class InterpolationError(Error):
374-
section: str
381+
section: _SectionName
375382
option: str
376-
def __init__(self, option: str, section: str, msg: str) -> None: ...
383+
def __init__(self, option: str, section: _SectionName, msg: str) -> None: ...
377384

378385
class InterpolationDepthError(InterpolationError):
379-
def __init__(self, option: str, section: str, rawval: object) -> None: ...
386+
def __init__(self, option: str, section: _SectionName, rawval: object) -> None: ...
380387

381388
class InterpolationMissingOptionError(InterpolationError):
382389
reference: str
383-
def __init__(self, option: str, section: str, rawval: object, reference: str) -> None: ...
390+
def __init__(self, option: str, section: _SectionName, rawval: object, reference: str) -> None: ...
384391

385392
class InterpolationSyntaxError(InterpolationError): ...
386393

0 commit comments

Comments
 (0)