22import os
33import unittest
44from collections .abc import Mapping , Sequence
5+ from typing import Any
56
67import 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):
503488class 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