getsentry / responses

A utility for mocking out the Python Requests library.
Apache License 2.0
4.08k stars 347 forks source link

Matcher arguments should use `Mapping` instead of `Dict` in type annotations #698

Closed The-Compiler closed 6 months ago

The-Compiler commented 6 months ago

Describe the bug

When running mypy on code using matchers, it complains about incompatible arguments, because responses' type annotations are too restrictive: Dict (which is invariant) rather than Mapping.

it complains about the arguments being incompatible.

Additional context

https://mypy.readthedocs.io/en/stable/common_issues.html#variance

https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html#standard-duck-types

Version of responses

0.24.1

Steps to Reproduce

Run mypy over:

from responses import matchers

def demo() -> None:
    d: dict[str, str] = {"Content-Type": "application/json"}
    matchers.header_matcher(d)

Expected Result

mypy succeeds without issues

Actual Result

error: Argument 1 to "header_matcher" has incompatible type "dict[str, str]"; expected "dict[str, str | Pattern[str]]"  [arg-type]
note: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
note: Consider using "Mapping" instead, which is covariant in the value type