Skip to content

Commit

Permalink
Allow unary + in Literal (#16729)
Browse files Browse the repository at this point in the history
Implements python/typing#1550

Fixes #16727 (a trivial bug I found while implementing this feature)
  • Loading branch information
JelleZijlstra authored Jan 2, 2024
1 parent d0d5876 commit f9e8e0b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
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

0 comments on commit f9e8e0b

Please sign in to comment.