-
-
Notifications
You must be signed in to change notification settings - Fork 30.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
Consider reintroducing types.EllipsisType
for the sake of typing
#85976
Comments
In order to resolve this issue I propose to reintroduce |
Can not |
If you're asking whether or not one can infer the return type of If you're asking if a valid annotation can be constructed from
|
Let’s do this. |
Would not be better to make MyPy supporting type(Ellipsis)? It would work also on Python versions older than 3.10. Also, could not Literal[Ellipsis] be used as annotation? |
That would be quite complicated. There is no place in annotations where a function call is currently supported, so I'd prefer not to go there.
Alas, it currently doesn't work (PEP-586 only allows specific types, and type checkers have implemented it exactly). Making it work would be more complicated than the proposal -- once it exists in types.py, it's trivial to add support to mypy. IMO ideally, eventually, all "hidden" built-in types ought to be exposed somewhere, unless they are truly implementation details -- but since Ellipsis is a first-class singleton object, I don't see how its type could be an implementation detail. (Its name is actually clearly visible in repr(type(Ellipsis)).) Note that we have started exporting the types of other constructs through types.py, e.g. type(int|str) is types.Union, and type(list[str]) is types.GenericAlias. This is revealed in their repr(). |
If we're going ahead with this: PR #22336 contains a concrete implementation of the proposed changes. |
I can't resist the pun on typing. More seriously, From a general consistency point of view, |
I’m okay with adding those (even though NoneType might not work in mypy, given the special-casing for None, per PEP-484). |
Apparently pyright has some interest in In any case, I'm ok with adding |
Please do. |
|
Does anyone know why types.EllipsisType was removed to begin with? I just want to make sure we're not repeating some mistake of the past. |
Bas can you do some research to answer Eric's question? In theory it should be possible to use git bisect to find it. I see it in 2.7 but not in 3.4. That's a fairly large interval, alas. |
According to the relevant commit (c9543e4): |
Thanks! Most of the deleted types *are* builtins (e.g. object, int). I assume the reasoning was just that if you wanted the type you could just write type(Ellipsis), type(None) etc. But that's too dynamic for static checkers, so we want *some* stable name for those back. Do any of the other deleted types strike you as possibly needing to come back? |
I don't think so, no. The only other one that stands out to me is |
Thanks for doing the research, Bas! It sounds like adding back in NoneType, NotImplementedType, and EllipsisType is appropriate, then. +1 The commit should have a comment about the reason: for type checkers which can't use type(Ellipsis), etc. I'll add a comment on the PR about adding a similar note to the blurb. |
Yes. NoneType would be useful. |
Since we're bringing these back, would you accept a backport of these types? [I'm writing a bunch of parsers/emitters—and starting to maintain an old runtime type-checker—for 3.6+] |
I don't think we should backport them. It's definitely a new feature, and our policy is no new features in micro versions. |
How does one get access to the type of (The "right" place to raise this sort of issue/question isn't clear to me, so if this isn't the right place, please let me know or point me elsewhere.) |
try the typing category on discuss.python.org? |
pyright 1.1.325 (released yesterday) added support for @type_check_only decorators¹, which were added (a year ago) to the builtins.ellipsis type of the stdlib typeshed². This type is available only during type-checking and doesn't actually exist at runtime, but previously pyright didn't care about that (and mypy still doesn't). Now it does, noticed thanks to scheduled CI, and throws an error like: "ellipsis" is marked as @type_check_only and can be used only in type annotations (reportGeneralTypeIssues) Explicitly clarify to pyright that our variable is a type alias³ and not a value also used at runtime so it knows its ok to use the type of Ellipsis (via a forward reference to the now-you-see-it, now-you-don't builtins.ellipsis). Relatedly, we can switch from 'builtins.ellipsis' → types.EllipsisType on Python ≥3.10⁴ and stop using the private/internal type for Ellipsis. I'll do that in the subsequent commit. ¹ <microsoft/pyright#5817> ² <python/typeshed@a92da58> <python/typeshed#8531> ³ <https://docs.python.org/3/library/typing.html#typing.TypeAlias> <https://peps.python.org/pep-0613/> ⁴ <https://docs.python.org/3/library/types.html#types.EllipsisType> <python/cpython#85976>
The former is preferable as its not a private/internal type¹, but it's only available on on Python ≥3.10.² ¹ <python/cpython#85976> ² <https://docs.python.org/3/library/types.html#types.EllipsisType>
The former is preferable as its not a private/internal type¹, but it's only available on on Python ≥3.10.² ¹ <python/cpython#85976> ² <https://docs.python.org/3/library/types.html#types.EllipsisType>
types.EllipsisType
,.NoneType
&.NotImplementedType
#22336Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: