-
-
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
Constraining a TypeVar to be not None seems impossible. #8881
Comments
+1. I think introducing a In terms of implementation, I'd imagine If that existed, the example above could be rewritten like this:
(The third overload is needed for calling with a value of type |
@russelldavis I don't think |
Would have to be something along the lines of class A:
pass
class B(A):
pass
class C(A):
pass
class D(A):
pass
class E(A):
pass
class F(E):
pass
_TANotDOrE = TypeVar("_TANotDOrE", bound=A, exclude=(D, E)) Where |
- _WrappedMat private - Algorithm base __init__ - VideoCapture - Method overload missing self arg - python/mypy#8881
Has the best solution been invented for this case? I have just run into the same 'unconstrained TypeVar contains None' pitfall. |
Fixes #8881 This is technically unsafe, and I remember we explicitly discussed this a while ago, but related use cases turn out to be more common than I expected (judging by how popular the issue is). Also the fix is really simple. --------- Co-authored-by: Ivan Levkivskyi <ilevkivskyi@hopper.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Should probably link the addition of |
First of all, I'm using Mypy 0.770.
I'd like to annotate a function in the following way:
The idea here is that I know that a string has been serialized from a given type (converted to a json-compatible type by the
to_json
function which is not depicted here.The overloads don't work, however. I'd like them to inform mypy that this only returns
None
ifNone
goes in, otherwise it doesn't. Unfortunately, sinceT
is an unconstrainedTypeVar
, it also matchesNone
, andmypy
gives me the following error:Thus, I need a way to say "anything but None" (as a TypeVar in this case), but I can't seem to find any way to do so, so I believe this may end up being a feature request. I've seen similar behaviour being asked about in StackOverflow:
https://stackoverflow.com/questions/57854871/exclude-type-in-python-typing-annotation
https://stackoverflow.com/questions/47215682/what-type-represents-typing-any-except-none?noredirect=1&lq=1
https://stackoverflow.com/questions/58612057/how-to-make-a-generic-typevar-in-mypy-be-non-optional?noredirect=1&lq=1
Also, I'm not using any flags that change
mypy
's behaviour aboutNone
.The text was updated successfully, but these errors were encountered: