@@ -22,11 +22,11 @@ class ConfigReader:
2222 '{\n "session_name": "my session"\n}'
2323 """
2424
25- def __init__ (self , content : "RawConfigData" ):
25+ def __init__ (self , content : "RawConfigData" ) -> None :
2626 self .content = content
2727
2828 @staticmethod
29- def _load (format : "FormatLiteral" , content : str ):
29+ def _load (format : "FormatLiteral" , content : str ) -> t . Dict [ str , t . Any ] :
3030 """Load raw config data and directly return it.
3131
3232 >>> ConfigReader._load("json", '{ "session_name": "my session" }')
@@ -36,17 +36,20 @@ def _load(format: "FormatLiteral", content: str):
3636 {'session_name': 'my session'}
3737 """
3838 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+ ),
4245 )
4346 elif format == "json" :
44- return json .loads (content )
47+ return t . cast ( t . Dict [ str , t . Any ], json .loads (content ) )
4548 else :
4649 raise NotImplementedError (f"{ format } not supported in configuration" )
4750
4851 @classmethod
49- def load (cls , format : "FormatLiteral" , content : str ):
52+ def load (cls , format : "FormatLiteral" , content : str ) -> "ConfigReader" :
5053 """Load raw config data into a ConfigReader instance (to dump later).
5154
5255 >>> cfg = ConfigReader.load("json", '{ "session_name": "my session" }')
@@ -69,7 +72,7 @@ def load(cls, format: "FormatLiteral", content: str):
6972 )
7073
7174 @classmethod
72- def _from_file (cls , path : pathlib .Path ):
75+ def _from_file (cls , path : pathlib .Path ) -> t . Dict [ str , t . Any ] :
7376 r"""Load data from file path directly to dictionary.
7477
7578 **YAML file**
@@ -102,7 +105,7 @@ def _from_file(cls, path: pathlib.Path):
102105 content = open (path ).read ()
103106
104107 if path .suffix in [".yaml" , ".yml" ]:
105- format : FormatLiteral = "yaml"
108+ format : " FormatLiteral" = "yaml"
106109 elif path .suffix == ".json" :
107110 format = "json"
108111 else :
@@ -114,7 +117,7 @@ def _from_file(cls, path: pathlib.Path):
114117 )
115118
116119 @classmethod
117- def from_file (cls , path : pathlib .Path ):
120+ def from_file (cls , path : pathlib .Path ) -> "ConfigReader" :
118121 r"""Load data from file path
119122
120123 **YAML file**
@@ -169,11 +172,14 @@ def _dump(
169172 '{\n "session_name": "my session"\n}'
170173 """
171174 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+ ),
177183 )
178184 elif format == "json" :
179185 return json .dumps (
0 commit comments