@@ -22,11 +22,11 @@ class ConfigReader:
22
22
'{\n "session_name": "my session"\n}'
23
23
"""
24
24
25
- def __init__ (self , content : "RawConfigData" ):
25
+ def __init__ (self , content : "RawConfigData" ) -> None :
26
26
self .content = content
27
27
28
28
@staticmethod
29
- def _load (format : "FormatLiteral" , content : str ):
29
+ def _load (format : "FormatLiteral" , content : str ) -> t . Dict [ str , t . Any ] :
30
30
"""Load raw config data and directly return it.
31
31
32
32
>>> ConfigReader._load("json", '{ "session_name": "my session" }')
@@ -36,17 +36,20 @@ def _load(format: "FormatLiteral", content: str):
36
36
{'session_name': 'my session'}
37
37
"""
38
38
if format == "yaml" :
39
- return yaml .load (
40
- content ,
41
- Loader = yaml .SafeLoader ,
39
+ return t .cast (
40
+ t .Dict [str , t .Any ],
41
+ yaml .load (
42
+ content ,
43
+ Loader = yaml .SafeLoader ,
44
+ ),
42
45
)
43
46
elif format == "json" :
44
- return json .loads (content )
47
+ return t . cast ( t . Dict [ str , t . Any ], json .loads (content ) )
45
48
else :
46
49
raise NotImplementedError (f"{ format } not supported in configuration" )
47
50
48
51
@classmethod
49
- def load (cls , format : "FormatLiteral" , content : str ):
52
+ def load (cls , format : "FormatLiteral" , content : str ) -> "ConfigReader" :
50
53
"""Load raw config data into a ConfigReader instance (to dump later).
51
54
52
55
>>> cfg = ConfigReader.load("json", '{ "session_name": "my session" }')
@@ -69,7 +72,7 @@ def load(cls, format: "FormatLiteral", content: str):
69
72
)
70
73
71
74
@classmethod
72
- def _from_file (cls , path : pathlib .Path ):
75
+ def _from_file (cls , path : pathlib .Path ) -> t . Dict [ str , t . Any ] :
73
76
r"""Load data from file path directly to dictionary.
74
77
75
78
**YAML file**
@@ -102,7 +105,7 @@ def _from_file(cls, path: pathlib.Path):
102
105
content = open (path ).read ()
103
106
104
107
if path .suffix in [".yaml" , ".yml" ]:
105
- format : FormatLiteral = "yaml"
108
+ format : " FormatLiteral" = "yaml"
106
109
elif path .suffix == ".json" :
107
110
format = "json"
108
111
else :
@@ -114,7 +117,7 @@ def _from_file(cls, path: pathlib.Path):
114
117
)
115
118
116
119
@classmethod
117
- def from_file (cls , path : pathlib .Path ):
120
+ def from_file (cls , path : pathlib .Path ) -> "ConfigReader" :
118
121
r"""Load data from file path
119
122
120
123
**YAML file**
@@ -169,11 +172,14 @@ def _dump(
169
172
'{\n "session_name": "my session"\n}'
170
173
"""
171
174
if format == "yaml" :
172
- return yaml .dump (
173
- content ,
174
- indent = 2 ,
175
- default_flow_style = False ,
176
- Dumper = yaml .SafeDumper ,
175
+ return t .cast (
176
+ str ,
177
+ yaml .dump (
178
+ content ,
179
+ indent = 2 ,
180
+ default_flow_style = False ,
181
+ Dumper = yaml .SafeDumper ,
182
+ ),
177
183
)
178
184
elif format == "json" :
179
185
return json .dumps (
0 commit comments