Open borislubimov opened 1 year ago
Is this the same issue as #222?
It's definitely related, at least changing type in either first or second argument of assert_that, fixes the issue. While changing types in both - returns the issue:
def test_assert_contravariant():
from hamcrest import assert_that, contains_exactly
from typing import TypeVar
from hamcrest.core.matcher import Matcher
li = [1, 2, 3]
T = TypeVar("T")
T_contravariant = TypeVar("T_contravariant", contravariant=True)
# 1 case
def my_assert_that_1(actual_or_assertion: T_contravariant, matcher: Matcher[T], reason: str = "") -> None:
assert_that(actual_or_assertion, matcher, reason)
my_assert_that_1(li, contains_exactly(*li)) # No Warnings
# 2 case
def my_assert_that_2(actual_or_assertion: T, matcher: Matcher[T_contravariant], reason: str = "") -> None:
assert_that(actual_or_assertion, matcher, reason)
my_assert_that_2(li, contains_exactly(*li)) # No Warnings
# 3 case
def my_assert_that_contr3(actual_or_assertion: T_contravariant, matcher: Matcher[T_contravariant],
reason: str = "") -> None:
assert_that(actual_or_assertion, matcher, reason)
# Expected type 'Matcher[list[int]]' (matched generic type 'Matcher[T_contravariant]'), got 'Matcher[Sequence]' instead
my_assert_that_contr3(li, contains_exactly(*li))
As I said on #222, I wasn't able to get this working on the real codebase. I'd very much appreciate a PR if anyone else can!
Currently, PyCharm highlights all usages of assert_that with sequence matchers due to unexpected type warnings. Is it possible to include an overloaded version of assert_that specifically designed for such cases? Would something like in the example below work?
How it looks in PyCharm: