-
Notifications
You must be signed in to change notification settings - Fork 254
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
Support Ellipsis as a type per se #684
Comments
Sorry, you won't get much traction here either. The problem is that you're using I would be against adding the type FWIW I have no idea what to do with your MCVE. Does it just mean that |
xarray has rolled out type annotations through the project (it's been great, and we fixed lots of problems we didn't know existed!). We have I included the That said, this is a convenience rather than a huge blocker. In particular, we can use the |
Oh, so you are an xarray dev or contributor, and you're stuck because this is in xarray's public API (and it's naturally so because of the similarity with slices). That's a reasonable concern. I came up with the following hack: from typing import *
if TYPE_CHECKING:
from enum import Enum
class ellipsis(Enum):
Ellipsis = "..."
Ellipsis = ellipsis.Ellipsis
else:
ellipsis = type(Ellipsis)
def f(a: Union[ellipsis, List], b: List) -> List:
if a is Ellipsis:
a = b
return a If that works for you now, there are two possible futures: either you could just stick with this, or you could lobby bugs.python.org to add something like ellipsis = type(Ellipsis) to |
Great, thank for you the response @gvanrossum. That's a clever hack. I'll put something into bugs.python.org |
Just a small note, type checkers will need some more special-casing around |
I believe this might be the issue that was filed to address the ellipsis type. |
So is this supported in mypy yet? In any other checker? Is it in typeshed? |
python/mypy#7818 was an issue raised to request support for |
I think we can close this now — it's been implemented in the linked commit at https://bugs.python.org/issue41810. mypy no longer needs ignores, so those were just removed in xarray: pydata/xarray#7343 Thank you to all! |
Referenced from python/mypy#7818
We use an Ellipsis as an easy-to-understand, easy-to-import sentinel value to mean "everything else".
But it's not possible to use fully with type checking, since mypy doesn't exclude it from a type when it's been excluded with an
if
, as it does forNone
, orEnum
s per #240). I've moved the issue here since it's apparently a python typing issue rather than a mypy implementation.I've included below a MCVE of the behavior below, and here's an example of how we use it, given @gvanrossum has already asked whether it's important to use an Ellipsis:
For example, to transpose an array, these are equivalent:
Code example with existing mypy:
Thank you!
The text was updated successfully, but these errors were encountered: