-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
List[Union[...]]
is not always satisfied by valid inputs.
#3351
Comments
No, See the docs for such situations. In your case I would recommend annotating your function as: def mean(numbers: Sequence[float]) -> float: (since you don't mutate |
Thanks for the clarification. 😄 |
This should also be called out much earlier in the mypy docs. It's a very common "bug" report. |
I think what Jukka proposed in #3352 (including link to docs in error messages) will solve the problem. |
Sorry for the necroBump but I'm not able to have a list from Literals (as the exemple here) and I guess it's the same topic. I Feel that something is possible but I cannot make the typing system satisfied with that For exemple :
I guess it's comming from |
@ierezell I don't get an error for that input. Please see Gitter for more help. |
Unlike `List`, which is invariant, `Sequence` is covariant, which lets `path` accept lists of subsets of the `Union` as well. I believe this is safe, as django doesn't mutate this input. I found [this comment](python/mypy#3351 (comment)) helpful
Unlike `List`, which is invariant, `Sequence` is covariant, which lets `path` accept lists of subsets of the `Union` as well. I believe this is safe, as django doesn't mutate this input. I found [this comment](python/mypy#3351 (comment)) helpful
To explain why this is unsafe, to help people like me who have never heard the term invariant before, say that you have the following code: def add_float(numbers: List[Union[int, float]]):
numbers.append(4.5)
ints = [1, 2, 3] # type: List[int] If you then do add_float(ints) you will end up with |
List is invariant so we have to update the function return type annotation a bit: python/mypy#3351
* change tyoe_id to type_name in DataStore for compatablility * fix mypy error * Update batch_processor.py * Update bert_based_query_creator.py * Update converter.py * Update converter.py List is invariant so we have to update the function return type annotation a bit: python/mypy#3351 * Update converter.py * Update converter.py * Update converter.py * Update converter.py * Update converter.py * Update converter.py * Update converter.py * Update forte/data/data_store.py * Update converter.py Co-authored-by: qinzzz <[email protected]> Co-authored-by: Hector <[email protected]>
Use `Sequence` instead of `List` in `apply_suffix_patterns` and `format_suffix_patterns`. This thread, python/mypy#3351 (comment), explains why `List`, which is invariant, isn't suitable here
When a list of unions is used, mypy will incorrectly error when a subset of the possible union types is used...
example.py:
console:
Presumably the inferred
List[int]
ought to be able to satisfyList[Union[int, float]]
, right?The text was updated successfully, but these errors were encountered: