Skip to content

Commit 4994ed2

Browse files
Fix mypy type errors by using Mapping instead of Dict (#130)
Changed VariableValueDict to VariableValueMapping and updated type from Dict to Mapping to properly represent immutable type annotations. Fixed _merge function to use dict() constructor instead of .copy() since Mapping types don't have a copy method.
1 parent 085490f commit 4994ed2

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

tests/test_from_fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def fixture_file_path(filename: str) -> str:
1111
return os.path.join(absolute_dir, "fixtures", filename)
1212

1313

14-
ExampleVariables = uritemplate.variable.VariableValueDict
14+
ExampleVariables = uritemplate.variable.VariableValueMapping
1515
ExampleTemplatesAndResults = t.List[t.Tuple[str, t.Union[str, t.List[str]]]]
1616

1717

tests/test_uritemplate.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111

1212
def merge_dicts(
1313
*args: t.Union[
14-
variable.VariableValueDict, t.Dict[str, str], t.Dict[str, t.List[str]]
14+
variable.VariableValueMapping,
15+
t.Dict[str, str],
16+
t.Dict[str, t.List[str]],
1517
]
16-
) -> variable.VariableValueDict:
18+
) -> variable.VariableValueMapping:
1719
d: t.Dict[str, variable.VariableValue] = {}
1820
for arg in args:
1921
d.update(arg)
2022
return d
2123

2224

23-
ExampleVariables = variable.VariableValueDict
25+
ExampleVariables = variable.VariableValueMapping
2426
ExampleTemplatesAndResults = t.List[t.Tuple[str, t.Union[str, t.List[str]]]]
2527

2628

@@ -582,7 +584,7 @@ def test_hashability(self) -> None:
582584
self.assertEqual(d, {t: 2})
583585

584586
def test_no_mutate(self) -> None:
585-
args: variable.VariableValueDict = {}
587+
args: variable.VariableValueMapping = {}
586588
t = URITemplate("")
587589
t.expand(args, key=1)
588590
self.assertEqual(args, {})
@@ -635,7 +637,7 @@ def test_variables(self) -> None:
635637

636638

637639
class TestNativeTypeSupport(unittest.TestCase):
638-
context: variable.VariableValueDict = {
640+
context: variable.VariableValueMapping = {
639641
"zero": 0,
640642
"one": 1,
641643
"digits": list(range(10)),

uritemplate/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
def expand(
2020
uri: str,
21-
var_dict: t.Optional[variable.VariableValueDict] = None,
21+
var_dict: t.Optional[variable.VariableValueMapping] = None,
2222
**kwargs: variable.VariableValue,
2323
) -> str:
2424
"""Expand the template with the given parameters.
@@ -46,7 +46,7 @@ def expand(
4646

4747
def partial(
4848
uri: str,
49-
var_dict: t.Optional[variable.VariableValueDict] = None,
49+
var_dict: t.Optional[variable.VariableValueMapping] = None,
5050
**kwargs: variable.VariableValue,
5151
) -> URITemplate:
5252
"""Partially expand the template with the given parameters.

uritemplate/template.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525

2626

2727
def _merge(
28-
var_dict: t.Optional[variable.VariableValueDict],
29-
overrides: variable.VariableValueDict,
30-
) -> variable.VariableValueDict:
28+
var_dict: t.Optional[variable.VariableValueMapping],
29+
overrides: variable.VariableValueMapping,
30+
) -> variable.VariableValueMapping:
3131
if var_dict:
32-
opts = var_dict.copy()
32+
opts = dict(var_dict)
3333
opts.update(overrides)
3434
return opts
3535
return overrides
@@ -97,7 +97,7 @@ def __hash__(self) -> int:
9797
return hash(self.uri)
9898

9999
def _expand(
100-
self, var_dict: variable.VariableValueDict, replace: bool
100+
self, var_dict: variable.VariableValueMapping, replace: bool
101101
) -> str:
102102
if not self.variables:
103103
return self.uri
@@ -121,7 +121,7 @@ def replace_partial(match: "re.Match[str]") -> str:
121121

122122
def expand(
123123
self,
124-
var_dict: t.Optional[variable.VariableValueDict] = None,
124+
var_dict: t.Optional[variable.VariableValueMapping] = None,
125125
**kwargs: variable.VariableValue,
126126
) -> str:
127127
"""Expand the template with the given parameters.
@@ -148,7 +148,7 @@ def expand(
148148

149149
def partial(
150150
self,
151-
var_dict: t.Optional[variable.VariableValueDict] = None,
151+
var_dict: t.Optional[variable.VariableValueMapping] = None,
152152
**kwargs: variable.VariableValue,
153153
) -> "URITemplate":
154154
"""Partially expand the template with the given parameters.

uritemplate/variable.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
t.Tuple[str, ScalarVariableValue],
3030
ScalarVariableValue,
3131
]
32-
VariableValueDict = t.Dict[str, VariableValue]
32+
VariableValueMapping = t.Mapping[str, VariableValue]
3333

3434

3535
_UNRESERVED_CHARACTERS: t.Final[str] = (
@@ -451,7 +451,7 @@ def _string_expansion(
451451
return self.operator.quote(value)
452452

453453
def expand(
454-
self, var_dict: t.Optional[VariableValueDict] = None
454+
self, var_dict: t.Optional[VariableValueMapping] = None
455455
) -> t.Mapping[str, str]:
456456
"""Expand the variable in question.
457457

0 commit comments

Comments
 (0)