Skip to content

Allow unary + in Literal #16729

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

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
import ast as ast3

# TODO: Index, ExtSlice are deprecated in 3.9.
from ast import AST, Attribute, Call, FunctionType, Index, Name, Starred, UnaryOp, USub
from ast import AST, Attribute, Call, FunctionType, Index, Name, Starred, UAdd, UnaryOp, USub


def ast3_parse(
Expand Down Expand Up @@ -1940,13 +1940,19 @@ def visit_Constant(self, n: Constant) -> Type:

# UnaryOp(op, operand)
def visit_UnaryOp(self, n: UnaryOp) -> Type:
# We support specifically Literal[-4] and nothing else.
# For example, Literal[+4] or Literal[~6] is not supported.
# We support specifically Literal[-4], Literal[+4], and nothing else.
# For example, Literal[~6] or Literal[not False] is not supported.
typ = self.visit(n.operand)
if isinstance(typ, RawExpressionType) and isinstance(n.op, USub):
if isinstance(typ.literal_value, int):
if (
isinstance(typ, RawExpressionType)
# Use type() because we do not want to allow bools.
and type(typ.literal_value) is int # noqa: E721
):
if isinstance(n.op, USub):
typ.literal_value *= -1
return typ
if isinstance(n.op, UAdd):
return typ
return self.invalid_type(n)

def numeric_type(self, value: object, n: AST) -> Type:
Expand Down
11 changes: 8 additions & 3 deletions test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,6 @@ from typing_extensions import Literal
def dummy() -> int: return 3
a: Literal[3 + 4] # E: Invalid type: Literal[...] cannot contain arbitrary expressions
b: Literal[" foo ".trim()] # E: Invalid type: Literal[...] cannot contain arbitrary expressions
c: Literal[+42] # E: Invalid type: Literal[...] cannot contain arbitrary expressions
d: Literal[~12] # E: Invalid type: Literal[...] cannot contain arbitrary expressions
e: Literal[dummy()] # E: Invalid type: Literal[...] cannot contain arbitrary expressions
[builtins fixtures/tuple.pyi]
Expand Down Expand Up @@ -2739,16 +2738,22 @@ def test() -> None:
...
[builtins fixtures/bool.pyi]

[case testNegativeIntLiteral]
[case testUnaryOpLiteral]
from typing_extensions import Literal

a: Literal[-2] = -2
b: Literal[-1] = -1
c: Literal[0] = 0
d: Literal[1] = 1
e: Literal[2] = 2
f: Literal[+1] = 1
g: Literal[+2] = 2

x: Literal[+True] = True # E: Invalid type: Literal[...] cannot contain arbitrary expressions
y: Literal[-True] = -1 # E: Invalid type: Literal[...] cannot contain arbitrary expressions
z: Literal[~0] = 0 # E: Invalid type: Literal[...] cannot contain arbitrary expressions
[out]
[builtins fixtures/float.pyi]
[builtins fixtures/ops.pyi]

[case testNegativeIntLiteralWithFinal]
from typing_extensions import Literal, Final
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/fixtures/ops.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class tuple(Sequence[Tco]):

class function: pass

class bool: pass

class str:
def __init__(self, x: 'int') -> None: pass
def __add__(self, x: 'str') -> 'str': pass
Expand Down Expand Up @@ -54,6 +52,8 @@ class int:
def __gt__(self, x: 'int') -> bool: pass
def __ge__(self, x: 'int') -> bool: pass

class bool(int): pass

class float:
def __add__(self, x: 'float') -> 'float': pass
def __radd__(self, x: 'float') -> 'float': pass
Expand Down