-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong type inference for lists of a generic type with varying component types #5218
Comments
Have you read up on invariance? |
I'm certainly familiar with the basics of in/co/contravariant types; if that's what you mean. In this case though I have precisely the same types inferred whether I pass I don't see the connection to invariance though, unless you think it's wrong that a literal list |
I don't think there is an action item. Inferring nominal joins for instance types instead of unions is a deliberate choice. Both have some downsides, but we can't change the current behaviour at least for backwards compatibility. To better understand the situation, consider this example: class B:
class C1(B): ...
class C2(B): ...
[C1(), C2()] # what should this be, List[Union[C1, C2]] or List[B]? Also there is a simple way to get a union by just giving it a union context: def f(x: List[Union[str, int]]) -> None:
...
x: List[Union[int, str]]
x = [1, "hi"] # OK
f([1, "hi"]) # Also OK Finally, there is an issue to require an annotation whenever |
@ilevkivskyi - surely it should still infer Discarding the generic type entirely makes more accurate type hints not just useless but actively counter-productive! |
No, because I wanted to propose you making
First, we don't just "discard" anything, there are rules. Second, I suggest you to watch your words. You are using results of free work of many people, so calling it "worse than useless" is at least ungrateful. |
Thanks - #5269 will fix the issue that I have downstream, which does involve a covariant generic as noted in the code sample above. I'm sorry to have come across as ungrateful - that was certainly not my intent, but I should have been more careful with my language. I do value and appreciate the work everyone puts in to Mypy and the ecosystem in general! |
OK, thanks. Written communication can lead to misunderstandings, I am glad it was just a misunderstanding. |
Using Mypy 0.610, default settings, and Python 3.6, I have found what I believe is a bug in inference about generic types.
This issue is blocking useful type hints for the
one_of
function, which takes the union of several strategies for generating test data with Hypothesis - see HypothesisWorks/hypothesis#1270 for discussion.Our current workaround is to have
one_of
accept and returnSearchStrategy[Any]
, so it does compose correctly but the checks are weaker than we'd like.The text was updated successfully, but these errors were encountered: