1
1
"""Configuration parser for YAML and JSON files."""
2
2
3
+ from __future__ import annotations
4
+
3
5
import json
4
6
import pathlib
5
7
import typing as t
@@ -24,11 +26,11 @@ class ConfigReader:
24
26
'{\n "session_name": "my session"\n}'
25
27
"""
26
28
27
- def __init__ (self , content : " RawConfigData" ) -> None :
29
+ def __init__ (self , content : RawConfigData ) -> None :
28
30
self .content = content
29
31
30
32
@staticmethod
31
- def _load (fmt : " FormatLiteral" , content : str ) -> dict [str , t .Any ]:
33
+ def _load (fmt : FormatLiteral , content : str ) -> dict [str , t .Any ]:
32
34
"""Load raw config data and directly return it.
33
35
34
36
>>> ConfigReader._load("json", '{ "session_name": "my session" }')
@@ -51,7 +53,7 @@ def _load(fmt: "FormatLiteral", content: str) -> dict[str, t.Any]:
51
53
raise NotImplementedError (msg )
52
54
53
55
@classmethod
54
- def load (cls , fmt : " FormatLiteral" , content : str ) -> " ConfigReader" :
56
+ def load (cls , fmt : FormatLiteral , content : str ) -> ConfigReader :
55
57
"""Load raw config data into a ConfigReader instance (to dump later).
56
58
57
59
>>> cfg = ConfigReader.load("json", '{ "session_name": "my session" }')
@@ -120,7 +122,7 @@ def _from_file(cls, path: pathlib.Path) -> dict[str, t.Any]:
120
122
)
121
123
122
124
@classmethod
123
- def from_file (cls , path : pathlib .Path ) -> " ConfigReader" :
125
+ def from_file (cls , path : pathlib .Path ) -> ConfigReader :
124
126
r"""Load data from file path.
125
127
126
128
**YAML file**
@@ -161,8 +163,8 @@ def from_file(cls, path: pathlib.Path) -> "ConfigReader":
161
163
162
164
@staticmethod
163
165
def _dump (
164
- fmt : " FormatLiteral" ,
165
- content : " RawConfigData" ,
166
+ fmt : FormatLiteral ,
167
+ content : RawConfigData ,
166
168
indent : int = 2 ,
167
169
** kwargs : t .Any ,
168
170
) -> str :
@@ -189,7 +191,7 @@ def _dump(
189
191
msg = f"{ fmt } not supported in config"
190
192
raise NotImplementedError (msg )
191
193
192
- def dump (self , fmt : " FormatLiteral" , indent : int = 2 , ** kwargs : t .Any ) -> str :
194
+ def dump (self , fmt : FormatLiteral , indent : int = 2 , ** kwargs : t .Any ) -> str :
193
195
r"""Dump via ConfigReader instance.
194
196
195
197
>>> cfg = ConfigReader({ "session_name": "my session" })
0 commit comments