You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider reintroducing types.EllipsisType for the sake of typing #85976
Note: 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:
assignee=Noneclosed_at=<Date2020-09-22.15:55:57.057>created_at=<Date2020-09-18.19:29:00.494>labels= ['type-feature', 'library', '3.10']
title='Consider reintroducing `types.EllipsisType` for the sake of typing'updated_at=<Date2020-11-05.09:51:11.541>user='https://github.com/BvB93'
Ellipsis is one of the few builtin objects whose type is not exposed via the types module.
This is not really an issue during runtime, as you can always call type(Ellipsis), but for the purpose of typing it is detrimental; the lack of suitable type means that it is impossible to properly annotate a function which takes or returns Ellipsis (unless one is willing to resort to the use of non-public types: python/typeshed#3556).
In order to resolve this issue I propose to reintroduce types.EllipsisType. This should be a fairly simple process, so if there are no objections I'd be willing to give it a shot.
If you're asking whether or not one can infer the return type of type(Ellipsis) then yes.
In such case the inferred type is builtins.ellipsis, which is a private stub-only class (see the referenced typeshed issue in my original post).
If you're asking if a valid annotation can be constructed from type(Ellipsis) then the answer is unfortunately no (see below for a few examples).
EllipsisType = type(Ellipsis)
# Both examples are considered invalid
def func1(a: type(Ellipsis): ...
def func2(a: EllipsisType): ...
Would not be better to make MyPy supporting type(Ellipsis)? It would work also on Python versions older than 3.10.
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.
Also, could not Literal[Ellipsis] be used as annotation?
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().
BvB93 commented on Sep 21, 2020
BvB93mannequin
MannequinAuthor
More actions
If we're going ahead with this: PR #22336 contains a concrete implementation of the proposed changes.
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?
BvB93 commented on Sep 21, 2020
BvB93mannequin
MannequinAuthor
More actions
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 DictProxyType, which has already been reintroduced as MappingProxyType.
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.
New changeset 0d0e9fe by Bas van Beek in branch 'master': bpo-41810: Reintroduce types.EllipsisType, .NoneType & .NotImplementedType (GH-22336) 0d0e9fe
How does one get access to the type of Ellipsis (for type annotation purposes, not at runtime) on Pythons older than 3.10, i.e. where the types.EllipsisType introduced here isn't available? There's typing_extensions for backported typing features, but no equivalent for types?
(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.)
Activity
BvB93 commentedon Sep 18, 2020
Ellipsisis one of the few builtin objects whose type is not exposed via thetypesmodule.This is not really an issue during runtime, as you can always call
type(Ellipsis), but for the purpose of typing it is detrimental; the lack of suitable type means that it is impossible to properly annotate a function which takes or returnsEllipsis(unless one is willing to resort to the use of non-public types: python/typeshed#3556).In order to resolve this issue I propose to reintroduce
types.EllipsisType. This should be a fairly simple process, so if there are no objections I'd be willing to give it a shot.serhiy-storchaka commentedon Sep 19, 2020
Can not
type(Ellipsis)be used for typing too?BvB93 commentedon Sep 19, 2020
If you're asking whether or not one can infer the return type of
type(Ellipsis)then yes.In such case the inferred type is
builtins.ellipsis, which is a private stub-only class (see the referenced typeshed issue in my original post).If you're asking if a valid annotation can be constructed from
type(Ellipsis)then the answer is unfortunately no (see below for a few examples).gvanrossum commentedon Sep 19, 2020
Let’s do this.
serhiy-storchaka commentedon Sep 19, 2020
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?
gvanrossum commentedon Sep 19, 2020
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().
BvB93 commentedon Sep 21, 2020
If we're going ahead with this: PR #22336 contains a concrete implementation of the proposed changes.
markshannon commentedon Sep 21, 2020
I can't resist the pun on typing.
type(...)is the least typing :)More seriously,
From a general consistency point of view,
if this is to be added, shouldn't
types.NoneTypeandtypes.NotImplementedTypebe added as well?gvanrossum commentedon Sep 21, 2020
I’m okay with adding those (even though NoneType might not work in mypy, given the special-casing for None, per PEP-484).
6 remaining items
gvanrossum commentedon Sep 21, 2020
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?
BvB93 commentedon Sep 21, 2020
I don't think so, no. The only other one that stands out to me is
DictProxyType, which has already been reintroduced asMappingProxyType.ericvsmith commentedon Sep 21, 2020
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.
rhettinger commentedon Sep 21, 2020
Yes. NoneType would be useful.
gvanrossum commentedon Sep 22, 2020
New changeset 0d0e9fe by Bas van Beek in branch 'master':
bpo-41810: Reintroduce
types.EllipsisType,.NoneType&.NotImplementedType(GH-22336)0d0e9fe
SamuelMarks commentedon Nov 5, 2020
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+]
ericvsmith commentedon Nov 5, 2020
I don't think we should backport them. It's definitely a new feature, and our policy is no new features in micro versions.
tsibley commentedon Aug 30, 2023
How does one get access to the type of
Ellipsis(for type annotation purposes, not at runtime) on Pythons older than 3.10, i.e. where thetypes.EllipsisTypeintroduced here isn't available? There'styping_extensionsfor backportedtypingfeatures, but no equivalent fortypes?(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.)
gvanrossum commentedon Aug 30, 2023
try the typing category on discuss.python.org?
dev: Fix new errors in type checking around the type of Ellipsis (...)
dev: Use types.EllipsisType instead of 'builtins.ellipsis' when we can
dev: Use types.EllipsisType instead of 'builtins.ellipsis' when we can