Home > Mobile >  python: annotation for ellipsis to be a possible argument of a function
python: annotation for ellipsis to be a possible argument of a function

Time:12-24

For "certain reasons" I want to allow python's Ellipsis "..." to (also) be an argument to some function (let's say) f, e.g. as follows:

def f(arg: ?) -> None:
    print(arg)

f(...)

what annotation should I use for arg?

CodePudding user response:

... is an instance of the Ellipsis class, so you could do something like:

def f(arg:Ellipsis) -> None:
    # your code

Since there is only one instance of that class, you probably want to combine it with some other types. Otherwise why have a parameter at all (could it be a way to detect all calls to function f by temporarily setting an unusable type)

CodePudding user response:

It appears that

from types import EllipsisType

def f(arg: EllipsisType):
    print(arg)

f(...)  # or alternatively f(Ellipsis)

is the right solution here (i.e. to annotate the Ellipsis itself)

  • Related