Skip to content

Commit 32778bb

Browse files
committed
Switch to ruff lint checker
1 parent 3cfbfcd commit 32778bb

File tree

10 files changed

+180
-209
lines changed

10 files changed

+180
-209
lines changed

.github/workflows/run_tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ on:
66
- main
77
pull_request:
88

9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: true
12+
913
env:
1014
POETRY_VERSION: 1.5.1
1115

config/__init__.py

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def config(
3131
interpolate: InterpolateType = False,
3232
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
3333
) -> ConfigurationSet:
34-
"""
35-
Create a :class:`ConfigurationSet` instance from an iterable of configs.
34+
"""Create a :class:`ConfigurationSet` instance from an iterable of configs.
3635
3736
:param configs: iterable of configurations
3837
:param prefix: prefix to filter environment variables with
@@ -88,7 +87,7 @@ def config(
8887
if not isinstance(config_, (tuple, list)) or len(config_) == 0:
8988
raise ValueError(
9089
"configuration parameters must be a list of dictionaries,"
91-
" strings, or non-empty tuples/lists"
90+
" strings, or non-empty tuples/lists",
9291
)
9392
type_ = config_[0]
9493
if type_ == "dict":
@@ -102,62 +101,66 @@ def config(
102101
params = list(config_[1:]) + default_args[(len(config_) - 2) :]
103102
instances.append(
104103
config_from_python(
105-
*params, **default_kwargs, ignore_missing_paths=ignore_missing_paths
106-
)
104+
*params,
105+
**default_kwargs,
106+
ignore_missing_paths=ignore_missing_paths,
107+
),
107108
)
108109
elif type_ == "json":
109110
instances.append(
110111
config_from_json(
111112
*config_[1:],
112113
**default_kwargs,
113114
ignore_missing_paths=ignore_missing_paths,
114-
)
115+
),
115116
)
116117
elif yaml and type_ == "yaml":
117118
instances.append(
118119
config_from_yaml(
119120
*config_[1:],
120121
**default_kwargs,
121122
ignore_missing_paths=ignore_missing_paths,
122-
)
123+
),
123124
)
124125
elif toml and type_ == "toml":
125126
instances.append(
126127
config_from_toml(
127128
*config_[1:],
128129
**default_kwargs,
129130
ignore_missing_paths=ignore_missing_paths,
130-
)
131+
),
131132
)
132133
elif type_ == "ini":
133134
instances.append(
134135
config_from_ini(
135136
*config_[1:],
136137
**default_kwargs,
137138
ignore_missing_paths=ignore_missing_paths,
138-
)
139+
),
139140
)
140141
elif type_ == "dotenv":
141142
instances.append(
142143
config_from_dotenv(
143144
*config_[1:],
144145
**default_kwargs,
145146
ignore_missing_paths=ignore_missing_paths,
146-
)
147+
),
147148
)
148149
elif type_ == "path":
149150
instances.append(
150151
config_from_path(
151152
*config_[1:],
152153
**default_kwargs,
153154
ignore_missing_paths=ignore_missing_paths,
154-
)
155+
),
155156
)
156157
else:
157158
raise ValueError(f'Unknown configuration type "{type_}"')
158159

159160
return ConfigurationSet(
160-
*instances, interpolate=interpolate, interpolate_type=interpolate_type
161+
*instances,
162+
interpolate=interpolate,
163+
interpolate_type=interpolate_type,
161164
)
162165

163166

@@ -173,8 +176,7 @@ def __init__(
173176
interpolate: InterpolateType = False,
174177
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
175178
):
176-
"""
177-
Constructor.
179+
"""Class Constructor.
178180
179181
:param prefix: prefix to filter environment variables with
180182
:param separator: separator to replace by dots
@@ -215,8 +217,7 @@ def config_from_env(
215217
interpolate: InterpolateType = False,
216218
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
217219
) -> Configuration:
218-
"""
219-
Create a :class:`EnvConfiguration` instance from environment variables.
220+
"""Create a :class:`EnvConfiguration` instance from environment variables.
220221
221222
:param prefix: prefix to filter environment variables with
222223
:param separator: separator to replace by dots
@@ -246,8 +247,7 @@ def __init__(
246247
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
247248
ignore_missing_paths: bool = False,
248249
):
249-
"""
250-
Constructor.
250+
"""Class Constructor.
251251
252252
:param path: path to read from
253253
:param remove_level: how many levels to remove from the resulting config
@@ -278,7 +278,7 @@ def reload(self) -> None:
278278
".".join(
279279
(x[0].split("/") + [y])[
280280
(dotted_path_levels + self._remove_level) :
281-
]
281+
],
282282
),
283283
)
284284
for x in os.walk(path)
@@ -288,7 +288,8 @@ def reload(self) -> None:
288288

289289
result = {}
290290
for filename, key in files_keys:
291-
result[key] = open(filename).read()
291+
with open(filename) as f:
292+
result[key] = f.read()
292293
except FileNotFoundError:
293294
if self._ignore_missing_paths:
294295
result = {}
@@ -311,8 +312,7 @@ def config_from_path(
311312
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
312313
ignore_missing_paths: bool = False,
313314
) -> Configuration:
314-
"""
315-
Create a :class:`Configuration` instance from filesystem path.
315+
"""Create a :class:`Configuration` instance from filesystem path.
316316
317317
:param path: path to read from
318318
:param remove_level: how many levels to remove from the resulting config
@@ -343,8 +343,7 @@ def __init__(
343343
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
344344
ignore_missing_paths: bool = False,
345345
):
346-
"""
347-
Constructor.
346+
"""Class Constructor.
348347
349348
:param data: path to a config file, or its contents
350349
:param read_from_file: whether to read from a file path or to interpret
@@ -397,7 +396,8 @@ def _reload(
397396
"""Reload the JSON data."""
398397
if read_from_file:
399398
if isinstance(data, str):
400-
result = json.load(open(data, "rt"))
399+
with open(data, "rt") as f:
400+
result = json.load(f)
401401
else:
402402
result = json.load(data)
403403
else:
@@ -414,8 +414,7 @@ def config_from_json(
414414
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
415415
ignore_missing_paths: bool = False,
416416
) -> Configuration:
417-
"""
418-
Create a :class:`Configuration` instance from a JSON file.
417+
"""Create a :class:`Configuration` instance from a JSON file.
419418
420419
:param data: path to a JSON file or contents
421420
:param read_from_file: whether to read from a file path or to interpret
@@ -449,6 +448,7 @@ def __init__(
449448
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
450449
ignore_missing_paths: bool = False,
451450
):
451+
"""Class Constructor."""
452452
self._section_prefix = section_prefix
453453
super().__init__(
454454
data=data,
@@ -471,7 +471,8 @@ def optionxform(self, optionstr: str) -> str:
471471

472472
if read_from_file:
473473
if isinstance(data, str):
474-
data = open(data, "rt").read()
474+
with open(data, "rt") as f:
475+
data = f.read()
475476
else:
476477
data = data.read()
477478
data = cast(str, data)
@@ -496,8 +497,7 @@ def config_from_ini(
496497
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
497498
ignore_missing_paths: bool = False,
498499
) -> Configuration:
499-
"""
500-
Create a :class:`Configuration` instance from an INI file.
500+
"""Create a :class:`Configuration` instance from an INI file.
501501
502502
:param data: path to an INI file or contents
503503
:param read_from_file: whether to read from a file path or to interpret
@@ -533,6 +533,7 @@ def __init__(
533533
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
534534
ignore_missing_paths: bool = False,
535535
):
536+
"""Class Constructor."""
536537
self._prefix = prefix
537538
self._separator = separator
538539
super().__init__(
@@ -552,7 +553,8 @@ def _reload(
552553
"""Reload the .env data."""
553554
if read_from_file:
554555
if isinstance(data, str):
555-
data = open(data, "rt").read()
556+
with open(data, "rt") as f:
557+
data = f.read()
556558
else:
557559
data = data.read()
558560
data = cast(str, data)
@@ -568,8 +570,6 @@ def _reload(
568570
if k.startswith(self._prefix)
569571
}
570572

571-
print(self._prefix, self._separator, result)
572-
573573
self._config = self._flatten_dict(result)
574574

575575

@@ -584,8 +584,7 @@ def config_from_dotenv(
584584
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
585585
ignore_missing_paths: bool = False,
586586
) -> Configuration:
587-
"""
588-
Create a :class:`Configuration` instance from a .env type file.
587+
"""Create a :class:`Configuration` instance from a .env type file.
589588
590589
:param data: path to a .env type file or contents
591590
:param read_from_file: whether to read from a file path or to interpret
@@ -621,8 +620,7 @@ def __init__(
621620
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
622621
ignore_missing_paths: bool = False,
623622
):
624-
"""
625-
Constructor.
623+
"""Class Constructor.
626624
627625
:param module: a module or path string
628626
:param prefix: prefix to use to filter object names
@@ -696,8 +694,7 @@ def config_from_python(
696694
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
697695
ignore_missing_paths: bool = False,
698696
) -> Configuration:
699-
"""
700-
Create a :class:`Configuration` instance from the objects in a Python module.
697+
"""Create a :class:`Configuration` instance from the objects in a Python module.
701698
702699
:param module: a module or path string
703700
:param prefix: prefix to use to filter object names
@@ -724,8 +721,7 @@ def config_from_dict(
724721
interpolate: InterpolateType = False,
725722
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
726723
) -> Configuration:
727-
"""
728-
Create a :class:`Configuration` instance from a dictionary.
724+
"""Create a :class:`Configuration` instance from a dictionary.
729725
730726
:param data: dictionary with string keys
731727
:param lowercase_keys: whether to convert every key to lower case.
@@ -741,10 +737,11 @@ def config_from_dict(
741737

742738

743739
def create_path_from_config(
744-
path: str, cfg: Configuration, remove_level: int = 1
740+
path: str,
741+
cfg: Configuration,
742+
remove_level: int = 1,
745743
) -> Configuration:
746-
"""
747-
Output a path configuration from a :class:`Configuration` instance.
744+
"""Output a path configuration from a :class:`Configuration` instance.
748745
749746
:param path: path to create the config files in
750747
:param cfg: :class:`Configuration` instance
@@ -776,9 +773,10 @@ def __init__(
776773
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
777774
ignore_missing_paths: bool = False,
778775
):
779-
if yaml is None:
776+
"""Class Constructor."""
777+
if yaml is None: # pragma: no cover
780778
raise ImportError(
781-
"Dependency <yaml> is not found, but required by this class."
779+
"Dependency <yaml> is not found, but required by this class.",
782780
)
783781
super().__init__(
784782
data=data,
@@ -792,7 +790,8 @@ def __init__(
792790
def _reload(self, data: Union[str, TextIO], read_from_file: bool = False) -> None:
793791
"""Reload the YAML data."""
794792
if read_from_file and isinstance(data, str):
795-
loaded = yaml.load(open(data, "rt"), Loader=yaml.FullLoader)
793+
with open(data, "rt") as f:
794+
loaded = yaml.load(f, Loader=yaml.FullLoader)
796795
else:
797796
loaded = yaml.load(data, Loader=yaml.FullLoader)
798797
if not isinstance(loaded, Mapping):
@@ -809,8 +808,7 @@ def config_from_yaml(
809808
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
810809
ignore_missing_paths: bool = False,
811810
) -> Configuration:
812-
"""
813-
Return a Configuration instance from YAML files.
811+
"""Return a Configuration instance from YAML files.
814812
815813
:param data: string or file
816814
:param read_from_file: whether `data` is a file or a YAML formatted string
@@ -843,9 +841,10 @@ def __init__(
843841
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
844842
ignore_missing_paths: bool = False,
845843
):
846-
if toml is None:
844+
"""Class Constructor."""
845+
if toml is None: # pragma: no cover
847846
raise ImportError(
848-
"Dependency <toml> is not found, but required by this class."
847+
"Dependency <toml> is not found, but required by this class.",
849848
)
850849

851850
self._section_prefix = section_prefix
@@ -862,7 +861,8 @@ def _reload(self, data: Union[str, TextIO], read_from_file: bool = False) -> Non
862861
"""Reload the TOML data."""
863862
if read_from_file:
864863
if isinstance(data, str):
865-
loaded = toml.load(open(data, "rt"))
864+
with open(data, "rt") as f:
865+
loaded = toml.load(f)
866866
else:
867867
loaded = toml.load(data)
868868
else:
@@ -889,8 +889,7 @@ def config_from_toml(
889889
interpolate_type: InterpolateEnumType = InterpolateEnumType.STANDARD,
890890
ignore_missing_paths: bool = False,
891891
) -> Configuration:
892-
"""
893-
Return a Configuration instance from TOML files.
892+
"""Return a Configuration instance from TOML files.
894893
895894
:param data: string or file
896895
:param read_from_file: whether `data` is a file or a TOML formatted string

0 commit comments

Comments
 (0)