Skip to content

Commit

Permalink
Remove support for Any(...) (#2759)
Browse files Browse the repository at this point in the history
Resolves #558.
  • Loading branch information
afrieder authored and ddfisher committed Jan 26, 2017
1 parent 57e5fc1 commit dcad691
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 46 deletions.
10 changes: 3 additions & 7 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2299,7 +2299,7 @@ def visit_call_expr(self, expr: CallExpr) -> None:
"""Analyze a call expression.
Some call expressions are recognized as special forms, including
cast(...) and Any(...).
cast(...).
"""
expr.callee.accept(self)
if refers_to_fullname(expr.callee, 'typing.cast'):
Expand All @@ -2325,12 +2325,8 @@ def visit_call_expr(self, expr: CallExpr) -> None:
expr.analyzed.column = expr.column
expr.analyzed.accept(self)
elif refers_to_fullname(expr.callee, 'typing.Any'):
# Special form Any(...).
if not self.check_fixed_args(expr, 1, 'Any'):
return
expr.analyzed = CastExpr(expr.args[0], AnyType())
expr.analyzed.line = expr.line
expr.analyzed.accept(self)
# Special form Any(...) no longer supported.
self.fail('Any(...) is no longer supported. Use cast(Any, ...) instead', expr)
elif refers_to_fullname(expr.callee, 'typing._promote'):
# Special form _promote(...).
if not self.check_fixed_args(expr, 1, '_promote'):
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,12 @@ class B: pass
main:3: error: Incompatible types in assignment (expression has type "A", variable has type "B")

[case testAccessingInit]
from typing import Any
from typing import Any, cast
class A:
def __init__(self, a: 'A') -> None: pass
a = None # type: A
a.__init__(a) # E: Cannot access "__init__" directly
(Any(a)).__init__(a)
(cast(Any, a)).__init__(a)

[case testDeepInheritanceHierarchy]
import typing
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-dynamic-typing.test
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ a = None # type: A
b = None # type: B
b = cast(A, d) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a = cast(A, d)
b = Any(d)
a = Any(f())
b = cast(Any, d)
a = cast(Any, f())
def f() -> None: pass

[case testCompatibilityOfDynamicWithOtherTypes]
Expand Down
10 changes: 5 additions & 5 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,9 @@ s = None # type: str
s = A() + B() # E: Unsupported operand types for + ("A" and "B")

[case testBinaryOperatorWithAnyRightOperand]
from typing import Any
from typing import Any, cast
class A: pass
A() + Any(1)
A() + cast(Any, 1)

[case testReverseComparisonOperator]

Expand Down Expand Up @@ -858,9 +858,9 @@ b = cast(Any, a)
[case testAnyCast]
from typing import cast, Any
a, b = None, None # type: (A, B)
a = Any(a()) # Fail
a = Any(b)
b = Any(a)
a = cast(Any, a()) # Fail
a = cast(Any, b)
b = cast(Any, a)
class A: pass
class B: pass
[out]
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -917,9 +917,9 @@ class A: pass
[builtins fixtures/for.pyi]

[case testMultipleAssignmentWithPartialDefinition3]
from typing import Any
from typing import Any, cast
a = None # type: A
x, a = Any(a)
x, a = cast(Any, a)
x = a
a = x
x = object()
Expand Down
7 changes: 6 additions & 1 deletion test-data/unit/check-newtype.test
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def func() -> None:

class MyClass:
C = NewType('C', float)

def foo(self) -> 'MyClass.C':
return MyClass.C(3.2)

Expand Down Expand Up @@ -317,3 +317,8 @@ class A: pass
B = NewType('B', A)
class C(B): pass # E: Cannot subclass NewType
[out]

[case testNewTypeAny]
from typing import NewType
Any = NewType('Any', int)
Any(5)
4 changes: 2 additions & 2 deletions test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ a, b, *c = t4
[builtins fixtures/list.pyi]

[case testAssignmentToStarFromAny]
from typing import Any
a, c = Any(1), C()
from typing import Any, cast
a, c = cast(Any, 1), C()
p, *q = a
c = a
c = q
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-typevar-values.test
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ o = f(1) # E: Type argument 1 of "f" has incompatible value "object"
[builtins fixtures/list.pyi]

[case testCallGenericFunctionWithTypeVarValueRestrictionAndAnyArgs]
from typing import TypeVar, Any
from typing import TypeVar, Any, cast
T = TypeVar('T', int, str)
def f(x: T) -> None: pass
f(Any(object()))
f(cast(Any, object()))
[out]

[case testCallGenericFunctionWithTypeVarValueRestrictionInDynamicFunc]
Expand Down Expand Up @@ -229,13 +229,13 @@ d = None # type: A[object] # E: Type argument 1 of "A" has incompatible value "
c = None # type: A[Any]

[case testConstructGenericTypeWithTypevarValuesAndTypeInference]
from typing import TypeVar, Generic, Any
from typing import TypeVar, Generic, Any, cast
X = TypeVar('X', int, str)
class A(Generic[X]):
def __init__(self, x: X) -> None: pass
A(1)
A('x')
A(Any(object()))
A(cast(Any, object()))
A(object()) # E: Type argument 1 of "A" has incompatible value "object"

[case testGenericTypeWithTypevarValuesAndTypevarArgument]
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-varargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class A: pass


[case testCallingWithListVarArgs]
from typing import List, Any
from typing import List, Any, cast
aa = None # type: List[A]
ab = None # type: List[B]
a = None # type: A
Expand All @@ -198,8 +198,8 @@ b = None # type: B
f(*aa) # Fail
f(a, *ab) # Ok
f(a, b)
(Any(f))(*aa) # IDEA: Move to check-dynamic?
(Any(f))(a, *ab) # IDEA: Move to check-dynamic?
(cast(Any, f))(*aa) # IDEA: Move to check-dynamic?
(cast(Any, f))(a, *ab) # IDEA: Move to check-dynamic?

def f(a: 'A', b: 'B') -> None:
pass
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,10 @@ cast(str, *None) # E: 'cast' must be called with 2 positional arguments
cast(str, target=None) # E: 'cast' must be called with 2 positional arguments
[out]

[case testInvalidArgsToAny]
[case testInvalidAnyCall]
from typing import Any
Any(str, None) # E: 'Any' expects 1 argument
Any(arg=str) # E: 'Any' must be called with 1 positional argument
Any(str, None) # E: Any(...) is no longer supported. Use cast(Any, ...) instead
Any(arg=str) # E: Any(...) is no longer supported. Use cast(Any, ...) instead
[out]

[case testTypeListAsType]
Expand Down
11 changes: 0 additions & 11 deletions test-data/unit/semanal-types.test
Original file line number Diff line number Diff line change
Expand Up @@ -355,17 +355,6 @@ MypyFile:1(
NameExpr(C [__main__.C])
__main__.C[builtins.str, builtins.int])))

[case testCastToAny]
from typing import Any
Any(None)
[out]
MypyFile:1(
ImportFrom:1(typing, [Any])
ExpressionStmt:2(
CastExpr:2(
NameExpr(None [builtins.None])
Any)))

[case testCastToTupleType]
from typing import Tuple, cast
cast(Tuple[int, str], None)
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/typexport-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -1027,23 +1027,23 @@ NameExpr(7) : S

[case testBinaryOperatorWithAnyLeftOperand]
## OpExpr
from typing import Any
from typing import Any, cast
class B:
def __add__(self, x: int) -> str: pass
class A:
def __radd__(self, x: B) -> int: pass
Any(1) + A()
cast(Any, 1) + A()
B() + A()
[out]
OpExpr(7) : Any
OpExpr(8) : builtins.int

[case testBinaryOperatorWithAnyRightOperand]
## OpExpr
from typing import Any
from typing import Any, cast
class A:
def __add__(self, x: str) -> int: pass
A() + Any(1)
A() + cast(Any, 1)
[out]
OpExpr(5) : Any

Expand Down

0 comments on commit dcad691

Please sign in to comment.