From 0d4af8690043383c9cd3d6bd158f21ae9d6a93de Mon Sep 17 00:00:00 2001 From: Jonathan Green Date: Fri, 3 Oct 2025 10:13:37 -0300 Subject: [PATCH] Fix mypy type errors by using Mapping instead of Dict 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. --- tests/test_from_fixtures.py | 2 +- tests/test_uritemplate.py | 12 +++++++----- uritemplate/api.py | 4 ++-- uritemplate/template.py | 14 +++++++------- uritemplate/variable.py | 4 ++-- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/tests/test_from_fixtures.py b/tests/test_from_fixtures.py index bbb4645..d497510 100644 --- a/tests/test_from_fixtures.py +++ b/tests/test_from_fixtures.py @@ -11,7 +11,7 @@ def fixture_file_path(filename: str) -> str: return os.path.join(absolute_dir, "fixtures", filename) -ExampleVariables = uritemplate.variable.VariableValueDict +ExampleVariables = uritemplate.variable.VariableValueMapping ExampleTemplatesAndResults = t.List[t.Tuple[str, t.Union[str, t.List[str]]]] diff --git a/tests/test_uritemplate.py b/tests/test_uritemplate.py index dc185d5..dfe19f5 100644 --- a/tests/test_uritemplate.py +++ b/tests/test_uritemplate.py @@ -11,16 +11,18 @@ def merge_dicts( *args: t.Union[ - variable.VariableValueDict, t.Dict[str, str], t.Dict[str, t.List[str]] + variable.VariableValueMapping, + t.Dict[str, str], + t.Dict[str, t.List[str]], ] -) -> variable.VariableValueDict: +) -> variable.VariableValueMapping: d: t.Dict[str, variable.VariableValue] = {} for arg in args: d.update(arg) return d -ExampleVariables = variable.VariableValueDict +ExampleVariables = variable.VariableValueMapping ExampleTemplatesAndResults = t.List[t.Tuple[str, t.Union[str, t.List[str]]]] @@ -582,7 +584,7 @@ def test_hashability(self) -> None: self.assertEqual(d, {t: 2}) def test_no_mutate(self) -> None: - args: variable.VariableValueDict = {} + args: variable.VariableValueMapping = {} t = URITemplate("") t.expand(args, key=1) self.assertEqual(args, {}) @@ -635,7 +637,7 @@ def test_variables(self) -> None: class TestNativeTypeSupport(unittest.TestCase): - context: variable.VariableValueDict = { + context: variable.VariableValueMapping = { "zero": 0, "one": 1, "digits": list(range(10)), diff --git a/uritemplate/api.py b/uritemplate/api.py index 8da3914..045100d 100644 --- a/uritemplate/api.py +++ b/uritemplate/api.py @@ -18,7 +18,7 @@ def expand( uri: str, - var_dict: t.Optional[variable.VariableValueDict] = None, + var_dict: t.Optional[variable.VariableValueMapping] = None, **kwargs: variable.VariableValue, ) -> str: """Expand the template with the given parameters. @@ -46,7 +46,7 @@ def expand( def partial( uri: str, - var_dict: t.Optional[variable.VariableValueDict] = None, + var_dict: t.Optional[variable.VariableValueMapping] = None, **kwargs: variable.VariableValue, ) -> URITemplate: """Partially expand the template with the given parameters. diff --git a/uritemplate/template.py b/uritemplate/template.py index 3205380..7582f2f 100644 --- a/uritemplate/template.py +++ b/uritemplate/template.py @@ -25,11 +25,11 @@ def _merge( - var_dict: t.Optional[variable.VariableValueDict], - overrides: variable.VariableValueDict, -) -> variable.VariableValueDict: + var_dict: t.Optional[variable.VariableValueMapping], + overrides: variable.VariableValueMapping, +) -> variable.VariableValueMapping: if var_dict: - opts = var_dict.copy() + opts = dict(var_dict) opts.update(overrides) return opts return overrides @@ -97,7 +97,7 @@ def __hash__(self) -> int: return hash(self.uri) def _expand( - self, var_dict: variable.VariableValueDict, replace: bool + self, var_dict: variable.VariableValueMapping, replace: bool ) -> str: if not self.variables: return self.uri @@ -121,7 +121,7 @@ def replace_partial(match: "re.Match[str]") -> str: def expand( self, - var_dict: t.Optional[variable.VariableValueDict] = None, + var_dict: t.Optional[variable.VariableValueMapping] = None, **kwargs: variable.VariableValue, ) -> str: """Expand the template with the given parameters. @@ -148,7 +148,7 @@ def expand( def partial( self, - var_dict: t.Optional[variable.VariableValueDict] = None, + var_dict: t.Optional[variable.VariableValueMapping] = None, **kwargs: variable.VariableValue, ) -> "URITemplate": """Partially expand the template with the given parameters. diff --git a/uritemplate/variable.py b/uritemplate/variable.py index 02e33e0..1f7993c 100644 --- a/uritemplate/variable.py +++ b/uritemplate/variable.py @@ -29,7 +29,7 @@ t.Tuple[str, ScalarVariableValue], ScalarVariableValue, ] -VariableValueDict = t.Dict[str, VariableValue] +VariableValueMapping = t.Mapping[str, VariableValue] _UNRESERVED_CHARACTERS: t.Final[str] = ( @@ -451,7 +451,7 @@ def _string_expansion( return self.operator.quote(value) def expand( - self, var_dict: t.Optional[VariableValueDict] = None + self, var_dict: t.Optional[VariableValueMapping] = None ) -> t.Mapping[str, str]: """Expand the variable in question.