From 43a300b4fca824ee3ee0fc442e49d6b0d25945c1 Mon Sep 17 00:00:00 2001 From: Alex Frieder Date: Wed, 25 Jan 2017 16:22:14 -0500 Subject: [PATCH 1/3] Semanal no longer supports Any(...) --- mypy/semanal.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index d0133cd44f77..08bff942f5db 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -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'): @@ -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'): From 7275547afb324f896d5a121ea96cb0ff4e06e4d2 Mon Sep 17 00:00:00 2001 From: Alex Frieder Date: Wed, 25 Jan 2017 16:22:29 -0500 Subject: [PATCH 2/3] Add tests for no longer supporting Any(...) --- test-data/unit/check-newtype.test | 7 ++++++- test-data/unit/semanal-errors.test | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/test-data/unit/check-newtype.test b/test-data/unit/check-newtype.test index 880a00feb320..7ebe696dd8e7 100644 --- a/test-data/unit/check-newtype.test +++ b/test-data/unit/check-newtype.test @@ -173,7 +173,7 @@ def func() -> None: class MyClass: C = NewType('C', float) - + def foo(self) -> 'MyClass.C': return MyClass.C(3.2) @@ -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) diff --git a/test-data/unit/semanal-errors.test b/test-data/unit/semanal-errors.test index ce4e68432622..aa45dde1faca 100644 --- a/test-data/unit/semanal-errors.test +++ b/test-data/unit/semanal-errors.test @@ -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] From 7ea841e401d59e93dac4558a39c03670d73dac9b Mon Sep 17 00:00:00 2001 From: Alex Frieder Date: Wed, 25 Jan 2017 16:22:39 -0500 Subject: [PATCH 3/3] Update old tests to no longer use Any(...) --- test-data/unit/check-classes.test | 4 ++-- test-data/unit/check-dynamic-typing.test | 4 ++-- test-data/unit/check-expressions.test | 10 +++++----- test-data/unit/check-inference.test | 4 ++-- test-data/unit/check-tuples.test | 4 ++-- test-data/unit/check-typevar-values.test | 8 ++++---- test-data/unit/check-varargs.test | 6 +++--- test-data/unit/semanal-types.test | 11 ----------- test-data/unit/typexport-basic.test | 8 ++++---- 9 files changed, 24 insertions(+), 35 deletions(-) diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index 6719abe660d2..445212defe98 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -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 diff --git a/test-data/unit/check-dynamic-typing.test b/test-data/unit/check-dynamic-typing.test index 997a9961eed7..27f8bee9f1c8 100644 --- a/test-data/unit/check-dynamic-typing.test +++ b/test-data/unit/check-dynamic-typing.test @@ -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] diff --git a/test-data/unit/check-expressions.test b/test-data/unit/check-expressions.test index 574c8c379bd8..8d221748023e 100644 --- a/test-data/unit/check-expressions.test +++ b/test-data/unit/check-expressions.test @@ -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] @@ -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] diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 54f73f5c1fb0..799fb3525d70 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -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() diff --git a/test-data/unit/check-tuples.test b/test-data/unit/check-tuples.test index 13c8ba4558ea..a506bceaf21e 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -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 diff --git a/test-data/unit/check-typevar-values.test b/test-data/unit/check-typevar-values.test index 26e56a510915..5ae96f978cf9 100644 --- a/test-data/unit/check-typevar-values.test +++ b/test-data/unit/check-typevar-values.test @@ -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] @@ -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] diff --git a/test-data/unit/check-varargs.test b/test-data/unit/check-varargs.test index 8187ea780519..cad1dad0231c 100644 --- a/test-data/unit/check-varargs.test +++ b/test-data/unit/check-varargs.test @@ -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 @@ -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 diff --git a/test-data/unit/semanal-types.test b/test-data/unit/semanal-types.test index 927ab85b34cf..beb3cbfa660e 100644 --- a/test-data/unit/semanal-types.test +++ b/test-data/unit/semanal-types.test @@ -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) diff --git a/test-data/unit/typexport-basic.test b/test-data/unit/typexport-basic.test index e7915cc60f8a..a51d844ed23c 100644 --- a/test-data/unit/typexport-basic.test +++ b/test-data/unit/typexport-basic.test @@ -1027,12 +1027,12 @@ 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 @@ -1040,10 +1040,10 @@ 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