Repository navigation
Consider reintroducing types.EllipsisType for the sake of typing #85976
Description
Activity
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 calltype(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.- added3.10 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
on Sep 18, 2020 Can not
type(Ellipsis)be used for typing too?If you're asking whether or not one can infer the return type of
type(Ellipsis)then yes.
In such case the inferred type isbuiltins.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): ...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?
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().
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.
type(...)is the least typing :)More seriously,
From a general consistency point of view,
if this is to be added, shouldn'ttypes.NoneTypeandtypes.NotImplementedTypebe added as well?Reacted by Maciej (MJ) MikulskiI’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
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?
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 asMappingProxyType.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.
Do any of the other deleted types strike you as possibly
needing to come back?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
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.)
Reacted by Pietro D'Antuonotry the typing category on discuss.python.org?
Reacted by Alex Waygood
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: