Skip to content

Commit 9ca9be5

Browse files
committed
Fix types in test directory
1 parent 5f31b48 commit 9ca9be5

File tree

10 files changed

+85
-70
lines changed

10 files changed

+85
-70
lines changed

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ allow_untyped_calls = true
2424
# FIXME: Would be better to actually type the libraries (if under our control),
2525
# or write our own stubs. For now, silence errors
2626
strict = true
27+
28+
[mypy-test.*]
29+
allow_untyped_defs = true

test/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
from __future__ import annotations
2+
13
import os
24
import shutil
35
import tempfile
6+
from typing import TYPE_CHECKING, Any
47

58
import confuse
69

10+
if TYPE_CHECKING:
11+
from collections.abc import Mapping
12+
713

8-
def _root(*sources):
14+
def _root(*sources: Mapping[str, Any]) -> confuse.RootView:
915
return confuse.RootView([confuse.ConfigSource.of(s) for s in sources])
1016

1117

test/test_dump.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import textwrap
22
import unittest
3+
from collections import OrderedDict
34

45
import confuse
56

@@ -32,7 +33,7 @@ def test_dump_short_list(self):
3233
assert yaml == "foo: [bar, baz]"
3334

3435
def test_dump_ordered_dict(self):
35-
odict = confuse.OrderedDict()
36+
odict = OrderedDict()
3637
odict["foo"] = "bar"
3738
odict["bar"] = "baz"
3839
odict["baz"] = "qux"

test/test_env.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import os
22
import unittest
3+
from unittest.mock import patch
34

45
import pytest
56

67
import confuse
78

89
from . import _root
910

10-
ENVIRON = os.environ
11-
1211

1312
class EnvSourceTest(unittest.TestCase):
1413
def setUp(self):
15-
os.environ = {}
14+
self.env_patcher = patch.dict("os.environ", {})
15+
self.env_patcher.start()
1616

1717
def tearDown(self):
18-
os.environ = ENVIRON
18+
self.env_patcher.stop()
1919

2020
def test_prefix(self):
2121
os.environ["TEST_FOO"] = "a"
@@ -235,16 +235,17 @@ def test_parse_yaml_docs_false(self):
235235

236236
class ConfigEnvTest(unittest.TestCase):
237237
def setUp(self):
238+
self.env_patcher = patch.dict(
239+
"os.environ",
240+
{
241+
"TESTAPP_FOO": "a",
242+
"TESTAPP_BAR__NESTED": "b",
243+
"TESTAPP_BAZ_SEP_NESTED": "c",
244+
"MYAPP_QUX_SEP_NESTED": "d",
245+
},
246+
)
247+
self.env_patcher.start()
238248
self.config = confuse.Configuration("TestApp", read=False)
239-
os.environ = {
240-
"TESTAPP_FOO": "a",
241-
"TESTAPP_BAR__NESTED": "b",
242-
"TESTAPP_BAZ_SEP_NESTED": "c",
243-
"MYAPP_QUX_SEP_NESTED": "d",
244-
}
245-
246-
def tearDown(self):
247-
os.environ = ENVIRON
248249

249250
def test_defaults(self):
250251
self.config.set_env()

test/test_paths.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,18 @@
1010
import confuse
1111
import confuse.yaml_util
1212

13-
DEFAULT = [platform.system, os.environ, os.path]
14-
13+
DEFAULT = (platform.system, os.environ, os.path)
1514
SYSTEMS = {
16-
"Linux": [{"HOME": "/home/test", "XDG_CONFIG_HOME": "~/xdgconfig"}, posixpath],
17-
"Darwin": [{"HOME": "/Users/test"}, posixpath],
18-
"Windows": [
15+
"Linux": ({"HOME": "/home/test", "XDG_CONFIG_HOME": "~/xdgconfig"}, posixpath),
16+
"Darwin": ({"HOME": "/Users/test"}, posixpath),
17+
"Windows": (
1918
{
2019
"APPDATA": "~\\winconfig",
2120
"HOME": "C:\\Users\\test",
2221
"USERPROFILE": "C:\\Users\\test",
2322
},
2423
ntpath,
25-
],
24+
),
2625
}
2726

2827

@@ -48,10 +47,10 @@ class FakeSystem(unittest.TestCase):
4847
def setUp(self):
4948
super().setUp()
5049
self.os_path = os.path
51-
os.environ = {}
50+
os.environ = {} # type: ignore[assignment]
5251

5352
environ, os.path = SYSTEMS[self.SYS_NAME]
54-
os.environ.update(environ) # copy
53+
os.environ.update(environ)
5554
platform.system = lambda: self.SYS_NAME
5655

5756
def tearDown(self):
@@ -128,11 +127,11 @@ def test_fallback_dir(self):
128127
class ConfigFilenamesTest(unittest.TestCase):
129128
def setUp(self):
130129
self._old = os.path.isfile, confuse.yaml_util.load_yaml
131-
os.path.isfile = lambda x: True
130+
os.path.isfile = lambda x: True # type: ignore[assignment]
132131
confuse.yaml_util.load_yaml = lambda *args, **kwargs: {}
133132

134133
def tearDown(self):
135-
confuse.yaml_util.load_yaml, os.path.isfile = self._old
134+
os.path.isfile, confuse.yaml_util.load_yaml = self._old
136135

137136
def test_no_sources_when_files_missing(self):
138137
config = confuse.Configuration("myapp", read=False)
@@ -173,7 +172,7 @@ def test_env_var_missing(self):
173172
assert self.config.config_dir() != self.home
174173

175174

176-
@unittest.skipUnless(os.system == "Linux", "Linux-specific tests")
175+
@unittest.skipUnless(platform.system() == "Linux", "Linux-specific tests")
177176
class PrimaryConfigDirTest(FakeHome, FakeSystem):
178177
SYS_NAME = "Linux" # conversion from posix to nt is easy
179178

test/test_utils.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
from argparse import Namespace
33
from collections import OrderedDict
4+
from typing import Any
45

56
import pytest
67

@@ -20,15 +21,15 @@ def test_namespaces(self):
2021
assert 1 == result["another"]
2122

2223
def test_dot_sep_keys(self):
23-
config = {"foo.bar": 1}
24+
config: dict[str, Any] = {"foo.bar": 1}
2425
result = confuse.util.build_dict(config.copy())
2526
assert 1 == result["foo.bar"]
2627

2728
result = confuse.util.build_dict(config.copy(), sep=".")
2829
assert 1 == result["foo"]["bar"]
2930

3031
def test_dot_sep_keys_clobber(self):
31-
args = [("foo.bar", 1), ("foo.bar.zar", 2)]
32+
args: list[tuple[str, Any]] = [("foo.bar", 1), ("foo.bar.zar", 2)]
3233
config = OrderedDict(args)
3334
result = confuse.util.build_dict(config.copy(), sep=".")
3435
assert {"zar": 2} == result["foo"]["bar"]
@@ -42,15 +43,19 @@ def test_dot_sep_keys_clobber(self):
4243
assert 2 == result["foo"]["bar"]["zar"]
4344

4445
def test_dot_sep_keys_no_clobber(self):
45-
args = [("foo.bar", 1), ("foo.far", 2), ("foo.zar.dar", 4)]
46+
args: list[tuple[str, Any]] = [
47+
("foo.bar", 1),
48+
("foo.far", 2),
49+
("foo.zar.dar", 4),
50+
]
4651
config = OrderedDict(args)
4752
result = confuse.util.build_dict(config.copy(), sep=".")
4853
assert 1 == result["foo"]["bar"]
4954
assert 2 == result["foo"]["far"]
5055
assert 4 == result["foo"]["zar"]["dar"]
5156

5257
def test_adjacent_underscores_sep_keys(self):
53-
config = {"foo__bar_baz": 1}
58+
config: dict[str, Any] = {"foo__bar_baz": 1}
5459
result = confuse.util.build_dict(config.copy())
5560
assert 1 == result["foo__bar_baz"]
5661

test/test_valid.py

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import unittest
44
from collections.abc import Mapping, Sequence
5+
from typing import Any
56

67
import pytest
78

@@ -69,29 +70,13 @@ def test_validate_individual_value(self):
6970
assert valid == 5
7071

7172
def test_nested_dict_template(self):
72-
config = _root(
73-
{
74-
"foo": {"bar": 9},
75-
}
76-
)
77-
valid = config.get(
78-
{
79-
"foo": {"bar": confuse.Integer()},
80-
}
81-
)
73+
config = _root({"foo": {"bar": 9}})
74+
valid = config.get({"foo": {"bar": confuse.Integer()}})
8275
assert valid["foo"]["bar"] == 9
8376

8477
def test_nested_attribute_access(self):
85-
config = _root(
86-
{
87-
"foo": {"bar": 8},
88-
}
89-
)
90-
valid = config.get(
91-
{
92-
"foo": {"bar": confuse.Integer()},
93-
}
94-
)
78+
config = _root({"foo": {"bar": 8}})
79+
valid = config.get({"foo": {"bar": confuse.Integer()}})
9580
assert valid.foo.bar == 8
9681

9782

@@ -132,12 +117,12 @@ def test_nested_dict_as_template(self):
132117
assert typ.subtemplates["outer"].subtemplates["inner"].default == 2
133118

134119
def test_list_as_template(self):
135-
typ = confuse.as_template(list())
120+
typ: confuse.OneOf[Any] = confuse.as_template(list())
136121
assert isinstance(typ, confuse.OneOf)
137122
assert typ.default == confuse.REQUIRED
138123

139124
def test_set_as_template(self):
140-
typ = confuse.as_template(set())
125+
typ: confuse.Choice[Any] = confuse.as_template(set())
141126
assert isinstance(typ, confuse.Choice)
142127

143128
def test_enum_type_as_template(self):
@@ -275,7 +260,7 @@ def test_default_value(self):
275260

276261
def test_validate_good_choice_in_list(self):
277262
config = _root({"foo": 2})
278-
valid = config["foo"].get(
263+
valid: str | int = config["foo"].get(
279264
confuse.OneOf(
280265
[
281266
confuse.String(),
@@ -287,7 +272,7 @@ def test_validate_good_choice_in_list(self):
287272

288273
def test_validate_first_good_choice_in_list(self):
289274
config = _root({"foo": 3.14})
290-
valid = config["foo"].get(
275+
valid: str | int = config["foo"].get(
291276
confuse.OneOf(
292277
[
293278
confuse.Integer(),
@@ -315,7 +300,7 @@ class BadTemplate:
315300

316301
config = _root({})
317302
with pytest.raises(ValueError, match="cannot convert to template"):
318-
config.get(confuse.OneOf([BadTemplate()]))
303+
config.get(confuse.OneOf([BadTemplate()])) # type: ignore[list-item]
319304
del BadTemplate
320305

321306

@@ -416,7 +401,7 @@ def test_filename_with_non_file_source(self):
416401
def test_filename_with_file_source(self):
417402
source = confuse.ConfigSource({"foo": "foo/bar"}, filename="/baz/config.yaml")
418403
config = _root(source)
419-
config.config_dir = lambda: "/config/path"
404+
config.config_dir = lambda: "/config/path" # type: ignore[attr-defined]
420405
valid = config["foo"].get(confuse.Filename())
421406
assert valid == os.path.realpath("/config/path/foo/bar")
422407

@@ -425,7 +410,7 @@ def test_filename_with_default_source(self):
425410
{"foo": "foo/bar"}, filename="/baz/config.yaml", default=True
426411
)
427412
config = _root(source)
428-
config.config_dir = lambda: "/config/path"
413+
config.config_dir = lambda: "/config/path" # type: ignore[attr-defined]
429414
valid = config["foo"].get(confuse.Filename())
430415
assert valid == os.path.realpath("/config/path/foo/bar")
431416

@@ -434,28 +419,28 @@ def test_filename_use_config_source_dir(self):
434419
{"foo": "foo/bar"}, filename="/baz/config.yaml", base_for_paths=True
435420
)
436421
config = _root(source)
437-
config.config_dir = lambda: "/config/path"
422+
config.config_dir = lambda: "/config/path" # type: ignore[attr-defined]
438423
valid = config["foo"].get(confuse.Filename())
439424
assert valid == os.path.realpath("/baz/foo/bar")
440425

441426
def test_filename_in_source_dir(self):
442427
source = confuse.ConfigSource({"foo": "foo/bar"}, filename="/baz/config.yaml")
443428
config = _root(source)
444-
config.config_dir = lambda: "/config/path"
429+
config.config_dir = lambda: "/config/path" # type: ignore[attr-defined]
445430
valid = config["foo"].get(confuse.Filename(in_source_dir=True))
446431
assert valid == os.path.realpath("/baz/foo/bar")
447432

448433
def test_filename_in_source_dir_overrides_in_app_dir(self):
449434
source = confuse.ConfigSource({"foo": "foo/bar"}, filename="/baz/config.yaml")
450435
config = _root(source)
451-
config.config_dir = lambda: "/config/path"
436+
config.config_dir = lambda: "/config/path" # type: ignore[attr-defined]
452437
valid = config["foo"].get(confuse.Filename(in_source_dir=True, in_app_dir=True))
453438
assert valid == os.path.realpath("/baz/foo/bar")
454439

455440
def test_filename_in_app_dir_non_file_source(self):
456441
source = confuse.ConfigSource({"foo": "foo/bar"})
457442
config = _root(source)
458-
config.config_dir = lambda: "/config/path"
443+
config.config_dir = lambda: "/config/path" # type: ignore[attr-defined]
459444
valid = config["foo"].get(confuse.Filename(in_app_dir=True))
460445
assert valid == os.path.realpath("/config/path/foo/bar")
461446

@@ -464,7 +449,7 @@ def test_filename_in_app_dir_overrides_config_source_dir(self):
464449
{"foo": "foo/bar"}, filename="/baz/config.yaml", base_for_paths=True
465450
)
466451
config = _root(source)
467-
config.config_dir = lambda: "/config/path"
452+
config.config_dir = lambda: "/config/path" # type: ignore[attr-defined]
468453
valid = config["foo"].get(confuse.Filename(in_app_dir=True))
469454
assert valid == os.path.realpath("/config/path/foo/bar")
470455

@@ -503,7 +488,7 @@ def test_missing_required_value(self):
503488
class BaseTemplateTest(unittest.TestCase):
504489
def test_base_template_accepts_any_value(self):
505490
config = _root({"foo": 4.2})
506-
valid = config["foo"].get(confuse.Template())
491+
valid: float = config["foo"].get(confuse.Template())
507492
assert valid == 4.2
508493

509494
def test_base_template_required(self):
@@ -576,7 +561,9 @@ def test_dict_dict(self):
576561
config = _root(
577562
{"foo": {"first": {"bar": 1, "baz": 2}, "second": {"bar": 3, "baz": 4}}}
578563
)
579-
valid = config["foo"].get(confuse.MappingValues({"bar": int, "baz": int}))
564+
valid: dict[str, dict[str, int]] = config["foo"].get(
565+
confuse.MappingValues({"bar": int, "baz": int})
566+
)
580567
assert valid == {"first": {"bar": 1, "baz": 2}, "second": {"bar": 3, "baz": 4}}
581568

582569
def test_invalid_item(self):
@@ -656,6 +643,7 @@ def test_optional_mapping_template_valid(self):
656643
config = _root({"foo": {"bar": 5, "baz": "bak"}})
657644
template = {"bar": confuse.Integer(), "baz": confuse.String()}
658645
valid = config.get({"foo": confuse.Optional(template)})
646+
assert valid["foo"]
659647
assert valid["foo"]["bar"] == 5
660648
assert valid["foo"]["baz"] == "bak"
661649

0 commit comments

Comments
 (0)