Skip to content

Commit dcad691

Browse files
afriederddfisher
authored andcommitted
Remove support for Any(...) (#2759)
Resolves #558.
1 parent 57e5fc1 commit dcad691

12 files changed

+36
-46
lines changed

mypy/semanal.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,7 +2299,7 @@ def visit_call_expr(self, expr: CallExpr) -> None:
22992299
"""Analyze a call expression.
23002300
23012301
Some call expressions are recognized as special forms, including
2302-
cast(...) and Any(...).
2302+
cast(...).
23032303
"""
23042304
expr.callee.accept(self)
23052305
if refers_to_fullname(expr.callee, 'typing.cast'):
@@ -2325,12 +2325,8 @@ def visit_call_expr(self, expr: CallExpr) -> None:
23252325
expr.analyzed.column = expr.column
23262326
expr.analyzed.accept(self)
23272327
elif refers_to_fullname(expr.callee, 'typing.Any'):
2328-
# Special form Any(...).
2329-
if not self.check_fixed_args(expr, 1, 'Any'):
2330-
return
2331-
expr.analyzed = CastExpr(expr.args[0], AnyType())
2332-
expr.analyzed.line = expr.line
2333-
expr.analyzed.accept(self)
2328+
# Special form Any(...) no longer supported.
2329+
self.fail('Any(...) is no longer supported. Use cast(Any, ...) instead', expr)
23342330
elif refers_to_fullname(expr.callee, 'typing._promote'):
23352331
# Special form _promote(...).
23362332
if not self.check_fixed_args(expr, 1, '_promote'):

test-data/unit/check-classes.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,12 @@ class B: pass
407407
main:3: error: Incompatible types in assignment (expression has type "A", variable has type "B")
408408

409409
[case testAccessingInit]
410-
from typing import Any
410+
from typing import Any, cast
411411
class A:
412412
def __init__(self, a: 'A') -> None: pass
413413
a = None # type: A
414414
a.__init__(a) # E: Cannot access "__init__" directly
415-
(Any(a)).__init__(a)
415+
(cast(Any, a)).__init__(a)
416416

417417
[case testDeepInheritanceHierarchy]
418418
import typing

test-data/unit/check-dynamic-typing.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ a = None # type: A
230230
b = None # type: B
231231
b = cast(A, d) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
232232
a = cast(A, d)
233-
b = Any(d)
234-
a = Any(f())
233+
b = cast(Any, d)
234+
a = cast(Any, f())
235235
def f() -> None: pass
236236

237237
[case testCompatibilityOfDynamicWithOtherTypes]

test-data/unit/check-expressions.test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -648,9 +648,9 @@ s = None # type: str
648648
s = A() + B() # E: Unsupported operand types for + ("A" and "B")
649649

650650
[case testBinaryOperatorWithAnyRightOperand]
651-
from typing import Any
651+
from typing import Any, cast
652652
class A: pass
653-
A() + Any(1)
653+
A() + cast(Any, 1)
654654

655655
[case testReverseComparisonOperator]
656656

@@ -858,9 +858,9 @@ b = cast(Any, a)
858858
[case testAnyCast]
859859
from typing import cast, Any
860860
a, b = None, None # type: (A, B)
861-
a = Any(a()) # Fail
862-
a = Any(b)
863-
b = Any(a)
861+
a = cast(Any, a()) # Fail
862+
a = cast(Any, b)
863+
b = cast(Any, a)
864864
class A: pass
865865
class B: pass
866866
[out]

test-data/unit/check-inference.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -917,9 +917,9 @@ class A: pass
917917
[builtins fixtures/for.pyi]
918918

919919
[case testMultipleAssignmentWithPartialDefinition3]
920-
from typing import Any
920+
from typing import Any, cast
921921
a = None # type: A
922-
x, a = Any(a)
922+
x, a = cast(Any, a)
923923
x = a
924924
a = x
925925
x = object()

test-data/unit/check-newtype.test

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def func() -> None:
173173

174174
class MyClass:
175175
C = NewType('C', float)
176-
176+
177177
def foo(self) -> 'MyClass.C':
178178
return MyClass.C(3.2)
179179

@@ -317,3 +317,8 @@ class A: pass
317317
B = NewType('B', A)
318318
class C(B): pass # E: Cannot subclass NewType
319319
[out]
320+
321+
[case testNewTypeAny]
322+
from typing import NewType
323+
Any = NewType('Any', int)
324+
Any(5)

test-data/unit/check-tuples.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,8 @@ a, b, *c = t4
417417
[builtins fixtures/list.pyi]
418418

419419
[case testAssignmentToStarFromAny]
420-
from typing import Any
421-
a, c = Any(1), C()
420+
from typing import Any, cast
421+
a, c = cast(Any, 1), C()
422422
p, *q = a
423423
c = a
424424
c = q

test-data/unit/check-typevar-values.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ o = f(1) # E: Type argument 1 of "f" has incompatible value "object"
2222
[builtins fixtures/list.pyi]
2323

2424
[case testCallGenericFunctionWithTypeVarValueRestrictionAndAnyArgs]
25-
from typing import TypeVar, Any
25+
from typing import TypeVar, Any, cast
2626
T = TypeVar('T', int, str)
2727
def f(x: T) -> None: pass
28-
f(Any(object()))
28+
f(cast(Any, object()))
2929
[out]
3030

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

231231
[case testConstructGenericTypeWithTypevarValuesAndTypeInference]
232-
from typing import TypeVar, Generic, Any
232+
from typing import TypeVar, Generic, Any, cast
233233
X = TypeVar('X', int, str)
234234
class A(Generic[X]):
235235
def __init__(self, x: X) -> None: pass
236236
A(1)
237237
A('x')
238-
A(Any(object()))
238+
A(cast(Any, object()))
239239
A(object()) # E: Type argument 1 of "A" has incompatible value "object"
240240

241241
[case testGenericTypeWithTypevarValuesAndTypevarArgument]

test-data/unit/check-varargs.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class A: pass
189189

190190

191191
[case testCallingWithListVarArgs]
192-
from typing import List, Any
192+
from typing import List, Any, cast
193193
aa = None # type: List[A]
194194
ab = None # type: List[B]
195195
a = None # type: A
@@ -198,8 +198,8 @@ b = None # type: B
198198
f(*aa) # Fail
199199
f(a, *ab) # Ok
200200
f(a, b)
201-
(Any(f))(*aa) # IDEA: Move to check-dynamic?
202-
(Any(f))(a, *ab) # IDEA: Move to check-dynamic?
201+
(cast(Any, f))(*aa) # IDEA: Move to check-dynamic?
202+
(cast(Any, f))(a, *ab) # IDEA: Move to check-dynamic?
203203

204204
def f(a: 'A', b: 'B') -> None:
205205
pass

test-data/unit/semanal-errors.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,10 +806,10 @@ cast(str, *None) # E: 'cast' must be called with 2 positional arguments
806806
cast(str, target=None) # E: 'cast' must be called with 2 positional arguments
807807
[out]
808808

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

815815
[case testTypeListAsType]

test-data/unit/semanal-types.test

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -355,17 +355,6 @@ MypyFile:1(
355355
NameExpr(C [__main__.C])
356356
__main__.C[builtins.str, builtins.int])))
357357

358-
[case testCastToAny]
359-
from typing import Any
360-
Any(None)
361-
[out]
362-
MypyFile:1(
363-
ImportFrom:1(typing, [Any])
364-
ExpressionStmt:2(
365-
CastExpr:2(
366-
NameExpr(None [builtins.None])
367-
Any)))
368-
369358
[case testCastToTupleType]
370359
from typing import Tuple, cast
371360
cast(Tuple[int, str], None)

test-data/unit/typexport-basic.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,23 +1027,23 @@ NameExpr(7) : S
10271027

10281028
[case testBinaryOperatorWithAnyLeftOperand]
10291029
## OpExpr
1030-
from typing import Any
1030+
from typing import Any, cast
10311031
class B:
10321032
def __add__(self, x: int) -> str: pass
10331033
class A:
10341034
def __radd__(self, x: B) -> int: pass
1035-
Any(1) + A()
1035+
cast(Any, 1) + A()
10361036
B() + A()
10371037
[out]
10381038
OpExpr(7) : Any
10391039
OpExpr(8) : builtins.int
10401040

10411041
[case testBinaryOperatorWithAnyRightOperand]
10421042
## OpExpr
1043-
from typing import Any
1043+
from typing import Any, cast
10441044
class A:
10451045
def __add__(self, x: str) -> int: pass
1046-
A() + Any(1)
1046+
A() + cast(Any, 1)
10471047
[out]
10481048
OpExpr(5) : Any
10491049

0 commit comments

Comments
 (0)