Annotate SearchStrategy.filter with TypeGuard overload#4672
Annotate SearchStrategy.filter with TypeGuard overload#4672CharString wants to merge 1 commit intoHypothesisWorks:masterfrom
Conversation
TypeIs case is not expressible (yet?), because you can't express the intersection type: @overload def filter(self, condition: Callable[[Ex], TypeIs[T]]) -> "SearchStrategy[Ex & T]": ... Note, while mypy accepts this, I doubt the ListStrategy annotation of `list[Ex]` is correct, because `Ex` is defined covariant and list is invariant in its argument type. class ListStrategy(SearchStrategy[list[Ex]]):
56d1985 to
784d522
Compare
|
I don't understand the motivation behind this change. |
|
@Liam-DeVoe A filter is a different branch of a conditional, in the same sense tail recursion is a for loop. For some strategies The semantics from set theoretic types (i.e. types are sets of values) are that a strategy samples from the And in a world where a bunch of code is generated by agents, having these types explicit and readable in our PBTs, helps with raising trust in the code. |
|
Here's an example of strategy whose inferred type changes: from typing import TypeGuard
def is_str(x: object) -> TypeGuard[str]:
return isinstance(x, str)
s = from_type(object).filter(is_str)
# previously: SearchStrategy[object]
# this PR: SearchStrategy[str]
reveal_type(s)In the future it would help to give a motivating example in PRs like this! Let's add some tests to |
TypeIs[T]case is not expressible (yet?), because you can't express the intersection type: