hamcrest / PyHamcrest

Hamcrest matchers for Python
http://hamcrest.org/
Other
766 stars 111 forks source link

Mypy fails with nested matchers #207

Closed robyoung closed 2 years ago

robyoung commented 2 years ago

In the example below I have used has_items and has_properties however I am seeing this with contains and friends and has_entries as well.

Minimal reproducable example

Packages

mypy==0.971
PyHamcrest==2.0.3

Example

from dataclasses import dataclass
from hamcrest import assert_that, has_items, has_properties

@dataclass
class Example:
    name: str
    age: int

items = [Example("dave", 1), Example("wave", 2)]

assert_that(
    items,
    has_items(
        has_properties(name="dave", age=1),
        has_properties(name="wave", age=2),
    ),
)

Expected outcome

mypy does not complain as this is a valid use of has_items and has_properties.

Actual outcome

mypy returns the following error

test.py:13: error: Cannot infer type argument 1 of "assert_that"
test.py:16: error: Argument 1 to "has_items" has incompatible type "Matcher[object]"; expected "Matcher[<nothing>]"
test.py:17: error: Argument 2 to "has_items" has incompatible type "Matcher[object]"; expected "Matcher[<nothing>]"
Found 3 errors in 1 file (checked 1 source file)
offbyone commented 2 years ago

This is a minimal example on mypy-play: https://mypy-play.net/?mypy=latest&python=3.10&gist=4c8824df5632e2358e459fd80cb42388