-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add a strictness flag that disables the bivariant behavior of TypeGuard
#11230
Comments
Can you clarify what error you'd want to see exactly? |
from typing import TypeGuard
def is_str_list(val: list[object]) -> TypeGuard[list[str]]: # E: list[str] is not a subtype of list[object]
... |
What value would that error message provide? Who would use it other than you? And if you saw that error message, what would you do to fix it? You can choose not to use this type of TypeGuard in your code if you don't like it. |
i believe this flag would be useful, however the broader issue is that from the PEP:
wouldn't the problem be much better solved by simply making
but couldn't you say the same thing about other scenarios where variance is taken into account? def foo(value: list[object]) -> None: ...
bar: list[int]
foo(bar) # error: Argument 1 to "foo" has incompatible type "List[int]"; expected "List[object]" in this case, you get some helpful information as well:
how is this any different? |
I understand the desire for unsafe narrowing, the issue I have is that it's extremely inconsistent with the rest of the typing landscape. The example from the pep: Ideally I would want a |
There has been discussion in the typing-sig about a
Then don't use the current TypeGuard feature in your code — or at least avoid using it in a manner that you don't like. I see no value in adding an optional error in mypy to tell you not to use it. |
The issue is that there is no way to know if the usage is safe or not, withought performing manual type checking. This has been addressed in basedmypy: def is_str_list(val: list[object]) -> val is list[str]: # error: A type-guard's type must be assignable to its parameter's type. (guard has type "list[str]", parameter has type "list[object]") [typeguard-subtype]
return all(isinstance(x, str) for x in val) So I'm going to close it. |
FYI, |
I can't stand bivariance, and I believe it should always be an opt-in behavior. I don't know any other place in python typing where bivariance exists (except for an
Any
generic parameter).Please add a strictness flag that enforces standard python typing variance rules on
TypeGuard
.The text was updated successfully, but these errors were encountered: