-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Cannot specify return type with Union[..., NotImplemented] (and others, e.g. Ellipsis) #4791
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
Comments
A similar issue occurs with I suggest that these, and other, builtin singleton values be handle the same way that |
Using the string
|
Yes, I think we can allow |
I suggest mypy continues to treat foo: Tuple[int, ...] = (2, 3, 4)
foo: Tuple[int, Ellipsis] = (2, ...) Switching them around would cause errors in both cases: foo: Tuple[int, Ellipsis] = (2, 3, 4) # error: 3 elements found, but only 2 expected
foo: Tuple[int, ...] = (2, ...) # error: type(Ellipsis) is not assignable to int |
Could also just support usage of # the commented version could be used instead,
# if this were to use explicit type aliases feature:
# EllipsisType: TypeAlias = type(Ellipsis)
EllipsisType = type(Ellipsis)
foo: Tuple[int, EllipsisType] = (2, ...) How does that look?
As far as I know, this would not be really possible as |
Some more discussion at https://mail.python.org/archives/list/[email protected]/thread/277RFXNQYLT6D3R4EYZZOPPGX56SNIK6/ |
So,
Making it a
I'm a bit unhappy. |
mypy_primer bisects this to python/typeshed#4222 to fix python/typeshed#3315 Seems like we should sprinkle some more special casing on this. Not the greatest workaround, but you could use |
Yeah, um, in my case I'm not sure what the code is doing, as it wraps a cmp-style function, and the callers then do > 0, for |
@julian-klode Not sure about your situation but I just encountered issues related to |
These are all together because pre-commit won't pass any other way. We update to the latest pre-commit hook versions, and drop the now-obsolete flake8 hook. We replace NotImplemented with Any, for simplicity, as mypy doesn't yet handle this singleton very well (python/mypy#4791). A few import locations have changed in our dependencies, so we now use the new locations.
Mypy 0.790 complains that: apt/package.py:473: error: Variable "builtins.NotImplemented" is not valid as a type apt/package.py:473: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases This worked fine in 0.782. As a workaround, use Any instead of NotImplemented. Ugh See python/mypy#4791 for more information.
This is interesting, yet really weird. It means that mypy will handle something like this: def foo() -> bool:
return NotImplemented as valid type-wise, even though this function never actually returned what was expected of it (being a bool). This probably shouldn't be the case. Even though it may be helpful since it allows for marking the return type of Also, since I didn't see any quick fix for this, if someone needs it, this is how I was able to get a mypy compliant annotation that works without issues: NotImplementedType = type(NotImplemented)
def foo() -> NotImplementedType:
... I'm relying on Edit: Apparently |
@ItsDrike In mypy 0.930, But
|
mypy is basically working as expected here. |
This does not make any sense whatsoever to me, sorry. Can you elaborate? Is every type in Mypy implicitly a union with I don’t mean to be confrontational, I really don’t, but these are the best of my guesses at what you could mean and they all sound kind of stupid to me now that I’ve written them out. What am I missing? |
Basically |
@JelleZijlstra Ah, I see, so indeed every type is implicitly a union with { @hauntsaninja What about |
|
@erictraut So |
Uh oh!
There was an error while loading. Please reload this page.
[UPDATE: The reported behavior happens only with
--warn-return-any
or--strict
.]Mypy gives
warning: Returning Any from function declared to return "bool"
forSo, try this:
which gives error
Following mypy's suggestion results in:
which also gives a runtime error:
TypeError: Parameters to generic types must be types. Got NotImplemented.
or (when attemptingtype[...]
)TypeError: 'type' object is not subscriptable
.(See also pull #4770, which attempts to fix some magic methods)
There seem to be two issues:
NotImplemented
without a complaint from mypy-> Union[bool, type(NotImplemented)]
, mypy doesn't like that and suggests changing it to-> Union[bool, type[NotImplemented]]
, which gets a runtime error (Type[NotImplemented]
doesn't work either).The text was updated successfully, but these errors were encountered: