1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
[case testClassVarDef]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int]
[out]
MypyFile:1(
ImportFrom:1(typing, [ClassVar])
ClassDef:2(
A
AssignmentStmt:3(
NameExpr(x [m])
IntExpr(1)
builtins.int)))
[case testClassVarDefInModuleScope]
from typing import ClassVar
x = None # type: ClassVar[int]
[out]
main:2: error: ClassVar can only be used for assignments in class body
[case testClassVarDefInFuncScope]
from typing import ClassVar
def f() -> None:
x = None # type: ClassVar[int]
[out]
main:3: error: ClassVar can only be used for assignments in class body
[case testClassVarDefInMethod]
from typing import ClassVar
class A:
def f(self) -> None:
x = None # type: ClassVar
[out]
main:4: error: ClassVar can only be used for assignments in class body
[case testClassVarTooManyArguments]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int, str]
[out]
main:3: error: ClassVar[...] must have at most one type argument
[case testClassVarWithoutArguments]
from typing import ClassVar
class A:
x = 1 # type: ClassVar
[out]
MypyFile:1(
ImportFrom:1(typing, [ClassVar])
ClassDef:2(
A
AssignmentStmt:3(
NameExpr(x [m])
IntExpr(1)
Any)))
[case testClassVarWithTypeVar]
from typing import ClassVar, TypeVar
T = TypeVar('T')
class A:
x = None # type: ClassVar[T]
[out]
main:5: error: Type variable "__main__.T" is unbound
main:5: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
main:5: note: (Hint: Use "T" in function signature to bind "T" inside a function)
[case testClassVarInFunctionArgs]
from typing import ClassVar
def f(x: str, y: ClassVar) -> None: pass
[out]
main:2: error: ClassVar can only be used for assignments in class body
[case testClassVarInMethodArgs]
from typing import ClassVar
class A:
def f(x: str, y: ClassVar) -> None: pass
[out]
main:3: error: ClassVar can only be used for assignments in class body
[case testClassVarFunctionRetType]
from typing import ClassVar
def f() -> ClassVar: pass
[out]
main:2: error: ClassVar can only be used for assignments in class body
[case testClassVarMethodRetType]
from typing import ClassVar
class A:
def f(self) -> ClassVar: pass
[out]
main:3: error: ClassVar can only be used for assignments in class body
[case testMultipleClassVarInFunctionSig]
from typing import ClassVar
def f(x: ClassVar, y: ClassVar) -> ClassVar: pass
[out]
main:2: error: ClassVar can only be used for assignments in class body
[case testClassVarInCallableArgs]
from typing import Callable, ClassVar, Any
f = None # type: Callable[[int, ClassVar], Any]
[out]
main:2: error: Invalid type: ClassVar nested inside other type
[case testClassVarInCallableRet]
from typing import Callable, ClassVar
f = None # type: Callable[..., ClassVar]
[out]
main:2: error: Invalid type: ClassVar nested inside other type
[case testClassVarInUnion]
from typing import ClassVar, Union
x = None # type: Union[ClassVar, str]
[out]
main:2: error: Invalid type: ClassVar nested inside other type
[case testClassVarInUnionAsAttribute]
from typing import ClassVar, Union
class A:
x = None # type: Union[ClassVar, str]
[out]
main:3: error: Invalid type: ClassVar nested inside other type
[case testListWithClassVars]
from typing import ClassVar, List
x = [] # type: List[ClassVar]
[builtins fixtures/list.pyi]
[out]
main:2: error: Invalid type: ClassVar nested inside other type
[case testTupleClassVar]
from typing import ClassVar, Tuple
x = None # type: Tuple[ClassVar, int]
[builtins fixtures/tuple.pyi]
[out]
main:2: error: Invalid type: ClassVar nested inside other type
[case testMultipleLvaluesWithList]
from typing import ClassVar, Tuple
class A:
[x, y] = None, None # type: Tuple[ClassVar, ClassVar]
[builtins fixtures/tuple.pyi]
[out]
main:3: error: Invalid type: ClassVar nested inside other type
[case testDeeplyNested]
from typing import Callable, ClassVar, Union
class A: pass
class B:
x = None # type: Union[str, Callable[[A, ClassVar], int]]
[out]
main:4: error: Invalid type: ClassVar nested inside other type
[case testClassVarInClassVar]
from typing import ClassVar
class A:
x = None # type: ClassVar[ClassVar[int]]
[out]
main:3: error: Invalid type: ClassVar nested inside other type
[case testInsideGeneric]
from typing import ClassVar, Generic, TypeVar
T = TypeVar('T')
class A(Generic[T]): pass
class B:
x = None # type: A[ClassVar]
[out]
main:5: error: Invalid type: ClassVar nested inside other type
[case testDefineOnSelf]
from typing import ClassVar
class A:
def __init__(self) -> None:
self.x = None # type: ClassVar
[out]
main:4: error: ClassVar can only be used for assignments in class body
[case testForIndex]
from typing import ClassVar
for i in []: # type: ClassVar
pass
[out]
main:2: error: ClassVar can only be used for assignments in class body
[case testForIndexInClassBody]
from typing import ClassVar
class A:
for i in []: # type: ClassVar
pass
[out]
main:3: error: ClassVar can only be used for assignments in class body
[case testWithStmt]
from typing import ClassVar
class A: pass
with A() as x: # type: ClassVar
pass
[out]
main:3: error: ClassVar can only be used for assignments in class body
[case testWithStmtInClassBody]
from typing import ClassVar
class A: pass
class B:
with A() as x: # type: ClassVar
pass
[out]
main:4: error: ClassVar can only be used for assignments in class body
[case testClassVarWithTypeVariable]
from typing import ClassVar, TypeVar, Generic, List
T = TypeVar('T')
class Some(Generic[T]):
error: ClassVar[T] # E: ClassVar cannot contain type variables
nested: ClassVar[List[List[T]]] # E: ClassVar cannot contain type variables
ok: ClassVar[int]
[out]
|