Search code examples
pythonpython-typingellipsis

Type hint for Ellipsis


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'.

Solution

  • As suggested by jsbueno, the following works as of python 3.10:

    from types import EllipsisType
    
    
    def f(x: EllipsisType):
        return
    
    f(Ellipsis)