How can I specify a type hint for a function that expects a literal Ellipsis
(or ...
) as an argument? In analogy with None
, I'm tempted to write something like:
def f(x: Ellipsis):
return
f(Ellipsis)
If I run this through mypy
, however, I get:
1: error: Variable "builtins.Ellipsis" is not valid as a type
mypy
is happy with the following:
import builtins
def f(x: builtins.ellipsis):
return
f(Ellipsis)
But this fails at runtime with:
AttributeError: module 'builtins' has no attribute 'ellipsis'.
builtins
and try"builtins.Ellipsis"
I could have sworn they were going to expose this type somewhere, but it would be in a very recent version of Python – TrilbiLiteral[Ellipsis]
may do the job – Strontianite"builtins.ellipsis"
, which could still be a workaround in Python < 3.10 – TrilbiEllipsis
- I can't find anywhere that the class definition for<class 'ellipsis'>
is actually exposed in python<3.10, which is of course the crux of the problem. – Chieflyimport builtins
then use"builtins.ellipsis"
. AFAIKT, it works for mypy at least. That type wasn't exposed prior to Python 3.10, I'm pretty sure – Trilbi"builtins.ellipsis"
. – Trilbi