Python 3.9
SonarLint 6.6.0.45106
In the following code sample, SonarLint reports violations of S5655 (Arguments given to functions should be of an expected type) and S5890 (Values assigned to variables should match their type annotations) in the lines marked with the comment FP:
from collections.abc import Mapping
from typing import Any
def f(mapping: Mapping[str, Any]) -> dict[str, Any]:
return dict(mapping)
f({"a": 2}) # FP S5655
d1: Mapping[str, Any] = {"a": 2} # FP S5890
f(d1)
d2: dict[str, Any] = {"a": 2}
f(d2)
d3 = {"a": 2}
f(d3)
dict
is an instance of collections.abc.Mapping
and all keys of the dicts used here are strings, therefore all dict literals here are instances of Mapping[str, Any]
and, in consequence, all assignments should pass.
Since both assignments of d2
and d3
as well as the function calls f(d2)
and f(d3)
pass the check, the FP seems to be triggered only when a dict literal is assigned to a variable or parameter declared as a Mapping[str, Any]
.