Skip to content

Commit 89f3f7b

Browse files
committed
core/refactor[typing]: Replace Any in config reader
why: Make loader and duplicate-tracking types safer without changing behavior. what: - Use object-typed mappings for raw config data - Tighten duplicate tracking payload types
1 parent 6500898 commit 89f3f7b

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/vcspull/_internal/config_reader.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import yaml
99

1010
FormatLiteral = t.Literal["json", "yaml"]
11-
RawConfigData: t.TypeAlias = dict[str, t.Any]
11+
RawConfigData: t.TypeAlias = dict[str, object]
1212

1313

1414
class ConfigReader:
@@ -25,7 +25,7 @@ def __init__(self, content: RawConfigData) -> None:
2525
self.content = content
2626

2727
@staticmethod
28-
def _load(fmt: FormatLiteral, content: str) -> dict[str, t.Any]:
28+
def _load(fmt: FormatLiteral, content: str) -> dict[str, object]:
2929
"""Load raw config data and directly return it.
3030
3131
>>> ConfigReader._load("json", '{ "session_name": "my session" }')
@@ -36,14 +36,14 @@ def _load(fmt: FormatLiteral, content: str) -> dict[str, t.Any]:
3636
"""
3737
if fmt == "yaml":
3838
return t.cast(
39-
"dict[str, t.Any]",
39+
"dict[str, object]",
4040
yaml.load(
4141
content,
4242
Loader=yaml.SafeLoader,
4343
),
4444
)
4545
if fmt == "json":
46-
return t.cast("dict[str, t.Any]", json.loads(content))
46+
return t.cast("dict[str, object]", json.loads(content))
4747
msg = f"{fmt} not supported in configuration"
4848
raise NotImplementedError(msg)
4949

@@ -71,7 +71,7 @@ def load(cls, fmt: FormatLiteral, content: str) -> ConfigReader:
7171
)
7272

7373
@classmethod
74-
def _from_file(cls, path: pathlib.Path) -> dict[str, t.Any]:
74+
def _from_file(cls, path: pathlib.Path) -> dict[str, object]:
7575
r"""Load data from file path directly to dictionary.
7676
7777
**YAML file**
@@ -175,7 +175,7 @@ def _dump(
175175
fmt: FormatLiteral,
176176
content: RawConfigData,
177177
indent: int = 2,
178-
**kwargs: t.Any,
178+
**kwargs: object,
179179
) -> str:
180180
r"""Dump directly.
181181
@@ -200,7 +200,7 @@ def _dump(
200200
msg = f"{fmt} not supported in config"
201201
raise NotImplementedError(msg)
202202

203-
def dump(self, fmt: FormatLiteral, indent: int = 2, **kwargs: t.Any) -> str:
203+
def dump(self, fmt: FormatLiteral, indent: int = 2, **kwargs: object) -> str:
204204
r"""Dump via ConfigReader instance.
205205
206206
>>> cfg = ConfigReader({ "session_name": "my session" })
@@ -222,23 +222,23 @@ class _DuplicateTrackingSafeLoader(yaml.SafeLoader):
222222

223223
def __init__(self, stream: str) -> None:
224224
super().__init__(stream)
225-
self.top_level_key_values: dict[t.Any, list[t.Any]] = {}
225+
self.top_level_key_values: dict[object, list[object]] = {}
226226
self._mapping_depth = 0
227-
self.top_level_items: list[tuple[t.Any, t.Any]] = []
227+
self.top_level_items: list[tuple[object, object]] = []
228228

229229

230230
def _duplicate_tracking_construct_mapping(
231231
loader: _DuplicateTrackingSafeLoader,
232232
node: yaml.nodes.MappingNode,
233233
deep: bool = False,
234-
) -> dict[t.Any, t.Any]:
234+
) -> dict[object, object]:
235235
loader._mapping_depth += 1
236236
loader.flatten_mapping(node)
237-
mapping: dict[t.Any, t.Any] = {}
237+
mapping: dict[object, object] = {}
238238

239239
for key_node, value_node in node.value:
240240
construct = t.cast(
241-
"t.Callable[[yaml.nodes.Node], t.Any]",
241+
"t.Callable[[yaml.nodes.Node], object]",
242242
loader.construct_object,
243243
)
244244
key = construct(key_node)
@@ -268,28 +268,28 @@ def __init__(
268268
self,
269269
content: RawConfigData,
270270
*,
271-
duplicate_sections: dict[str, list[t.Any]] | None = None,
272-
top_level_items: list[tuple[str, t.Any]] | None = None,
271+
duplicate_sections: dict[str, list[object]] | None = None,
272+
top_level_items: list[tuple[str, object]] | None = None,
273273
) -> None:
274274
super().__init__(content)
275275
self._duplicate_sections = duplicate_sections or {}
276276
self._top_level_items = top_level_items or []
277277

278278
@property
279-
def duplicate_sections(self) -> dict[str, list[t.Any]]:
279+
def duplicate_sections(self) -> dict[str, list[object]]:
280280
"""Mapping of top-level keys to the list of duplicated values."""
281281
return self._duplicate_sections
282282

283283
@property
284-
def top_level_items(self) -> list[tuple[str, t.Any]]:
284+
def top_level_items(self) -> list[tuple[str, object]]:
285285
"""Ordered list of top-level items, including duplicates."""
286286
return copy.deepcopy(self._top_level_items)
287287

288288
@classmethod
289289
def _load_yaml_with_duplicates(
290290
cls,
291291
content: str,
292-
) -> tuple[dict[str, t.Any], dict[str, list[t.Any]], list[tuple[str, t.Any]]]:
292+
) -> tuple[dict[str, object], dict[str, list[object]], list[tuple[str, object]]]:
293293
loader = _DuplicateTrackingSafeLoader(content)
294294

295295
try:
@@ -299,12 +299,12 @@ def _load_yaml_with_duplicates(
299299
dispose()
300300

301301
if data is None:
302-
loaded: dict[str, t.Any] = {}
302+
loaded: dict[str, object] = {}
303303
else:
304304
if not isinstance(data, dict):
305305
msg = "Loaded configuration is not a mapping"
306306
raise TypeError(msg)
307-
loaded = t.cast("dict[str, t.Any]", data)
307+
loaded = t.cast("dict[str, object]", data)
308308

309309
duplicate_sections = {
310310
t.cast("str", key): values
@@ -323,7 +323,7 @@ def _load_yaml_with_duplicates(
323323
def _load_from_path(
324324
cls,
325325
path: pathlib.Path,
326-
) -> tuple[dict[str, t.Any], dict[str, list[t.Any]], list[tuple[str, t.Any]]]:
326+
) -> tuple[dict[str, object], dict[str, list[object]], list[tuple[str, object]]]:
327327
if path.suffix.lower() in {".yaml", ".yml"}:
328328
content = path.read_text(encoding="utf-8")
329329
return cls._load_yaml_with_duplicates(content)
@@ -340,14 +340,14 @@ def from_file(cls, path: pathlib.Path) -> DuplicateAwareConfigReader:
340340
)
341341

342342
@classmethod
343-
def _from_file(cls, path: pathlib.Path) -> dict[str, t.Any]:
343+
def _from_file(cls, path: pathlib.Path) -> dict[str, object]:
344344
content, _, _ = cls._load_from_path(path)
345345
return content
346346

347347
@classmethod
348348
def load_with_duplicates(
349349
cls,
350350
path: pathlib.Path,
351-
) -> tuple[dict[str, t.Any], dict[str, list[t.Any]], list[tuple[str, t.Any]]]:
351+
) -> tuple[dict[str, object], dict[str, list[object]], list[tuple[str, object]]]:
352352
reader = cls.from_file(path)
353353
return reader.content, reader.duplicate_sections, reader.top_level_items

0 commit comments

Comments
 (0)