@@ -39,7 +39,7 @@ def __init__(
3939 imageprofile_name : str ,
4040 imageprofile : ImageProfile | None = None ,
4141 bands : list [str ] | None = None ,
42- ):
42+ ) -> None :
4343 """Constructor for ImageConfig.
4444
4545 Args:
@@ -62,9 +62,9 @@ def __init__(
6262def read_config (
6363 config_paths : list [Path ] | Path | None ,
6464 default_basedir : Path | None = None ,
65- overrules : list [str ] = [] ,
65+ overrules : list [str ] | None = None ,
6666 preload_defaults : bool = True ,
67- ):
67+ ) -> None :
6868 """Read cropclassification configuration file(s).
6969
7070 Args:
@@ -102,10 +102,13 @@ def read_config(
102102 if not config_path .exists ():
103103 raise ValueError (f"Config file doesn't exist: { config_path } " )
104104
105+ if overrules is None :
106+ overrules = []
107+
105108 # If there are overrules, write them to a temporary configuration file.
106- global config_overrules
109+ global config_overrules # noqa: PLW0603
107110 config_overrules = overrules
108- global config_overrules_path
111+ global config_overrules_path # noqa: PLW0603
109112 config_overrules_path = None
110113 if len (config_overrules ) > 0 :
111114 tmp_dir = Path (tempfile .gettempdir ())
@@ -131,12 +134,12 @@ def read_config(
131134 overrules_parser [section ][parameter ] = value
132135
133136 # Write to temp file and add file to config_paths
134- with open (config_overrules_path , "w" ) as overrules_file :
137+ with config_overrules_path . open ("w" ) as overrules_file :
135138 overrules_parser .write (overrules_file )
136139 config_paths .append (config_overrules_path )
137140
138141 # Read and parse the config files
139- global config
142+ global config # noqa: PLW0603
140143 config = configparser .ConfigParser (
141144 interpolation = configparser .ExtendedInterpolation (),
142145 converters = {
@@ -195,40 +198,40 @@ def read_config(
195198 tmp_dir = tmp_dir_str
196199 )
197200
198- global config_paths_used
201+ global config_paths_used # noqa: PLW0603
199202 config_paths_used = config_paths
200203
201204 # Now set global variables to each section as shortcuts
202- global general
205+ global general # noqa: PLW0603
203206 general = config ["general" ]
204- global calc_timeseries_params
207+ global calc_timeseries_params # noqa: PLW0603
205208 calc_timeseries_params = config ["calc_timeseries_params" ]
206- global calc_marker_params
209+ global calc_marker_params # noqa: PLW0603
207210 calc_marker_params = config ["calc_marker_params" ]
208- global calc_periodic_mosaic_params
211+ global calc_periodic_mosaic_params # noqa: PLW0603
209212 if "calc_periodic_mosaic_params" in config :
210213 calc_periodic_mosaic_params = config ["calc_periodic_mosaic_params" ]
211214 else :
212215 calc_periodic_mosaic_params = None
213- global roi
216+ global roi # noqa: PLW0603
214217 roi = config ["roi" ]
215- global period
218+ global period # noqa: PLW0603
216219 period = config ["period" ]
217- global images
220+ global images # noqa: PLW0603
218221 images = config ["images" ]
219- global marker
222+ global marker # noqa: PLW0603
220223 marker = config ["marker" ]
221- global timeseries
224+ global timeseries # noqa: PLW0603
222225 timeseries = config ["timeseries" ]
223- global preprocess
226+ global preprocess # noqa: PLW0603
224227 preprocess = config ["preprocess" ]
225- global classifier
228+ global classifier # noqa: PLW0603
226229 classifier = config ["classifier" ]
227- global postprocess
230+ global postprocess # noqa: PLW0603
228231 postprocess = config ["postprocess" ]
229- global columns
232+ global columns # noqa: PLW0603
230233 columns = config ["columns" ]
231- global paths
234+ global paths # noqa: PLW0603
232235 paths = config ["paths" ]
233236
234237 # Check some parameters that should be overriden to have a valid config
@@ -239,7 +242,7 @@ def read_config(
239242 raise ValueError ("paths.images_periodic_dir must be overridden" )
240243
241244 # Load image profiles
242- global image_profiles
245+ global image_profiles # noqa: PLW0603
243246 image_profiles_config_filepath = paths .getpath ("image_profiles_config_filepath" )
244247 if image_profiles_config_filepath is not None :
245248 image_profiles = _get_image_profiles (
@@ -250,12 +253,12 @@ def read_config(
250253 image_profiles = {}
251254
252255
253- def parse_image_config (input ) -> dict [str , ImageConfig ]:
256+ def parse_image_config (image_config : str ) -> dict [str , ImageConfig ]:
254257 """Parses the json input to a dictionary of ImageConfig objects."""
255258 result = None
256259 imageconfig_parsed = None
257260 try :
258- imageconfig_parsed = json .loads (input )
261+ imageconfig_parsed = json .loads (image_config )
259262 except Exception :
260263 pass
261264
@@ -281,7 +284,7 @@ def parse_image_config(input) -> dict[str, ImageConfig]:
281284 )
282285 else :
283286 # It was no json object, so it must be a list
284- result = {i .strip (): ImageConfig (i .strip ()) for i in input .split ("," )}
287+ result = {i .strip (): ImageConfig (i .strip ()) for i in image_config .split ("," )}
285288
286289 return result
287290
@@ -330,7 +333,7 @@ def _get_image_profiles(image_profiles_path: Path) -> dict[str, ImageProfile]:
330333 return profiles
331334
332335
333- def _validate_image_profiles (profiles : dict [str , ImageProfile ]):
336+ def _validate_image_profiles (profiles : dict [str , ImageProfile ]) -> None :
334337 # Check that all base_image_profile s are actually existing image profiles.
335338 for profile in list (profiles ):
336339 base_image_profile = profiles [profile ].base_imageprofile
@@ -340,7 +343,7 @@ def _validate_image_profiles(profiles: dict[str, ImageProfile]):
340343 )
341344
342345
343- def pformat_config ():
346+ def pformat_config () -> str :
344347 """Formats the config as a pretty string."""
345348 message = (
346349 f"Config files used: { pprint .pformat (config_paths_used )} \n "
@@ -351,13 +354,13 @@ def pformat_config():
351354 return message
352355
353356
354- def as_dict ():
357+ def as_dict () -> dict [ str , Any ] :
355358 """Converts the config objects into a dictionary.
356359
357360 The resulting dictionary has sections as keys which point to a dict of the
358361 sections options as key => value pairs.
359362 """
360- the_dict = {}
363+ the_dict : dict [ str , dict [ str , Any ]] = {}
361364 for section in config .sections ():
362365 the_dict [section ] = {}
363366 for key , val in config .items (section ):
0 commit comments