From 4849ddd74a2036ea29dd309d29c2466febc310f1 Mon Sep 17 00:00:00 2001 From: Jeff Dairiki Date: Wed, 7 Jun 2023 15:00:49 -0700 Subject: [PATCH] inifile: add missing attributes (#10273) And make many attributes read-only. The primary entry point to this module in `IniFile` (or its subclass `AppIniFile`). Since the config file is read (from `filename`) and parsed (using the `dialect`) in the `IniFile` constructor, modifying attributes like `IniFile.filename` or `IniFile.dialect` after construction is likely to cause problems. --- stubs/inifile/@tests/stubtest_allowlist.txt | 15 +++++++++ stubs/inifile/inifile.pyi | 36 +++++++++++++++------ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/stubs/inifile/@tests/stubtest_allowlist.txt b/stubs/inifile/@tests/stubtest_allowlist.txt index f56a2b2e9..8d212b367 100644 --- a/stubs/inifile/@tests/stubtest_allowlist.txt +++ b/stubs/inifile/@tests/stubtest_allowlist.txt @@ -6,3 +6,18 @@ inifile.iter_from_file inifile.iteritems inifile.reraise inifile.string_types + +# Attributes that should be treated as read-only and thus are annotated +# with @property +inifile.Dialect.ns_sep +inifile.Dialect.kv_sep +inifile.Dialect.quotes +inifile.Dialect.true +inifile.Dialect.false +inifile.Dialect.comments +inifile.Dialect.allow_escaping +inifile.Dialect.linesep +inifile.IniData.dialect +inifile.IniFile.filename +inifile.IniFile.encoding +inifile.IniFile.is_new diff --git a/stubs/inifile/inifile.pyi b/stubs/inifile/inifile.pyi index 743a1dc3d..ec8c93226 100644 --- a/stubs/inifile/inifile.pyi +++ b/stubs/inifile/inifile.pyi @@ -1,4 +1,4 @@ -from _typeshed import StrOrBytesPath, StrPath, SupportsKeysAndGetItem +from _typeshed import StrPath, SupportsKeysAndGetItem from collections.abc import Container, Iterable, Iterator, Mapping, MutableMapping, Sequence from typing import TypeVar, overload from typing_extensions import Literal, TypeAlias @@ -16,14 +16,6 @@ _Token: TypeAlias = ( def get_app_dir(app_name: str, roaming: bool = ..., force_posix: bool = ...) -> str: ... class Dialect: - ns_sep: str - kv_sep: str - quotes: Sequence[str] - true: Sequence[str] - false: Sequence[str] - comments: Container[str] - allow_escaping: bool - linesep: str | None def __init__( self, ns_sep: str = ..., @@ -35,6 +27,22 @@ class Dialect: allow_escaping: bool = ..., linesep: str | None = ..., ) -> None: ... + @property + def ns_sep(self) -> str: ... + @property + def kv_sep(self) -> str: ... + @property + def quotes(self) -> Sequence[str]: ... + @property + def true(self) -> Sequence[str]: ... + @property + def false(self) -> Sequence[str]: ... + @property + def comments(self) -> Container[str]: ... + @property + def allow_escaping(self) -> bool: ... + @property + def linesep(self) -> str | None: ... def get_actual_linesep(self) -> str: ... def get_strippable_lineseps(self) -> str: ... def kv_serialize(self, key, val: str | None) -> str | None: ... @@ -52,6 +60,8 @@ default_dialect: Dialect class IniData(MutableMapping[str, str]): def __init__(self, mapping: Mapping[str, str] | None = ..., dialect: Dialect | None = ...) -> None: ... @property + def dialect(self) -> Dialect: ... + @property def is_dirty(self) -> bool: ... def get_updated_lines(self, line_iter: Iterable[_Token] | None = ...) -> list[_Token]: ... def discard(self) -> None: ... @@ -99,7 +109,13 @@ class IniData(MutableMapping[str, str]): def __delitem__(self, name: str) -> None: ... class IniFile(IniData): - def __init__(self, filename: StrOrBytesPath | int, encoding: str | None = ..., dialect: Dialect | None = ...) -> None: ... + def __init__(self, filename: StrPath, encoding: str | None = ..., dialect: Dialect | None = ...) -> None: ... + @property + def filename(self) -> str: ... + @property + def encoding(self) -> str | None: ... + @property + def is_new(self) -> bool: ... def save(self, create_folder: bool = ...) -> None: ... class AppIniFile(IniFile):