Skip to content

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

@The-Compiler

Description

@The-Compiler

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions