Skip to content

Literal ellipsis and literal tuples #9140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
joshbode opened this issue Jul 13, 2020 · 5 comments
Closed

Literal ellipsis and literal tuples #9140

joshbode opened this issue Jul 13, 2020 · 5 comments

Comments

@joshbode
Copy link

I'm trying to determine how to annotate ellipsis (...) and I'm getting an error that ... is invalid for typing.Literal - is there a correct way to annotate a variable to indicate that it may contain ellipsis?

I also, out of curiosity, tried using a tuple as an element in Literal and got an unexpected result in that it appears the tuple is unpacked (e.g. Literal[("a", "b")] == Literal["a", "b"]) which may be correct, but surprised me :)

Inputs:

# test.py
from typing import Literal

x: Literal["a", 1, None, True, False]
y: Literal[("a", "b")]
z: Literal["a", ...]

x = "a"
y = ("a", "b")
z = ...

Output:

$ mypy test.py
test.py:5: error: Parameter 2 of Literal[...] is invalid
test.py:8: error: Incompatible types in assignment (expression has type "Tuple[str, str]", variable has type "Union[Literal['a'], Literal['b']]")
Found 2 errors in 1 file (checked 1 source file)

Versions:

$ python --version
Python 3.8.3
$ mypy --version
mypy 0.782
@JelleZijlstra
Copy link
Member

From looking at typeshed, you should be able to annotate Ellipsis as ellipsis (https://github.com/python/typeshed/blob/master/stdlib/2and3/builtins.pyi#L1747). PEP 586 explicitly lists all types allowed as Literals (https://www.python.org/dev/peps/pep-0586/#legal-parameters-for-literal-at-type-check-time), and Ellipsis is not among them.

The tuple thing is because the AST is the same whether or not you put the parens in, so mypy can't (easily) distinguish between Literal[1, 2] and Literal[(1, 2)].

@JukkaL
Copy link
Collaborator

JukkaL commented Sep 11, 2020

There seems to be no further action required.

@JukkaL JukkaL closed this as completed Sep 11, 2020
@BvB93
Copy link
Contributor

BvB93 commented Sep 18, 2020

From looking at typeshed, you should be able to annotate Ellipsis as ellipsis

Based on, among others, #8240 there seems to be a desire to either make ellipsis private or get rid of it.
With this in mind I'm concerned that annotation objects using ellipsis will cause issues in the (near?) future.

@gvanrossum
Copy link
Member

Based on, among others, #8240 there seems to be a desire to either make ellipsis private or get rid of it.
With this in mind I'm concerned that annotation objects using ellipsis will cause issues in the (near?) future.

You should probably petition the CPython project to add that type to types.py in the stdlib, so it can be added to typeshed.

@BvB93
Copy link
Contributor

BvB93 commented Sep 18, 2020

You should probably petition the CPython project to add that type to types.py in the stdlib, so it can be added to typeshed.

Yeah, that would be the most definitive solution; I just opened bpo-41810 for the (re-)introduction request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants