| 1 |
[case testAttrsSimple_no_empty] |
| 2 |
import attr |
| 3 |
@attr.s |
| 4 |
class A: |
| 5 |
a = attr.ib() |
| 6 |
_b = attr.ib() |
| 7 |
c = attr.ib(18) |
| 8 |
_d = attr.ib(validator=None, default=18) |
| 9 |
E = 18 |
| 10 |
|
| 11 |
def foo(self): |
| 12 |
return self.a |
| 13 |
reveal_type(A) # N: Revealed type is "def (a: Any, b: Any, c: Any =, d: Any =) -> __main__.A" |
| 14 |
A(1, [2]) |
| 15 |
A(1, [2], '3', 4) |
| 16 |
A(1, 2, 3, 4) |
| 17 |
A(1, [2], '3', 4, 5) # E: Too many arguments for "A" |
| 18 |
[builtins fixtures/list.pyi] |
| 19 |
|
| 20 |
[case testAttrsAnnotated] |
| 21 |
import attr |
| 22 |
from typing import List, ClassVar |
| 23 |
@attr.s |
| 24 |
class A: |
| 25 |
a: int = attr.ib() |
| 26 |
_b: List[int] = attr.ib() |
| 27 |
c: str = attr.ib('18') |
| 28 |
_d: int = attr.ib(validator=None, default=18) |
| 29 |
E = 7 |
| 30 |
F: ClassVar[int] = 22 |
| 31 |
reveal_type(A) # N: Revealed type is "def (a: builtins.int, b: builtins.list[builtins.int], c: builtins.str =, d: builtins.int =) -> __main__.A" |
| 32 |
A(1, [2]) |
| 33 |
A(1, [2], '3', 4) |
| 34 |
A(1, 2, 3, 4) # E: Argument 2 to "A" has incompatible type "int"; expected "list[int]" # E: Argument 3 to "A" has incompatible type "int"; expected "str" |
| 35 |
A(1, [2], '3', 4, 5) # E: Too many arguments for "A" |
| 36 |
[builtins fixtures/list.pyi] |
| 37 |
|
| 38 |
[case testAttrsTypeComments] |
| 39 |
import attr |
| 40 |
from typing import List, ClassVar |
| 41 |
@attr.s |
| 42 |
class A: |
| 43 |
a = attr.ib() # type: int |
| 44 |
_b = attr.ib() # type: List[int] |
| 45 |
c = attr.ib('18') # type: str |
| 46 |
_d = attr.ib(validator=None, default=18) # type: int |
| 47 |
E = 7 |
| 48 |
F: ClassVar[int] = 22 |
| 49 |
reveal_type(A) # N: Revealed type is "def (a: builtins.int, b: builtins.list[builtins.int], c: builtins.str =, d: builtins.int =) -> __main__.A" |
| 50 |
A(1, [2]) |
| 51 |
A(1, [2], '3', 4) |
| 52 |
A(1, 2, 3, 4) # E: Argument 2 to "A" has incompatible type "int"; expected "list[int]" # E: Argument 3 to "A" has incompatible type "int"; expected "str" |
| 53 |
A(1, [2], '3', 4, 5) # E: Too many arguments for "A" |
| 54 |
[builtins fixtures/list.pyi] |
| 55 |
|
| 56 |
[case testAttrsAutoAttribs] |
| 57 |
import attr |
| 58 |
from typing import List, ClassVar |
| 59 |
@attr.s(auto_attribs=True) |
| 60 |
class A: |
| 61 |
a: int |
| 62 |
_b: List[int] |
| 63 |
c: str = '18' |
| 64 |
_d: int = attr.ib(validator=None, default=18) |
| 65 |
E = 7 |
| 66 |
F: ClassVar[int] = 22 |
| 67 |
reveal_type(A) # N: Revealed type is "def (a: builtins.int, b: builtins.list[builtins.int], c: builtins.str =, d: builtins.int =) -> __main__.A" |
| 68 |
A(1, [2]) |
| 69 |
A(1, [2], '3', 4) |
| 70 |
A(1, 2, 3, 4) # E: Argument 2 to "A" has incompatible type "int"; expected "list[int]" # E: Argument 3 to "A" has incompatible type "int"; expected "str" |
| 71 |
A(1, [2], '3', 4, 5) # E: Too many arguments for "A" |
| 72 |
[builtins fixtures/list.pyi] |
| 73 |
|
| 74 |
[case testAttrsUntypedNoUntypedDefs] |
| 75 |
# flags: --disallow-untyped-defs |
| 76 |
import attr |
| 77 |
@attr.s |
| 78 |
class A: |
| 79 |
a = attr.ib() # E: Need type annotation for "a" |
| 80 |
_b = attr.ib() # E: Need type annotation for "_b" |
| 81 |
c = attr.ib(18) # E: Need type annotation for "c" |
| 82 |
_d = attr.ib(validator=None, default=18) # E: Need type annotation for "_d" |
| 83 |
E = 18 |
| 84 |
[builtins fixtures/bool.pyi] |
| 85 |
|
| 86 |
[case testAttrsWrongReturnValue] |
| 87 |
import attr |
| 88 |
@attr.s |
| 89 |
class A: |
| 90 |
x: int = attr.ib(8) |
| 91 |
def foo(self) -> str: |
| 92 |
return self.x # E: Incompatible return value type (got "int", expected "str") |
| 93 |
@attr.s |
| 94 |
class B: |
| 95 |
x = attr.ib(8) # type: int |
| 96 |
def foo(self) -> str: |
| 97 |
return self.x # E: Incompatible return value type (got "int", expected "str") |
| 98 |
@attr.dataclass |
| 99 |
class C: |
| 100 |
x: int = 8 |
| 101 |
def foo(self) -> str: |
| 102 |
return self.x # E: Incompatible return value type (got "int", expected "str") |
| 103 |
@attr.s |
| 104 |
class D: |
| 105 |
x = attr.ib(8, type=int) |
| 106 |
def foo(self) -> str: |
| 107 |
return self.x # E: Incompatible return value type (got "int", expected "str") |
| 108 |
[builtins fixtures/bool.pyi] |
| 109 |
|
| 110 |
[case testAttrsSeriousNames] |
| 111 |
from attr import attrib, attrs |
| 112 |
from typing import List |
| 113 |
@attrs(init=True) |
| 114 |
class A: |
| 115 |
a = attrib() |
| 116 |
_b: List[int] = attrib() |
| 117 |
c = attrib(18) |
| 118 |
_d = attrib(validator=None, default=18) |
| 119 |
CLASS_VAR = 18 |
| 120 |
reveal_type(A) # N: Revealed type is "def (a: Any, b: builtins.list[builtins.int], c: Any =, d: Any =) -> __main__.A" |
| 121 |
A(1, [2]) |
| 122 |
A(1, [2], '3', 4) |
| 123 |
A(1, 2, 3, 4) # E: Argument 2 to "A" has incompatible type "int"; expected "list[int]" |
| 124 |
A(1, [2], '3', 4, 5) # E: Too many arguments for "A" |
| 125 |
[builtins fixtures/list.pyi] |
| 126 |
|
| 127 |
[case testAttrsDefaultErrors] |
| 128 |
import attr |
| 129 |
@attr.s |
| 130 |
class A: |
| 131 |
x = attr.ib(default=17) |
| 132 |
y = attr.ib() # E: Non-default attributes not allowed after default attributes. |
| 133 |
@attr.s(auto_attribs=True) |
| 134 |
class B: |
| 135 |
x: int = 17 |
| 136 |
y: int # E: Non-default attributes not allowed after default attributes. |
| 137 |
@attr.s(auto_attribs=True) |
| 138 |
class C: |
| 139 |
x: int = attr.ib(default=17) |
| 140 |
y: int # E: Non-default attributes not allowed after default attributes. |
| 141 |
@attr.s |
| 142 |
class D: |
| 143 |
x = attr.ib() |
| 144 |
y = attr.ib() # E: Non-default attributes not allowed after default attributes. |
| 145 |
|
| 146 |
@x.default |
| 147 |
def foo(self): |
| 148 |
return 17 |
| 149 |
[builtins fixtures/bool.pyi] |
| 150 |
|
| 151 |
[case testAttrsNotBooleans] |
| 152 |
import attr |
| 153 |
x = True |
| 154 |
@attr.s(cmp=x) # E: "cmp" argument must be a True, False, or None literal |
| 155 |
class A: |
| 156 |
a = attr.ib(init=x) # E: "init" argument must be a True or False literal |
| 157 |
[builtins fixtures/bool.pyi] |
| 158 |
|
| 159 |
[case testAttrsInitFalse] |
| 160 |
from attr import attrib, attrs |
| 161 |
@attrs(auto_attribs=True, init=False) |
| 162 |
class A: |
| 163 |
a: int |
| 164 |
_b: int |
| 165 |
c: int = 18 |
| 166 |
_d: int = attrib(validator=None, default=18) |
| 167 |
reveal_type(A) # N: Revealed type is "def () -> __main__.A" |
| 168 |
A() |
| 169 |
A(1, [2]) # E: Too many arguments for "A" |
| 170 |
A(1, [2], '3', 4) # E: Too many arguments for "A" |
| 171 |
[builtins fixtures/list.pyi] |
| 172 |
|
| 173 |
[case testAttrsInitAttribFalse] |
| 174 |
from attr import attrib, attrs |
| 175 |
@attrs |
| 176 |
class A: |
| 177 |
a = attrib(init=False) |
| 178 |
b = attrib() |
| 179 |
reveal_type(A) # N: Revealed type is "def (b: Any) -> __main__.A" |
| 180 |
[builtins fixtures/bool.pyi] |
| 181 |
|
| 182 |
[case testAttrsCmpTrue] |
| 183 |
from attr import attrib, attrs |
| 184 |
@attrs(auto_attribs=True) |
| 185 |
class A: |
| 186 |
a: int |
| 187 |
reveal_type(A) # N: Revealed type is "def (a: builtins.int) -> __main__.A" |
| 188 |
reveal_type(A.__lt__) # N: Revealed type is "def [_AT] (self: _AT`3, other: _AT`3) -> builtins.bool" |
| 189 |
reveal_type(A.__le__) # N: Revealed type is "def [_AT] (self: _AT`4, other: _AT`4) -> builtins.bool" |
| 190 |
reveal_type(A.__gt__) # N: Revealed type is "def [_AT] (self: _AT`5, other: _AT`5) -> builtins.bool" |
| 191 |
reveal_type(A.__ge__) # N: Revealed type is "def [_AT] (self: _AT`6, other: _AT`6) -> builtins.bool" |
| 192 |
|
| 193 |
A(1) < A(2) |
| 194 |
A(1) <= A(2) |
| 195 |
A(1) > A(2) |
| 196 |
A(1) >= A(2) |
| 197 |
A(1) == A(2) |
| 198 |
A(1) != A(2) |
| 199 |
|
| 200 |
A(1) < 1 # E: Unsupported operand types for < ("A" and "int") |
| 201 |
A(1) <= 1 # E: Unsupported operand types for <= ("A" and "int") |
| 202 |
A(1) > 1 # E: Unsupported operand types for > ("A" and "int") |
| 203 |
A(1) >= 1 # E: Unsupported operand types for >= ("A" and "int") |
| 204 |
A(1) == 1 |
| 205 |
A(1) != 1 |
| 206 |
|
| 207 |
1 < A(1) # E: Unsupported operand types for > ("A" and "int") |
| 208 |
1 <= A(1) # E: Unsupported operand types for >= ("A" and "int") |
| 209 |
1 > A(1) # E: Unsupported operand types for < ("A" and "int") |
| 210 |
1 >= A(1) # E: Unsupported operand types for <= ("A" and "int") |
| 211 |
1 == A(1) |
| 212 |
1 != A(1) |
| 213 |
[builtins fixtures/plugin_attrs.pyi] |
| 214 |
|
| 215 |
[case testAttrsEqFalse] |
| 216 |
from attr import attrib, attrs |
| 217 |
@attrs(auto_attribs=True, eq=False) |
| 218 |
class A: |
| 219 |
a: int |
| 220 |
reveal_type(A) # N: Revealed type is "def (a: builtins.int) -> __main__.A" |
| 221 |
reveal_type(A.__eq__) # N: Revealed type is "def (builtins.object, builtins.object) -> builtins.bool" |
| 222 |
reveal_type(A.__ne__) # N: Revealed type is "def (builtins.object, builtins.object) -> builtins.bool" |
| 223 |
|
| 224 |
A(1) < A(2) # E: Unsupported left operand type for < ("A") |
| 225 |
A(1) <= A(2) # E: Unsupported left operand type for <= ("A") |
| 226 |
A(1) > A(2) # E: Unsupported left operand type for > ("A") |
| 227 |
A(1) >= A(2) # E: Unsupported left operand type for >= ("A") |
| 228 |
A(1) == A(2) |
| 229 |
A(1) != A(2) |
| 230 |
|
| 231 |
A(1) < 1 # E: Unsupported left operand type for < ("A") |
| 232 |
A(1) <= 1 # E: Unsupported left operand type for <= ("A") |
| 233 |
A(1) > 1 # E: Unsupported left operand type for > ("A") |
| 234 |
A(1) >= 1 # E: Unsupported left operand type for >= ("A") |
| 235 |
A(1) == 1 |
| 236 |
A(1) != 1 |
| 237 |
|
| 238 |
1 < A(1) # E: Unsupported left operand type for < ("int") |
| 239 |
1 <= A(1) # E: Unsupported left operand type for <= ("int") |
| 240 |
1 > A(1) # E: Unsupported left operand type for > ("int") |
| 241 |
1 >= A(1) # E: Unsupported left operand type for >= ("int") |
| 242 |
1 == A(1) |
| 243 |
1 != A(1) |
| 244 |
[builtins fixtures/plugin_attrs.pyi] |
| 245 |
|
| 246 |
[case testAttrsOrderFalse] |
| 247 |
from attr import attrib, attrs |
| 248 |
@attrs(auto_attribs=True, order=False) |
| 249 |
class A: |
| 250 |
a: int |
| 251 |
reveal_type(A) # N: Revealed type is "def (a: builtins.int) -> __main__.A" |
| 252 |
|
| 253 |
A(1) < A(2) # E: Unsupported left operand type for < ("A") |
| 254 |
A(1) <= A(2) # E: Unsupported left operand type for <= ("A") |
| 255 |
A(1) > A(2) # E: Unsupported left operand type for > ("A") |
| 256 |
A(1) >= A(2) # E: Unsupported left operand type for >= ("A") |
| 257 |
A(1) == A(2) |
| 258 |
A(1) != A(2) |
| 259 |
|
| 260 |
A(1) < 1 # E: Unsupported left operand type for < ("A") |
| 261 |
A(1) <= 1 # E: Unsupported left operand type for <= ("A") |
| 262 |
A(1) > 1 # E: Unsupported left operand type for > ("A") |
| 263 |
A(1) >= 1 # E: Unsupported left operand type for >= ("A") |
| 264 |
A(1) == 1 |
| 265 |
A(1) != 1 |
| 266 |
|
| 267 |
1 < A(1) # E: Unsupported left operand type for < ("int") |
| 268 |
1 <= A(1) # E: Unsupported left operand type for <= ("int") |
| 269 |
1 > A(1) # E: Unsupported left operand type for > ("int") |
| 270 |
1 >= A(1) # E: Unsupported left operand type for >= ("int") |
| 271 |
1 == A(1) |
| 272 |
1 != A(1) |
| 273 |
[builtins fixtures/plugin_attrs.pyi] |
| 274 |
|
| 275 |
[case testAttrsCmpEqOrderValues] |
| 276 |
from attr import attrib, attrs |
| 277 |
@attrs(cmp=True) |
| 278 |
class DeprecatedTrue: |
| 279 |
... |
| 280 |
|
| 281 |
@attrs(cmp=False) |
| 282 |
class DeprecatedFalse: |
| 283 |
... |
| 284 |
|
| 285 |
@attrs(cmp=False, eq=True) # E: Don't mix "cmp" with "eq" and "order" |
| 286 |
class Mixed: |
| 287 |
... |
| 288 |
|
| 289 |
@attrs(order=True, eq=False) # E: eq must be True if order is True |
| 290 |
class Confused: |
| 291 |
... |
| 292 |
[builtins fixtures/plugin_attrs.pyi] |
| 293 |
|
| 294 |
|
| 295 |
[case testAttrsInheritance] |
| 296 |
import attr |
| 297 |
@attr.s |
| 298 |
class A: |
| 299 |
a: int = attr.ib() |
| 300 |
@attr.s |
| 301 |
class B: |
| 302 |
b: str = attr.ib() |
| 303 |
@attr.s |
| 304 |
class C(A, B): |
| 305 |
c: bool = attr.ib() |
| 306 |
reveal_type(C) # N: Revealed type is "def (a: builtins.int, b: builtins.str, c: builtins.bool) -> __main__.C" |
| 307 |
[builtins fixtures/bool.pyi] |
| 308 |
|
| 309 |
[case testAttrsNestedInClasses] |
| 310 |
import attr |
| 311 |
@attr.s |
| 312 |
class C: |
| 313 |
y = attr.ib() |
| 314 |
@attr.s |
| 315 |
class D: |
| 316 |
x: int = attr.ib() |
| 317 |
reveal_type(C) # N: Revealed type is "def (y: Any) -> __main__.C" |
| 318 |
reveal_type(C.D) # N: Revealed type is "def (x: builtins.int) -> __main__.C.D" |
| 319 |
[builtins fixtures/bool.pyi] |
| 320 |
|
| 321 |
[case testAttrsInheritanceOverride] |
| 322 |
import attr |
| 323 |
|
| 324 |
@attr.s |
| 325 |
class A: |
| 326 |
a: int = attr.ib() |
| 327 |
x: int = attr.ib() |
| 328 |
|
| 329 |
@attr.s |
| 330 |
class B(A): |
| 331 |
b: str = attr.ib() |
| 332 |
x: int = attr.ib(default=22) |
| 333 |
|
| 334 |
@attr.s |
| 335 |
class C(B): |
| 336 |
c: bool = attr.ib() # No error here because the x below overwrites the x above. |
| 337 |
x: int = attr.ib() |
| 338 |
|
| 339 |
reveal_type(A) # N: Revealed type is "def (a: builtins.int, x: builtins.int) -> __main__.A" |
| 340 |
reveal_type(B) # N: Revealed type is "def (a: builtins.int, b: builtins.str, x: builtins.int =) -> __main__.B" |
| 341 |
reveal_type(C) # N: Revealed type is "def (a: builtins.int, b: builtins.str, c: builtins.bool, x: builtins.int) -> __main__.C" |
| 342 |
[builtins fixtures/bool.pyi] |
| 343 |
|
| 344 |
[case testAttrsTypeEquals] |
| 345 |
import attr |
| 346 |
|
| 347 |
@attr.s |
| 348 |
class A: |
| 349 |
a = attr.ib(type=int) |
| 350 |
b = attr.ib(18, type=int) |
| 351 |
reveal_type(A) # N: Revealed type is "def (a: builtins.int, b: builtins.int =) -> __main__.A" |
| 352 |
[builtins fixtures/bool.pyi] |
| 353 |
|
| 354 |
[case testAttrsFrozen] |
| 355 |
import attr |
| 356 |
|
| 357 |
@attr.s(frozen=True) |
| 358 |
class A: |
| 359 |
a = attr.ib() |
| 360 |
|
| 361 |
a = A(5) |
| 362 |
a.a = 16 # E: Property "a" defined in "A" is read-only |
| 363 |
[builtins fixtures/plugin_attrs.pyi] |
| 364 |
|
| 365 |
[case testAttrsNextGenFrozen] |
| 366 |
from attr import frozen, field |
| 367 |
|
| 368 |
@frozen |
| 369 |
class A: |
| 370 |
a = field() |
| 371 |
|
| 372 |
a = A(5) |
| 373 |
a.a = 16 # E: Property "a" defined in "A" is read-only |
| 374 |
[builtins fixtures/plugin_attrs.pyi] |
| 375 |
|
| 376 |
[case testAttrsNextGenDetect] |
| 377 |
from attr import define, field |
| 378 |
|
| 379 |
@define |
| 380 |
class A: |
| 381 |
a = field() |
| 382 |
|
| 383 |
@define |
| 384 |
class B: |
| 385 |
a: int |
| 386 |
|
| 387 |
@define |
| 388 |
class C: |
| 389 |
a: int = field() |
| 390 |
b = field() |
| 391 |
|
| 392 |
@define |
| 393 |
class D: |
| 394 |
a: int |
| 395 |
b = field() |
| 396 |
|
| 397 |
reveal_type(A) # N: Revealed type is "def (a: Any) -> __main__.A" |
| 398 |
reveal_type(B) # N: Revealed type is "def (a: builtins.int) -> __main__.B" |
| 399 |
reveal_type(C) # N: Revealed type is "def (a: builtins.int, b: Any) -> __main__.C" |
| 400 |
reveal_type(D) # N: Revealed type is "def (b: Any) -> __main__.D" |
| 401 |
|
| 402 |
[builtins fixtures/bool.pyi] |
| 403 |
|
| 404 |
[case testAttrsOldPackage] |
| 405 |
import attr |
| 406 |
@attr.s(auto_attribs=True) |
| 407 |
class A: |
| 408 |
a: int = attr.ib() |
| 409 |
b: bool |
| 410 |
|
| 411 |
@attr.s(auto_attribs=True, frozen=True) |
| 412 |
class B: |
| 413 |
a: bool |
| 414 |
b: int |
| 415 |
|
| 416 |
@attr.s |
| 417 |
class C: |
| 418 |
a = attr.ib(type=int) |
| 419 |
|
| 420 |
reveal_type(A) # N: Revealed type is "def (a: builtins.int, b: builtins.bool) -> __main__.A" |
| 421 |
reveal_type(B) # N: Revealed type is "def (a: builtins.bool, b: builtins.int) -> __main__.B" |
| 422 |
reveal_type(C) # N: Revealed type is "def (a: builtins.int) -> __main__.C" |
| 423 |
|
| 424 |
[builtins fixtures/plugin_attrs.pyi] |
| 425 |
|
| 426 |
[case testAttrsDataClass] |
| 427 |
import attr |
| 428 |
from typing import List, ClassVar |
| 429 |
@attr.dataclass |
| 430 |
class A: |
| 431 |
a: int |
| 432 |
_b: List[str] |
| 433 |
c: str = '18' |
| 434 |
_d: int = attr.ib(validator=None, default=18) |
| 435 |
E = 7 |
| 436 |
F: ClassVar[int] = 22 |
| 437 |
reveal_type(A) # N: Revealed type is "def (a: builtins.int, b: builtins.list[builtins.str], c: builtins.str =, d: builtins.int =) -> __main__.A" |
| 438 |
A(1, ['2']) |
| 439 |
[builtins fixtures/list.pyi] |
| 440 |
|
| 441 |
[case testAttrsTypeAlias] |
| 442 |
from typing import List |
| 443 |
import attr |
| 444 |
Alias = List[int] |
| 445 |
@attr.s(auto_attribs=True) |
| 446 |
class A: |
| 447 |
Alias2 = List[str] |
| 448 |
x: Alias |
| 449 |
y: Alias2 = attr.ib() |
| 450 |
reveal_type(A) # N: Revealed type is "def (x: builtins.list[builtins.int], y: builtins.list[builtins.str]) -> __main__.A" |
| 451 |
[builtins fixtures/list.pyi] |
| 452 |
|
| 453 |
[case testAttrsGeneric] |
| 454 |
from typing import TypeVar, Generic, List |
| 455 |
import attr |
| 456 |
T = TypeVar('T') |
| 457 |
@attr.s(auto_attribs=True) |
| 458 |
class A(Generic[T]): |
| 459 |
x: List[T] |
| 460 |
y: T = attr.ib() |
| 461 |
def foo(self) -> List[T]: |
| 462 |
return [self.y] |
| 463 |
def bar(self) -> T: |
| 464 |
return self.x[0] |
| 465 |
def problem(self) -> T: |
| 466 |
return self.x # E: Incompatible return value type (got "list[T]", expected "T") |
| 467 |
reveal_type(A) # N: Revealed type is "def [T] (x: builtins.list[T`1], y: T`1) -> __main__.A[T`1]" |
| 468 |
a = A([1], 2) |
| 469 |
reveal_type(a) # N: Revealed type is "__main__.A[builtins.int]" |
| 470 |
reveal_type(a.x) # N: Revealed type is "builtins.list[builtins.int]" |
| 471 |
reveal_type(a.y) # N: Revealed type is "builtins.int" |
| 472 |
|
| 473 |
A(['str'], 7) # E: Cannot infer value of type parameter "T" of "A" |
| 474 |
A([1], '2') # E: Cannot infer value of type parameter "T" of "A" |
| 475 |
|
| 476 |
[builtins fixtures/list.pyi] |
| 477 |
|
| 478 |
[case testAttrsGenericWithConverter] |
| 479 |
from typing import TypeVar, Generic, List, Iterable, Iterator, Callable |
| 480 |
import attr |
| 481 |
T = TypeVar('T') |
| 482 |
|
| 483 |
def int_gen() -> Iterator[int]: |
| 484 |
yield 1 |
| 485 |
|
| 486 |
def list_converter(x: Iterable[T]) -> List[T]: |
| 487 |
return list(x) |
| 488 |
|
| 489 |
@attr.s(auto_attribs=True) |
| 490 |
class A(Generic[T]): |
| 491 |
x: List[T] = attr.ib(converter=list_converter) |
| 492 |
y: T = attr.ib() |
| 493 |
def foo(self) -> List[T]: |
| 494 |
return [self.y] |
| 495 |
def bar(self) -> T: |
| 496 |
return self.x[0] |
| 497 |
def problem(self) -> T: |
| 498 |
return self.x # E: Incompatible return value type (got "list[T]", expected "T") |
| 499 |
reveal_type(A) # N: Revealed type is "def [T] (x: typing.Iterable[T`1], y: T`1) -> __main__.A[T`1]" |
| 500 |
a1 = A([1], 2) |
| 501 |
reveal_type(a1) # N: Revealed type is "__main__.A[builtins.int]" |
| 502 |
reveal_type(a1.x) # N: Revealed type is "builtins.list[builtins.int]" |
| 503 |
reveal_type(a1.y) # N: Revealed type is "builtins.int" |
| 504 |
|
| 505 |
a2 = A(int_gen(), 2) |
| 506 |
reveal_type(a2) # N: Revealed type is "__main__.A[builtins.int]" |
| 507 |
reveal_type(a2.x) # N: Revealed type is "builtins.list[builtins.int]" |
| 508 |
reveal_type(a2.y) # N: Revealed type is "builtins.int" |
| 509 |
|
| 510 |
|
| 511 |
def get_int() -> int: |
| 512 |
return 1 |
| 513 |
|
| 514 |
class Other(Generic[T]): |
| 515 |
def __init__(self, x: T) -> None: |
| 516 |
pass |
| 517 |
|
| 518 |
@attr.s(auto_attribs=True) |
| 519 |
class B(Generic[T]): |
| 520 |
x: Other[Callable[..., T]] = attr.ib(converter=Other[Callable[..., T]]) |
| 521 |
|
| 522 |
b1 = B(get_int) |
| 523 |
reveal_type(b1) # N: Revealed type is "__main__.B[builtins.int]" |
| 524 |
reveal_type(b1.x) # N: Revealed type is "__main__.Other[def (*Any, **Any) -> builtins.int]" |
| 525 |
|
| 526 |
[builtins fixtures/list.pyi] |
| 527 |
|
| 528 |
|
| 529 |
[case testAttrsUntypedGenericInheritance] |
| 530 |
from typing import Generic, TypeVar |
| 531 |
import attr |
| 532 |
|
| 533 |
T = TypeVar("T") |
| 534 |
|
| 535 |
@attr.s(auto_attribs=True) |
| 536 |
class Base(Generic[T]): |
| 537 |
attr: T |
| 538 |
|
| 539 |
@attr.s(auto_attribs=True) |
| 540 |
class Sub(Base): |
| 541 |
pass |
| 542 |
|
| 543 |
sub = Sub(attr=1) |
| 544 |
reveal_type(sub) # N: Revealed type is "__main__.Sub" |
| 545 |
reveal_type(sub.attr) # N: Revealed type is "Any" |
| 546 |
|
| 547 |
[builtins fixtures/bool.pyi] |
| 548 |
|
| 549 |
|
| 550 |
[case testAttrsGenericInheritance] |
| 551 |
from typing import Generic, TypeVar |
| 552 |
import attr |
| 553 |
|
| 554 |
S = TypeVar("S") |
| 555 |
T = TypeVar("T") |
| 556 |
|
| 557 |
@attr.s(auto_attribs=True) |
| 558 |
class Base(Generic[T]): |
| 559 |
attr: T |
| 560 |
|
| 561 |
@attr.s(auto_attribs=True) |
| 562 |
class Sub(Base[S]): |
| 563 |
pass |
| 564 |
|
| 565 |
sub_int = Sub[int](attr=1) |
| 566 |
reveal_type(sub_int) # N: Revealed type is "__main__.Sub[builtins.int]" |
| 567 |
reveal_type(sub_int.attr) # N: Revealed type is "builtins.int" |
| 568 |
|
| 569 |
sub_str = Sub[str](attr='ok') |
| 570 |
reveal_type(sub_str) # N: Revealed type is "__main__.Sub[builtins.str]" |
| 571 |
reveal_type(sub_str.attr) # N: Revealed type is "builtins.str" |
| 572 |
|
| 573 |
[builtins fixtures/bool.pyi] |
| 574 |
|
| 575 |
|
| 576 |
[case testAttrsGenericInheritance2] |
| 577 |
from typing import Generic, TypeVar |
| 578 |
import attr |
| 579 |
|
| 580 |
T1 = TypeVar("T1") |
| 581 |
T2 = TypeVar("T2") |
| 582 |
T3 = TypeVar("T3") |
| 583 |
|
| 584 |
@attr.s(auto_attribs=True) |
| 585 |
class Base(Generic[T1, T2, T3]): |
| 586 |
one: T1 |
| 587 |
two: T2 |
| 588 |
three: T3 |
| 589 |
|
| 590 |
@attr.s(auto_attribs=True) |
| 591 |
class Sub(Base[int, str, float]): |
| 592 |
pass |
| 593 |
|
| 594 |
sub = Sub(one=1, two='ok', three=3.14) |
| 595 |
reveal_type(sub) # N: Revealed type is "__main__.Sub" |
| 596 |
reveal_type(sub.one) # N: Revealed type is "builtins.int" |
| 597 |
reveal_type(sub.two) # N: Revealed type is "builtins.str" |
| 598 |
reveal_type(sub.three) # N: Revealed type is "builtins.float" |
| 599 |
|
| 600 |
[builtins fixtures/bool.pyi] |
| 601 |
|
| 602 |
|
| 603 |
[case testAttrsGenericInheritance3] |
| 604 |
import attr |
| 605 |
from typing import Any, Callable, Generic, TypeVar, List |
| 606 |
|
| 607 |
T = TypeVar("T") |
| 608 |
S = TypeVar("S") |
| 609 |
|
| 610 |
@attr.s(auto_attribs=True) |
| 611 |
class Parent(Generic[T]): |
| 612 |
f: Callable[[T], Any] |
| 613 |
|
| 614 |
@attr.s(auto_attribs=True) |
| 615 |
class Child(Parent[T]): ... |
| 616 |
|
| 617 |
class A: ... |
| 618 |
def func(obj: A) -> bool: ... |
| 619 |
|
| 620 |
reveal_type(Child[A](func).f) # N: Revealed type is "def (__main__.A) -> Any" |
| 621 |
|
| 622 |
@attr.s(auto_attribs=True) |
| 623 |
class Parent2(Generic[T]): |
| 624 |
a: List[T] |
| 625 |
|
| 626 |
@attr.s(auto_attribs=True) |
| 627 |
class Child2(Generic[T, S], Parent2[S]): |
| 628 |
b: List[T] |
| 629 |
|
| 630 |
reveal_type(Child2([A()], [1]).a) # N: Revealed type is "builtins.list[__main__.A]" |
| 631 |
reveal_type(Child2[int, A]([A()], [1]).b) # N: Revealed type is "builtins.list[builtins.int]" |
| 632 |
[builtins fixtures/list.pyi] |
| 633 |
|
| 634 |
[case testAttrsMultiGenericInheritance] |
| 635 |
from typing import Generic, TypeVar |
| 636 |
import attr |
| 637 |
|
| 638 |
T = TypeVar("T") |
| 639 |
|
| 640 |
@attr.s(auto_attribs=True, eq=False) |
| 641 |
class Base(Generic[T]): |
| 642 |
base_attr: T |
| 643 |
|
| 644 |
S = TypeVar("S") |
| 645 |
|
| 646 |
@attr.s(auto_attribs=True, eq=False) |
| 647 |
class Middle(Base[int], Generic[S]): |
| 648 |
middle_attr: S |
| 649 |
|
| 650 |
@attr.s(auto_attribs=True, eq=False) |
| 651 |
class Sub(Middle[str]): |
| 652 |
pass |
| 653 |
|
| 654 |
sub = Sub(base_attr=1, middle_attr='ok') |
| 655 |
reveal_type(sub) # N: Revealed type is "__main__.Sub" |
| 656 |
reveal_type(sub.base_attr) # N: Revealed type is "builtins.int" |
| 657 |
reveal_type(sub.middle_attr) # N: Revealed type is "builtins.str" |
| 658 |
|
| 659 |
[builtins fixtures/bool.pyi] |
| 660 |
|
| 661 |
|
| 662 |
[case testAttrsGenericClassmethod] |
| 663 |
from typing import TypeVar, Generic, Optional |
| 664 |
import attr |
| 665 |
T = TypeVar('T') |
| 666 |
@attr.s(auto_attribs=True) |
| 667 |
class A(Generic[T]): |
| 668 |
x: Optional[T] |
| 669 |
@classmethod |
| 670 |
def clsmeth(cls) -> None: |
| 671 |
reveal_type(cls) # N: Revealed type is "type[__main__.A[T`1]]" |
| 672 |
|
| 673 |
[builtins fixtures/classmethod.pyi] |
| 674 |
|
| 675 |
[case testAttrsForwardReference] |
| 676 |
# flags: --no-strict-optional |
| 677 |
import attr |
| 678 |
@attr.s(auto_attribs=True) |
| 679 |
class A: |
| 680 |
parent: 'B' |
| 681 |
|
| 682 |
@attr.s(auto_attribs=True) |
| 683 |
class B: |
| 684 |
parent: A |
| 685 |
|
| 686 |
reveal_type(A) # N: Revealed type is "def (parent: __main__.B) -> __main__.A" |
| 687 |
reveal_type(B) # N: Revealed type is "def (parent: __main__.A) -> __main__.B" |
| 688 |
A(B(None)) |
| 689 |
[builtins fixtures/list.pyi] |
| 690 |
|
| 691 |
[case testAttrsForwardReferenceInClass] |
| 692 |
# flags: --no-strict-optional |
| 693 |
import attr |
| 694 |
@attr.s(auto_attribs=True) |
| 695 |
class A: |
| 696 |
parent: A.B |
| 697 |
|
| 698 |
@attr.s(auto_attribs=True) |
| 699 |
class B: |
| 700 |
parent: A |
| 701 |
|
| 702 |
reveal_type(A) # N: Revealed type is "def (parent: __main__.A.B) -> __main__.A" |
| 703 |
reveal_type(A.B) # N: Revealed type is "def (parent: __main__.A) -> __main__.A.B" |
| 704 |
A(A.B(None)) |
| 705 |
[builtins fixtures/list.pyi] |
| 706 |
|
| 707 |
[case testAttrsImporting] |
| 708 |
from helper import A |
| 709 |
reveal_type(A) # N: Revealed type is "def (a: builtins.int, b: builtins.str) -> helper.A" |
| 710 |
[file helper.py] |
| 711 |
import attr |
| 712 |
@attr.s(auto_attribs=True) |
| 713 |
class A: |
| 714 |
a: int |
| 715 |
b: str = attr.ib() |
| 716 |
[builtins fixtures/list.pyi] |
| 717 |
|
| 718 |
[case testAttrsOtherMethods] |
| 719 |
import attr |
| 720 |
@attr.s(auto_attribs=True) |
| 721 |
class A: |
| 722 |
a: int |
| 723 |
b: str = attr.ib() |
| 724 |
@classmethod |
| 725 |
def new(cls) -> A: |
| 726 |
reveal_type(cls) # N: Revealed type is "type[__main__.A]" |
| 727 |
return cls(6, 'hello') |
| 728 |
@classmethod |
| 729 |
def bad(cls) -> A: |
| 730 |
return cls(17) # E: Missing positional argument "b" in call to "A" |
| 731 |
def foo(self) -> int: |
| 732 |
return self.a |
| 733 |
reveal_type(A) # N: Revealed type is "def (a: builtins.int, b: builtins.str) -> __main__.A" |
| 734 |
a = A.new() |
| 735 |
reveal_type(a.foo) # N: Revealed type is "def () -> builtins.int" |
| 736 |
[builtins fixtures/classmethod.pyi] |
| 737 |
|
| 738 |
[case testAttrsOtherOverloads] |
| 739 |
import attr |
| 740 |
from typing import overload, Union |
| 741 |
|
| 742 |
@attr.s |
| 743 |
class A: |
| 744 |
a = attr.ib() |
| 745 |
b = attr.ib(default=3) |
| 746 |
|
| 747 |
@classmethod |
| 748 |
def other(cls) -> str: |
| 749 |
return "..." |
| 750 |
|
| 751 |
@overload |
| 752 |
@classmethod |
| 753 |
def foo(cls, x: int) -> int: ... |
| 754 |
|
| 755 |
@overload |
| 756 |
@classmethod |
| 757 |
def foo(cls, x: str) -> str: ... |
| 758 |
|
| 759 |
@classmethod |
| 760 |
def foo(cls, x: Union[int, str]) -> Union[int, str]: |
| 761 |
reveal_type(cls) # N: Revealed type is "type[__main__.A]" |
| 762 |
reveal_type(cls.other()) # N: Revealed type is "builtins.str" |
| 763 |
return x |
| 764 |
|
| 765 |
reveal_type(A.foo(3)) # N: Revealed type is "builtins.int" |
| 766 |
reveal_type(A.foo("foo")) # N: Revealed type is "builtins.str" |
| 767 |
|
| 768 |
[builtins fixtures/classmethod.pyi] |
| 769 |
|
| 770 |
[case testAttrsDefaultDecorator] |
| 771 |
import attr |
| 772 |
@attr.s |
| 773 |
class C(object): |
| 774 |
x: int = attr.ib(default=1) |
| 775 |
y: int = attr.ib() |
| 776 |
@y.default |
| 777 |
def name_does_not_matter(self): |
| 778 |
return self.x + 1 |
| 779 |
C() |
| 780 |
[builtins fixtures/list.pyi] |
| 781 |
|
| 782 |
[case testAttrsValidatorDecorator] |
| 783 |
import attr |
| 784 |
@attr.s |
| 785 |
class C(object): |
| 786 |
x = attr.ib() |
| 787 |
@x.validator |
| 788 |
def check(self, attribute, value): |
| 789 |
if value > 42: |
| 790 |
raise ValueError("x must be smaller or equal to 42") |
| 791 |
C(42) |
| 792 |
C(43) |
| 793 |
[builtins fixtures/exception.pyi] |
| 794 |
|
| 795 |
[case testAttrsLocalVariablesInClassMethod] |
| 796 |
import attr |
| 797 |
@attr.s(auto_attribs=True) |
| 798 |
class A: |
| 799 |
a: int |
| 800 |
b: int = attr.ib() |
| 801 |
@classmethod |
| 802 |
def new(cls, foo: int) -> A: |
| 803 |
a = foo |
| 804 |
b = a |
| 805 |
return cls(a, b) |
| 806 |
[builtins fixtures/classmethod.pyi] |
| 807 |
|
| 808 |
[case testAttrsUnionForward] |
| 809 |
import attr |
| 810 |
from typing import Union, List |
| 811 |
|
| 812 |
@attr.s(auto_attribs=True) |
| 813 |
class A: |
| 814 |
frob: List['AOrB'] |
| 815 |
|
| 816 |
class B: |
| 817 |
pass |
| 818 |
|
| 819 |
AOrB = Union[A, B] |
| 820 |
|
| 821 |
reveal_type(A) # N: Revealed type is "def (frob: builtins.list[__main__.A | __main__.B]) -> __main__.A" |
| 822 |
reveal_type(B) # N: Revealed type is "def () -> __main__.B" |
| 823 |
|
| 824 |
A([B()]) |
| 825 |
[builtins fixtures/list.pyi] |
| 826 |
|
| 827 |
[case testAttrsUsingConvert] |
| 828 |
import attr |
| 829 |
|
| 830 |
def convert(s:int) -> str: |
| 831 |
return 'hello' |
| 832 |
|
| 833 |
@attr.s |
| 834 |
class C: |
| 835 |
x: str = attr.ib(convert=convert) # E: convert is deprecated, use converter |
| 836 |
|
| 837 |
# Because of the convert the __init__ takes an int, but the variable is a str. |
| 838 |
reveal_type(C) # N: Revealed type is "def (x: builtins.int) -> __main__.C" |
| 839 |
reveal_type(C(15).x) # N: Revealed type is "builtins.str" |
| 840 |
[builtins fixtures/list.pyi] |
| 841 |
|
| 842 |
[case testAttrsUsingConverter] |
| 843 |
import attr |
| 844 |
import helper |
| 845 |
|
| 846 |
def converter2(s:int) -> str: |
| 847 |
return 'hello' |
| 848 |
|
| 849 |
@attr.s |
| 850 |
class C: |
| 851 |
x: str = attr.ib(converter=helper.converter) |
| 852 |
y: str = attr.ib(converter=converter2) |
| 853 |
|
| 854 |
# Because of the converter the __init__ takes an int, but the variable is a str. |
| 855 |
reveal_type(C) # N: Revealed type is "def (x: builtins.int, y: builtins.int) -> __main__.C" |
| 856 |
reveal_type(C(15, 16).x) # N: Revealed type is "builtins.str" |
| 857 |
[file helper.py] |
| 858 |
def converter(s:int) -> str: |
| 859 |
return 'hello' |
| 860 |
[builtins fixtures/list.pyi] |
| 861 |
|
| 862 |
[case testAttrsUsingConvertAndConverter] |
| 863 |
import attr |
| 864 |
|
| 865 |
def converter(s:int) -> str: |
| 866 |
return 'hello' |
| 867 |
|
| 868 |
@attr.s |
| 869 |
class C: |
| 870 |
x: str = attr.ib(converter=converter, convert=converter) # E: Can't pass both "convert" and "converter". |
| 871 |
|
| 872 |
[builtins fixtures/list.pyi] |
| 873 |
|
| 874 |
[case testAttrsUsingBadConverter] |
| 875 |
# flags: --no-strict-optional |
| 876 |
import attr |
| 877 |
from typing import overload |
| 878 |
@overload |
| 879 |
def bad_overloaded_converter(x: int, y: int) -> int: |
| 880 |
... |
| 881 |
@overload |
| 882 |
def bad_overloaded_converter(x: str, y: str) -> str: |
| 883 |
... |
| 884 |
def bad_overloaded_converter(x, y=7): |
| 885 |
return x |
| 886 |
def bad_converter() -> str: |
| 887 |
return '' |
| 888 |
@attr.dataclass |
| 889 |
class A: |
| 890 |
bad: str = attr.ib(converter=bad_converter) |
| 891 |
bad_overloaded: int = attr.ib(converter=bad_overloaded_converter) |
| 892 |
reveal_type(A) |
| 893 |
[out] |
| 894 |
main:16: error: Cannot determine __init__ type from converter |
| 895 |
main:16: error: Argument "converter" has incompatible type "Callable[[], str]"; expected "Callable[[Any], str]" |
| 896 |
main:17: error: Cannot determine __init__ type from converter |
| 897 |
main:17: error: Argument "converter" has incompatible type overloaded function; expected "Callable[[Any], int]" |
| 898 |
main:18: note: Revealed type is "def (bad: Any, bad_overloaded: Any) -> __main__.A" |
| 899 |
[builtins fixtures/list.pyi] |
| 900 |
|
| 901 |
[case testAttrsUsingBadConverterReprocess] |
| 902 |
# flags: --no-strict-optional |
| 903 |
import attr |
| 904 |
from typing import overload |
| 905 |
forward: 'A' |
| 906 |
@overload |
| 907 |
def bad_overloaded_converter(x: int, y: int) -> int: |
| 908 |
... |
| 909 |
@overload |
| 910 |
def bad_overloaded_converter(x: str, y: str) -> str: |
| 911 |
... |
| 912 |
def bad_overloaded_converter(x, y=7): |
| 913 |
return x |
| 914 |
def bad_converter() -> str: |
| 915 |
return '' |
| 916 |
@attr.dataclass |
| 917 |
class A: |
| 918 |
bad: str = attr.ib(converter=bad_converter) |
| 919 |
bad_overloaded: int = attr.ib(converter=bad_overloaded_converter) |
| 920 |
reveal_type(A) |
| 921 |
[out] |
| 922 |
main:17: error: Cannot determine __init__ type from converter |
| 923 |
main:17: error: Argument "converter" has incompatible type "Callable[[], str]"; expected "Callable[[Any], str]" |
| 924 |
main:18: error: Cannot determine __init__ type from converter |
| 925 |
main:18: error: Argument "converter" has incompatible type overloaded function; expected "Callable[[Any], int]" |
| 926 |
main:19: note: Revealed type is "def (bad: Any, bad_overloaded: Any) -> __main__.A" |
| 927 |
[builtins fixtures/list.pyi] |
| 928 |
|
| 929 |
[case testAttrsUsingUnsupportedConverter] |
| 930 |
import attr |
| 931 |
class Thing: |
| 932 |
def do_it(self, int) -> str: |
| 933 |
... |
| 934 |
thing = Thing() |
| 935 |
def factory(default: int): |
| 936 |
... |
| 937 |
@attr.s |
| 938 |
class C: |
| 939 |
x: str = attr.ib(converter=thing.do_it) # E: Unsupported converter, only named functions, types and lambdas are currently supported |
| 940 |
y: str = attr.ib(converter=lambda x: x) |
| 941 |
z: str = attr.ib(converter=factory(8)) # E: Unsupported converter, only named functions, types and lambdas are currently supported |
| 942 |
reveal_type(C) # N: Revealed type is "def (x: Any, y: Any, z: Any) -> __main__.C" |
| 943 |
[builtins fixtures/list.pyi] |
| 944 |
|
| 945 |
[case testAttrsUsingConverterAndSubclass] |
| 946 |
import attr |
| 947 |
|
| 948 |
def converter(s:int) -> str: |
| 949 |
return 'hello' |
| 950 |
|
| 951 |
@attr.s |
| 952 |
class C: |
| 953 |
x: str = attr.ib(converter=converter) |
| 954 |
|
| 955 |
@attr.s |
| 956 |
class A(C): |
| 957 |
pass |
| 958 |
|
| 959 |
# Because of the convert the __init__ takes an int, but the variable is a str. |
| 960 |
reveal_type(A) # N: Revealed type is "def (x: builtins.int) -> __main__.A" |
| 961 |
reveal_type(A(15).x) # N: Revealed type is "builtins.str" |
| 962 |
[builtins fixtures/list.pyi] |
| 963 |
|
| 964 |
[case testAttrsUsingConverterWithTypes] |
| 965 |
from typing import overload |
| 966 |
import attr |
| 967 |
|
| 968 |
@attr.dataclass |
| 969 |
class A: |
| 970 |
x: str |
| 971 |
|
| 972 |
@attr.s |
| 973 |
class C: |
| 974 |
x: complex = attr.ib(converter=complex) |
| 975 |
y: int = attr.ib(converter=int) |
| 976 |
z: A = attr.ib(converter=A) |
| 977 |
|
| 978 |
o = C("1", "2", "3") |
| 979 |
o = C(1, 2, "3") |
| 980 |
[builtins fixtures/plugin_attrs.pyi] |
| 981 |
|
| 982 |
[case testAttrsCmpWithSubclasses] |
| 983 |
import attr |
| 984 |
@attr.s |
| 985 |
class A: pass |
| 986 |
@attr.s |
| 987 |
class B: pass |
| 988 |
@attr.s |
| 989 |
class C(A, B): pass |
| 990 |
@attr.s |
| 991 |
class D(A): pass |
| 992 |
|
| 993 |
reveal_type(A.__lt__) # N: Revealed type is "def [_AT] (self: _AT`29, other: _AT`29) -> builtins.bool" |
| 994 |
reveal_type(B.__lt__) # N: Revealed type is "def [_AT] (self: _AT`30, other: _AT`30) -> builtins.bool" |
| 995 |
reveal_type(C.__lt__) # N: Revealed type is "def [_AT] (self: _AT`31, other: _AT`31) -> builtins.bool" |
| 996 |
reveal_type(D.__lt__) # N: Revealed type is "def [_AT] (self: _AT`32, other: _AT`32) -> builtins.bool" |
| 997 |
|
| 998 |
A() < A() |
| 999 |
B() < B() |
| 1000 |
A() < B() # E: Unsupported operand types for < ("A" and "B") |
| 1001 |
|
| 1002 |
C() > A() |
| 1003 |
C() > B() |
| 1004 |
C() > C() |
| 1005 |
C() > D() # E: Unsupported operand types for > ("C" and "D") |
| 1006 |
|
| 1007 |
D() >= A() |
| 1008 |
D() >= B() # E: Unsupported operand types for >= ("D" and "B") |
| 1009 |
D() >= C() # E: Unsupported operand types for >= ("D" and "C") |
| 1010 |
D() >= D() |
| 1011 |
|
| 1012 |
A() <= 1 # E: Unsupported operand types for <= ("A" and "int") |
| 1013 |
B() <= 1 # E: Unsupported operand types for <= ("B" and "int") |
| 1014 |
C() <= 1 # E: Unsupported operand types for <= ("C" and "int") |
| 1015 |
D() <= 1 # E: Unsupported operand types for <= ("D" and "int") |
| 1016 |
|
| 1017 |
[builtins fixtures/list.pyi] |
| 1018 |
|
| 1019 |
[case testAttrsComplexSuperclass] |
| 1020 |
import attr |
| 1021 |
@attr.s |
| 1022 |
class C: |
| 1023 |
x: int = attr.ib(default=1) |
| 1024 |
y: int = attr.ib() |
| 1025 |
@y.default |
| 1026 |
def name_does_not_matter(self): |
| 1027 |
return self.x + 1 |
| 1028 |
@attr.s |
| 1029 |
class A(C): |
| 1030 |
z: int = attr.ib(default=18) |
| 1031 |
reveal_type(C) # N: Revealed type is "def (x: builtins.int =, y: builtins.int =) -> __main__.C" |
| 1032 |
reveal_type(A) # N: Revealed type is "def (x: builtins.int =, y: builtins.int =, z: builtins.int =) -> __main__.A" |
| 1033 |
[builtins fixtures/list.pyi] |
| 1034 |
|
| 1035 |
[case testAttrsMultiAssign] |
| 1036 |
import attr |
| 1037 |
@attr.s |
| 1038 |
class A: |
| 1039 |
x, y, z = attr.ib(), attr.ib(type=int), attr.ib(default=17) |
| 1040 |
reveal_type(A) # N: Revealed type is "def (x: Any, y: builtins.int, z: Any =) -> __main__.A" |
| 1041 |
[builtins fixtures/list.pyi] |
| 1042 |
|
| 1043 |
[case testAttrsMultiAssign2] |
| 1044 |
import attr |
| 1045 |
@attr.s |
| 1046 |
class A: |
| 1047 |
x = y = z = attr.ib() # E: Too many names for one attribute |
| 1048 |
[builtins fixtures/list.pyi] |
| 1049 |
|
| 1050 |
[case testAttrsPrivateInit] |
| 1051 |
import attr |
| 1052 |
@attr.s |
| 1053 |
class C(object): |
| 1054 |
_x = attr.ib(init=False, default=42) |
| 1055 |
C() |
| 1056 |
C(_x=42) # E: Unexpected keyword argument "_x" for "C" |
| 1057 |
[builtins fixtures/list.pyi] |
| 1058 |
|
| 1059 |
[case testAttrsAliasForInit] |
| 1060 |
from attrs import define, field |
| 1061 |
|
| 1062 |
@define |
| 1063 |
class C1: |
| 1064 |
_x: int = field(alias="x1") |
| 1065 |
|
| 1066 |
c1 = C1(x1=42) |
| 1067 |
reveal_type(c1._x) # N: Revealed type is "builtins.int" |
| 1068 |
c1.x1 # E: "C1" has no attribute "x1" |
| 1069 |
C1(_x=42) # E: Unexpected keyword argument "_x" for "C1" |
| 1070 |
|
| 1071 |
alias = "x2" |
| 1072 |
@define |
| 1073 |
class C2: |
| 1074 |
_x: int = field(alias=alias) # E: "alias" argument to attrs field must be a string literal |
| 1075 |
|
| 1076 |
@define |
| 1077 |
class C3: |
| 1078 |
_x: int = field(alias="_x") |
| 1079 |
|
| 1080 |
c3 = C3(_x=1) |
| 1081 |
reveal_type(c3._x) # N: Revealed type is "builtins.int" |
| 1082 |
[builtins fixtures/plugin_attrs.pyi] |
| 1083 |
|
| 1084 |
[case testAttrsAutoMustBeAll] |
| 1085 |
import attr |
| 1086 |
@attr.s(auto_attribs=True) |
| 1087 |
class A: |
| 1088 |
a: int |
| 1089 |
b = 17 |
| 1090 |
# The following forms are not allowed with auto_attribs=True |
| 1091 |
c = attr.ib() # E: Need type annotation for "c" |
| 1092 |
d, e = attr.ib(), attr.ib() # E: Need type annotation for "d" # E: Need type annotation for "e" |
| 1093 |
f = g = attr.ib() # E: Need type annotation for "f" # E: Need type annotation for "g" |
| 1094 |
[builtins fixtures/bool.pyi] |
| 1095 |
|
| 1096 |
[case testAttrsRepeatedName] |
| 1097 |
import attr |
| 1098 |
@attr.s |
| 1099 |
class A: |
| 1100 |
a = attr.ib(default=8) |
| 1101 |
b = attr.ib() |
| 1102 |
a = attr.ib() |
| 1103 |
reveal_type(A) # N: Revealed type is "def (b: Any, a: Any) -> __main__.A" |
| 1104 |
@attr.s |
| 1105 |
class B: |
| 1106 |
a: int = attr.ib(default=8) |
| 1107 |
b: int = attr.ib() |
| 1108 |
a: int = attr.ib() # E: Name "a" already defined on line 10 |
| 1109 |
reveal_type(B) # N: Revealed type is "def (b: builtins.int, a: builtins.int) -> __main__.B" |
| 1110 |
@attr.s(auto_attribs=True) |
| 1111 |
class C: |
| 1112 |
a: int = 8 |
| 1113 |
b: int |
| 1114 |
a: int = attr.ib() # E: Name "a" already defined on line 16 |
| 1115 |
reveal_type(C) # N: Revealed type is "def (a: builtins.int, b: builtins.int) -> __main__.C" |
| 1116 |
[builtins fixtures/bool.pyi] |
| 1117 |
|
| 1118 |
[case testAttrsFrozenSubclass] |
| 1119 |
import attr |
| 1120 |
|
| 1121 |
@attr.dataclass |
| 1122 |
class NonFrozenBase: |
| 1123 |
a: int |
| 1124 |
|
| 1125 |
@attr.dataclass(frozen=True) |
| 1126 |
class FrozenBase: |
| 1127 |
a: int |
| 1128 |
|
| 1129 |
@attr.dataclass(frozen=True) |
| 1130 |
class FrozenNonFrozen(NonFrozenBase): |
| 1131 |
b: int |
| 1132 |
|
| 1133 |
@attr.dataclass(frozen=True) |
| 1134 |
class FrozenFrozen(FrozenBase): |
| 1135 |
b: int |
| 1136 |
|
| 1137 |
@attr.dataclass |
| 1138 |
class NonFrozenFrozen(FrozenBase): |
| 1139 |
b: int |
| 1140 |
|
| 1141 |
# Make sure these are untouched |
| 1142 |
non_frozen_base = NonFrozenBase(1) |
| 1143 |
non_frozen_base.a = 17 |
| 1144 |
frozen_base = FrozenBase(1) |
| 1145 |
frozen_base.a = 17 # E: Property "a" defined in "FrozenBase" is read-only |
| 1146 |
|
| 1147 |
a = FrozenNonFrozen(1, 2) |
| 1148 |
a.a = 17 # E: Property "a" defined in "FrozenNonFrozen" is read-only |
| 1149 |
a.b = 17 # E: Property "b" defined in "FrozenNonFrozen" is read-only |
| 1150 |
|
| 1151 |
b = FrozenFrozen(1, 2) |
| 1152 |
b.a = 17 # E: Property "a" defined in "FrozenFrozen" is read-only |
| 1153 |
b.b = 17 # E: Property "b" defined in "FrozenFrozen" is read-only |
| 1154 |
|
| 1155 |
c = NonFrozenFrozen(1, 2) |
| 1156 |
c.a = 17 # E: Property "a" defined in "NonFrozenFrozen" is read-only |
| 1157 |
c.b = 17 # E: Property "b" defined in "NonFrozenFrozen" is read-only |
| 1158 |
|
| 1159 |
[builtins fixtures/plugin_attrs.pyi] |
| 1160 |
[case testAttrsCallableAttributes] |
| 1161 |
from typing import Callable |
| 1162 |
import attr |
| 1163 |
def blah(a: int, b: int) -> bool: |
| 1164 |
return True |
| 1165 |
|
| 1166 |
@attr.s(auto_attribs=True) |
| 1167 |
class F: |
| 1168 |
_cb: Callable[[int, int], bool] = blah |
| 1169 |
def foo(self) -> bool: |
| 1170 |
return self._cb(5, 6) |
| 1171 |
|
| 1172 |
@attr.s |
| 1173 |
class G: |
| 1174 |
_cb: Callable[[int, int], bool] = attr.ib(blah) |
| 1175 |
def foo(self) -> bool: |
| 1176 |
return self._cb(5, 6) |
| 1177 |
|
| 1178 |
@attr.s(auto_attribs=True, frozen=True) |
| 1179 |
class FFrozen(F): |
| 1180 |
def bar(self) -> bool: |
| 1181 |
return self._cb(5, 6) |
| 1182 |
[builtins fixtures/plugin_attrs.pyi] |
| 1183 |
|
| 1184 |
[case testAttrsWithFactory] |
| 1185 |
from typing import List |
| 1186 |
import attr |
| 1187 |
def my_factory() -> int: |
| 1188 |
return 7 |
| 1189 |
@attr.s |
| 1190 |
class A: |
| 1191 |
x: List[int] = attr.ib(factory=list) |
| 1192 |
y: int = attr.ib(factory=my_factory) |
| 1193 |
A() |
| 1194 |
[builtins fixtures/list.pyi] |
| 1195 |
|
| 1196 |
[case testAttrsFactoryAndDefault] |
| 1197 |
import attr |
| 1198 |
@attr.s |
| 1199 |
class A: |
| 1200 |
x: int = attr.ib(factory=int, default=7) # E: Can't pass both "default" and "factory". |
| 1201 |
[builtins fixtures/bool.pyi] |
| 1202 |
|
| 1203 |
[case testAttrsFactoryBadReturn] |
| 1204 |
import attr |
| 1205 |
def my_factory() -> int: |
| 1206 |
return 7 |
| 1207 |
@attr.s |
| 1208 |
class A: |
| 1209 |
x: int = attr.ib(factory=list) # E: Incompatible types in assignment (expression has type "list[Never]", variable has type "int") |
| 1210 |
y: str = attr.ib(factory=my_factory) # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 1211 |
[builtins fixtures/list.pyi] |
| 1212 |
|
| 1213 |
[case testAttrsDefaultAndInit] |
| 1214 |
import attr |
| 1215 |
|
| 1216 |
@attr.s |
| 1217 |
class C: |
| 1218 |
a = attr.ib(init=False, default=42) |
| 1219 |
b = attr.ib() # Ok because previous attribute is init=False |
| 1220 |
c = attr.ib(default=44) |
| 1221 |
d = attr.ib(init=False) # Ok because this attribute is init=False |
| 1222 |
e = attr.ib() # E: Non-default attributes not allowed after default attributes. |
| 1223 |
|
| 1224 |
[builtins fixtures/bool.pyi] |
| 1225 |
|
| 1226 |
[case testAttrsOptionalConverter] |
| 1227 |
import attr |
| 1228 |
from attr.converters import optional |
| 1229 |
from typing import Optional |
| 1230 |
|
| 1231 |
def converter(s:int) -> str: |
| 1232 |
return 'hello' |
| 1233 |
|
| 1234 |
|
| 1235 |
@attr.s |
| 1236 |
class A: |
| 1237 |
y: Optional[int] = attr.ib(converter=optional(int)) |
| 1238 |
z: Optional[str] = attr.ib(converter=optional(converter)) |
| 1239 |
|
| 1240 |
|
| 1241 |
A(None, None) |
| 1242 |
|
| 1243 |
[builtins fixtures/plugin_attrs.pyi] |
| 1244 |
|
| 1245 |
[case testAttrsOptionalConverterNewPackage] |
| 1246 |
import attrs |
| 1247 |
from attrs.converters import optional |
| 1248 |
from typing import Optional |
| 1249 |
|
| 1250 |
def converter(s:int) -> str: |
| 1251 |
return 'hello' |
| 1252 |
|
| 1253 |
|
| 1254 |
@attrs.define |
| 1255 |
class A: |
| 1256 |
y: Optional[int] = attrs.field(converter=optional(int)) |
| 1257 |
z: Optional[str] = attrs.field(converter=optional(converter)) |
| 1258 |
|
| 1259 |
|
| 1260 |
A(None, None) |
| 1261 |
|
| 1262 |
[builtins fixtures/plugin_attrs.pyi] |
| 1263 |
|
| 1264 |
|
| 1265 |
[case testAttrsTypeVarNoCollision] |
| 1266 |
from typing import TypeVar, Generic |
| 1267 |
import attr |
| 1268 |
|
| 1269 |
T = TypeVar("T", bytes, str) |
| 1270 |
|
| 1271 |
# Make sure the generated __le__ (and friends) don't use T for their arguments. |
| 1272 |
@attr.s(auto_attribs=True) |
| 1273 |
class A(Generic[T]): |
| 1274 |
v: T |
| 1275 |
[builtins fixtures/plugin_attrs.pyi] |
| 1276 |
|
| 1277 |
[case testAttrsKwOnlyAttrib] |
| 1278 |
import attr |
| 1279 |
@attr.s |
| 1280 |
class A: |
| 1281 |
a = attr.ib(kw_only=True) |
| 1282 |
A() # E: Missing named argument "a" for "A" |
| 1283 |
A(15) # E: Too many positional arguments for "A" |
| 1284 |
A(a=15) |
| 1285 |
[builtins fixtures/plugin_attrs.pyi] |
| 1286 |
|
| 1287 |
[case testAttrsKwOnlyClass] |
| 1288 |
import attr |
| 1289 |
@attr.s(kw_only=True, auto_attribs=True) |
| 1290 |
class A: |
| 1291 |
a: int |
| 1292 |
b: bool |
| 1293 |
A() # E: Missing named argument "a" for "A" # E: Missing named argument "b" for "A" |
| 1294 |
A(b=True, a=15) |
| 1295 |
[builtins fixtures/plugin_attrs.pyi] |
| 1296 |
|
| 1297 |
[case testAttrsKwOnlyClassNoInit] |
| 1298 |
import attr |
| 1299 |
@attr.s(kw_only=True) |
| 1300 |
class B: |
| 1301 |
a = attr.ib(init=False) |
| 1302 |
b = attr.ib() |
| 1303 |
B(b=True) |
| 1304 |
[builtins fixtures/plugin_attrs.pyi] |
| 1305 |
|
| 1306 |
[case testAttrsKwOnlyWithDefault] |
| 1307 |
import attr |
| 1308 |
@attr.s |
| 1309 |
class C: |
| 1310 |
a = attr.ib(0) |
| 1311 |
b = attr.ib(kw_only=True) |
| 1312 |
c = attr.ib(16, kw_only=True) |
| 1313 |
C(b=17) |
| 1314 |
[builtins fixtures/plugin_attrs.pyi] |
| 1315 |
|
| 1316 |
[case testAttrsKwOnlyClassWithMixedDefaults] |
| 1317 |
import attr |
| 1318 |
@attr.s(kw_only=True) |
| 1319 |
class D: |
| 1320 |
a = attr.ib(10) |
| 1321 |
b = attr.ib() |
| 1322 |
c = attr.ib(15) |
| 1323 |
D(b=17) |
| 1324 |
[builtins fixtures/plugin_attrs.pyi] |
| 1325 |
|
| 1326 |
|
| 1327 |
[case testAttrsKwOnlyClassAttributeFalse] |
| 1328 |
from attrs import define, field |
| 1329 |
@define(kw_only=True) |
| 1330 |
class A: |
| 1331 |
a: int = field(kw_only=False) |
| 1332 |
b: int = 1 |
| 1333 |
A(0) |
| 1334 |
A(1, b=3) |
| 1335 |
[builtins fixtures/plugin_attrs.pyi] |
| 1336 |
|
| 1337 |
[case testAttrsKwOnlyClassAttributeFalseSubclass] |
| 1338 |
from attrs import define, field |
| 1339 |
@define(kw_only=True) |
| 1340 |
class A: |
| 1341 |
a: int = field(kw_only=False) |
| 1342 |
b: int = 1 |
| 1343 |
@define(kw_only=True) |
| 1344 |
class B(A): |
| 1345 |
c: int = field(kw_only=False) |
| 1346 |
d: int = 3 |
| 1347 |
|
| 1348 |
B(0, 1) |
| 1349 |
B(0, 1, b=3, d=4) |
| 1350 |
[builtins fixtures/plugin_attrs.pyi] |
| 1351 |
|
| 1352 |
[case testAttrsKwOnlyClassAttributeFalseSubclassOnly] |
| 1353 |
from attrs import define, field |
| 1354 |
@define |
| 1355 |
class A: |
| 1356 |
a: int |
| 1357 |
b: int = field(kw_only=True, default=1) |
| 1358 |
@define(kw_only=True) |
| 1359 |
class B(A): |
| 1360 |
c: int = field(kw_only=False) |
| 1361 |
d: int = 3 |
| 1362 |
|
| 1363 |
B(0, 1) |
| 1364 |
B(0, 1, b=3, d=4) |
| 1365 |
[builtins fixtures/plugin_attrs.pyi] |
| 1366 |
|
| 1367 |
|
| 1368 |
[case testAttrsKwOnlySubclass] |
| 1369 |
import attr |
| 1370 |
@attr.s |
| 1371 |
class A2: |
| 1372 |
a = attr.ib(default=0) |
| 1373 |
@attr.s |
| 1374 |
class B2(A2): |
| 1375 |
b = attr.ib(kw_only=True) |
| 1376 |
B2(b=1) |
| 1377 |
[builtins fixtures/plugin_attrs.pyi] |
| 1378 |
|
| 1379 |
[case testAttrsNonKwOnlyAfterKwOnly] |
| 1380 |
import attr |
| 1381 |
@attr.s(kw_only=True) |
| 1382 |
class A: |
| 1383 |
a = attr.ib(default=0) |
| 1384 |
@attr.s |
| 1385 |
class B(A): |
| 1386 |
b = attr.ib() |
| 1387 |
@attr.s |
| 1388 |
class C: |
| 1389 |
a = attr.ib(kw_only=True) |
| 1390 |
b = attr.ib(15) |
| 1391 |
|
| 1392 |
[builtins fixtures/plugin_attrs.pyi] |
| 1393 |
|
| 1394 |
[case testAttrsDisallowUntypedWorksForward] |
| 1395 |
# flags: --disallow-untyped-defs |
| 1396 |
import attr |
| 1397 |
from typing import List |
| 1398 |
|
| 1399 |
@attr.s |
| 1400 |
class B: |
| 1401 |
x: C = attr.ib() |
| 1402 |
|
| 1403 |
class C(List[C]): |
| 1404 |
pass |
| 1405 |
|
| 1406 |
reveal_type(B) # N: Revealed type is "def (x: __main__.C) -> __main__.B" |
| 1407 |
[builtins fixtures/list.pyi] |
| 1408 |
|
| 1409 |
[case testDisallowUntypedWorksForwardBad] |
| 1410 |
# flags: --disallow-untyped-defs |
| 1411 |
import attr |
| 1412 |
|
| 1413 |
@attr.s |
| 1414 |
class B: |
| 1415 |
x = attr.ib() # E: Need type annotation for "x" |
| 1416 |
|
| 1417 |
reveal_type(B) # N: Revealed type is "def (x: Any) -> __main__.B" |
| 1418 |
[builtins fixtures/list.pyi] |
| 1419 |
|
| 1420 |
[case testAttrsDefaultDecoratorDeferred] |
| 1421 |
defer: Yes |
| 1422 |
|
| 1423 |
import attr |
| 1424 |
@attr.s |
| 1425 |
class C(object): |
| 1426 |
x: int = attr.ib(default=1) |
| 1427 |
y: int = attr.ib() |
| 1428 |
@y.default |
| 1429 |
def inc(self): |
| 1430 |
return self.x + 1 |
| 1431 |
|
| 1432 |
class Yes: ... |
| 1433 |
[builtins fixtures/list.pyi] |
| 1434 |
|
| 1435 |
[case testAttrsValidatorDecoratorDeferred] |
| 1436 |
defer: Yes |
| 1437 |
|
| 1438 |
import attr |
| 1439 |
@attr.s |
| 1440 |
class C(object): |
| 1441 |
x = attr.ib() |
| 1442 |
@x.validator |
| 1443 |
def check(self, attribute, value): |
| 1444 |
if value > 42: |
| 1445 |
raise ValueError("x must be smaller or equal to 42") |
| 1446 |
C(42) |
| 1447 |
C(43) |
| 1448 |
|
| 1449 |
class Yes: ... |
| 1450 |
[builtins fixtures/exception.pyi] |
| 1451 |
|
| 1452 |
[case testTypeInAttrUndefined] |
| 1453 |
import attr |
| 1454 |
|
| 1455 |
@attr.s |
| 1456 |
class C: |
| 1457 |
total = attr.ib(type=Bad) # E: Name "Bad" is not defined |
| 1458 |
[builtins fixtures/bool.pyi] |
| 1459 |
|
| 1460 |
[case testTypeInAttrForwardInRuntime] |
| 1461 |
import attr |
| 1462 |
|
| 1463 |
@attr.s |
| 1464 |
class C: |
| 1465 |
total = attr.ib(type=Forward) |
| 1466 |
|
| 1467 |
reveal_type(C.total) # N: Revealed type is "__main__.Forward" |
| 1468 |
C('no') # E: Argument 1 to "C" has incompatible type "str"; expected "Forward" |
| 1469 |
class Forward: ... |
| 1470 |
[builtins fixtures/bool.pyi] |
| 1471 |
|
| 1472 |
[case testDefaultInAttrForward] |
| 1473 |
import attr |
| 1474 |
|
| 1475 |
@attr.s |
| 1476 |
class C: |
| 1477 |
total = attr.ib(default=func()) |
| 1478 |
|
| 1479 |
def func() -> int: ... |
| 1480 |
|
| 1481 |
C() |
| 1482 |
C(1) |
| 1483 |
C(1, 2) # E: Too many arguments for "C" |
| 1484 |
[builtins fixtures/bool.pyi] |
| 1485 |
|
| 1486 |
[case testTypeInAttrUndefinedFrozen] |
| 1487 |
import attr |
| 1488 |
|
| 1489 |
@attr.s(frozen=True) |
| 1490 |
class C: |
| 1491 |
total = attr.ib(type=Bad) # E: Name "Bad" is not defined |
| 1492 |
|
| 1493 |
C(0).total = 1 # E: Property "total" defined in "C" is read-only |
| 1494 |
[builtins fixtures/plugin_attrs.pyi] |
| 1495 |
|
| 1496 |
[case testTypeInAttrDeferredStar] |
| 1497 |
import lib |
| 1498 |
[file lib.py] |
| 1499 |
import attr |
| 1500 |
MYPY = False |
| 1501 |
if MYPY: # Force deferral |
| 1502 |
from other import * |
| 1503 |
|
| 1504 |
@attr.s |
| 1505 |
class C: |
| 1506 |
total = attr.ib(type=int) |
| 1507 |
|
| 1508 |
C() # E: Missing positional argument "total" in call to "C" |
| 1509 |
C('no') # E: Argument 1 to "C" has incompatible type "str"; expected "int" |
| 1510 |
[file other.py] |
| 1511 |
import lib |
| 1512 |
[builtins fixtures/bool.pyi] |
| 1513 |
|
| 1514 |
[case testAttrsDefaultsMroOtherFile] |
| 1515 |
import a |
| 1516 |
|
| 1517 |
[file a.py] |
| 1518 |
import attr |
| 1519 |
from b import A1, A2 |
| 1520 |
|
| 1521 |
@attr.s |
| 1522 |
class Asdf(A1, A2): # E: Non-default attributes not allowed after default attributes. |
| 1523 |
pass |
| 1524 |
|
| 1525 |
[file b.py] |
| 1526 |
import attr |
| 1527 |
|
| 1528 |
@attr.s |
| 1529 |
class A1: |
| 1530 |
a: str = attr.ib('test') |
| 1531 |
|
| 1532 |
@attr.s |
| 1533 |
class A2: |
| 1534 |
b: int = attr.ib() |
| 1535 |
|
| 1536 |
[builtins fixtures/list.pyi] |
| 1537 |
|
| 1538 |
[case testAttrsInheritanceNoAnnotation] |
| 1539 |
import attr |
| 1540 |
|
| 1541 |
@attr.s |
| 1542 |
class A: |
| 1543 |
foo = attr.ib() # type: int |
| 1544 |
|
| 1545 |
x = 0 |
| 1546 |
@attr.s |
| 1547 |
class B(A): |
| 1548 |
foo = x |
| 1549 |
|
| 1550 |
reveal_type(B) # N: Revealed type is "def (foo: builtins.int) -> __main__.B" |
| 1551 |
[builtins fixtures/bool.pyi] |
| 1552 |
|
| 1553 |
[case testAttrsClassHasMagicAttribute] |
| 1554 |
import attr |
| 1555 |
|
| 1556 |
@attr.s |
| 1557 |
class A: |
| 1558 |
b: int = attr.ib() |
| 1559 |
c: str = attr.ib() |
| 1560 |
|
| 1561 |
reveal_type(A.__attrs_attrs__) # N: Revealed type is "tuple[attr.Attribute[builtins.int], attr.Attribute[builtins.str], fallback=__main__.A.____main___A_AttrsAttributes__]" |
| 1562 |
reveal_type(A.__attrs_attrs__[0]) # N: Revealed type is "attr.Attribute[builtins.int]" |
| 1563 |
reveal_type(A.__attrs_attrs__.b) # N: Revealed type is "attr.Attribute[builtins.int]" |
| 1564 |
A.__attrs_attrs__.x # E: "____main___A_AttrsAttributes__" has no attribute "x" |
| 1565 |
|
| 1566 |
[builtins fixtures/plugin_attrs.pyi] |
| 1567 |
|
| 1568 |
[case testAttrsBareClassHasMagicAttribute] |
| 1569 |
import attr |
| 1570 |
|
| 1571 |
@attr.s |
| 1572 |
class A: |
| 1573 |
b = attr.ib() |
| 1574 |
c = attr.ib() |
| 1575 |
|
| 1576 |
reveal_type(A.__attrs_attrs__) # N: Revealed type is "tuple[attr.Attribute[Any], attr.Attribute[Any], fallback=__main__.A.____main___A_AttrsAttributes__]" |
| 1577 |
reveal_type(A.__attrs_attrs__[0]) # N: Revealed type is "attr.Attribute[Any]" |
| 1578 |
reveal_type(A.__attrs_attrs__.b) # N: Revealed type is "attr.Attribute[Any]" |
| 1579 |
A.__attrs_attrs__.x # E: "____main___A_AttrsAttributes__" has no attribute "x" |
| 1580 |
|
| 1581 |
[builtins fixtures/plugin_attrs.pyi] |
| 1582 |
|
| 1583 |
[case testAttrsNGClassHasMagicAttribute] |
| 1584 |
import attr |
| 1585 |
|
| 1586 |
@attr.define |
| 1587 |
class A: |
| 1588 |
b: int |
| 1589 |
c: str |
| 1590 |
|
| 1591 |
reveal_type(A.__attrs_attrs__) # N: Revealed type is "tuple[attr.Attribute[builtins.int], attr.Attribute[builtins.str], fallback=__main__.A.____main___A_AttrsAttributes__]" |
| 1592 |
reveal_type(A.__attrs_attrs__[0]) # N: Revealed type is "attr.Attribute[builtins.int]" |
| 1593 |
reveal_type(A.__attrs_attrs__.b) # N: Revealed type is "attr.Attribute[builtins.int]" |
| 1594 |
A.__attrs_attrs__.x # E: "____main___A_AttrsAttributes__" has no attribute "x" |
| 1595 |
|
| 1596 |
[builtins fixtures/plugin_attrs.pyi] |
| 1597 |
|
| 1598 |
[case testAttrsMagicAttributeProtocol] |
| 1599 |
import attr |
| 1600 |
from typing import Any, Protocol, Type, ClassVar |
| 1601 |
|
| 1602 |
class AttrsInstance(Protocol): |
| 1603 |
__attrs_attrs__: ClassVar[Any] |
| 1604 |
|
| 1605 |
@attr.define |
| 1606 |
class A: |
| 1607 |
b: int |
| 1608 |
c: str |
| 1609 |
|
| 1610 |
def takes_attrs_cls(cls: Type[AttrsInstance]) -> None: |
| 1611 |
pass |
| 1612 |
|
| 1613 |
def takes_attrs_instance(inst: AttrsInstance) -> None: |
| 1614 |
pass |
| 1615 |
|
| 1616 |
takes_attrs_cls(A) |
| 1617 |
takes_attrs_instance(A(1, "")) |
| 1618 |
|
| 1619 |
takes_attrs_cls(A(1, "")) # E: Argument 1 to "takes_attrs_cls" has incompatible type "A"; expected "type[AttrsInstance]" |
| 1620 |
takes_attrs_instance(A) # E: Argument 1 to "takes_attrs_instance" has incompatible type "type[A]"; expected "AttrsInstance" # N: ClassVar protocol member AttrsInstance.__attrs_attrs__ can never be matched by a class object |
| 1621 |
[builtins fixtures/plugin_attrs.pyi] |
| 1622 |
|
| 1623 |
[case testAttrsFields] |
| 1624 |
import attr |
| 1625 |
from attrs import fields as f # Common usage. |
| 1626 |
|
| 1627 |
@attr.define |
| 1628 |
class A: |
| 1629 |
b: int |
| 1630 |
c: str |
| 1631 |
|
| 1632 |
reveal_type(f(A)) # N: Revealed type is "tuple[attr.Attribute[builtins.int], attr.Attribute[builtins.str], fallback=__main__.A.____main___A_AttrsAttributes__]" |
| 1633 |
reveal_type(f(A)[0]) # N: Revealed type is "attr.Attribute[builtins.int]" |
| 1634 |
reveal_type(f(A).b) # N: Revealed type is "attr.Attribute[builtins.int]" |
| 1635 |
f(A).x # E: "____main___A_AttrsAttributes__" has no attribute "x" |
| 1636 |
|
| 1637 |
for ff in f(A): |
| 1638 |
reveal_type(ff) # N: Revealed type is "attr.Attribute[Any]" |
| 1639 |
|
| 1640 |
[builtins fixtures/plugin_attrs.pyi] |
| 1641 |
|
| 1642 |
[case testAttrsGenericFields] |
| 1643 |
from typing import TypeVar |
| 1644 |
|
| 1645 |
import attr |
| 1646 |
from attrs import fields |
| 1647 |
|
| 1648 |
@attr.define |
| 1649 |
class A: |
| 1650 |
b: int |
| 1651 |
c: str |
| 1652 |
|
| 1653 |
TA = TypeVar('TA', bound=A) |
| 1654 |
|
| 1655 |
def f(t: TA) -> None: |
| 1656 |
reveal_type(fields(t)) # N: Revealed type is "tuple[attr.Attribute[builtins.int], attr.Attribute[builtins.str], fallback=__main__.A.____main___A_AttrsAttributes__]" |
| 1657 |
reveal_type(fields(t)[0]) # N: Revealed type is "attr.Attribute[builtins.int]" |
| 1658 |
reveal_type(fields(t).b) # N: Revealed type is "attr.Attribute[builtins.int]" |
| 1659 |
fields(t).x # E: "____main___A_AttrsAttributes__" has no attribute "x" |
| 1660 |
|
| 1661 |
|
| 1662 |
[builtins fixtures/plugin_attrs.pyi] |
| 1663 |
|
| 1664 |
[case testNonattrsFields] |
| 1665 |
from typing import Any, cast, Type |
| 1666 |
from attrs import fields, has |
| 1667 |
|
| 1668 |
class A: |
| 1669 |
b: int |
| 1670 |
c: str |
| 1671 |
|
| 1672 |
if has(A): |
| 1673 |
fields(A) |
| 1674 |
else: |
| 1675 |
fields(A) # E: Argument 1 to "fields" has incompatible type "type[A]"; expected "type[AttrsInstance]" |
| 1676 |
fields(None) # E: Argument 1 to "fields" has incompatible type "None"; expected "type[AttrsInstance]" |
| 1677 |
fields(cast(Any, 42)) |
| 1678 |
fields(cast(Type[Any], 43)) |
| 1679 |
|
| 1680 |
[builtins fixtures/plugin_attrs.pyi] |
| 1681 |
|
| 1682 |
[case testAttrsInitMethodAlwaysGenerates] |
| 1683 |
from typing import Tuple |
| 1684 |
import attr |
| 1685 |
|
| 1686 |
@attr.define(init=False) |
| 1687 |
class A: |
| 1688 |
b: int |
| 1689 |
c: str |
| 1690 |
def __init__(self, bc: Tuple[int, str]) -> None: |
| 1691 |
b, c = bc |
| 1692 |
self.__attrs_init__(b, c) |
| 1693 |
|
| 1694 |
reveal_type(A) # N: Revealed type is "def (bc: tuple[builtins.int, builtins.str]) -> __main__.A" |
| 1695 |
reveal_type(A.__init__) # N: Revealed type is "def (self: __main__.A, bc: tuple[builtins.int, builtins.str])" |
| 1696 |
reveal_type(A.__attrs_init__) # N: Revealed type is "def (self: __main__.A, b: builtins.int, c: builtins.str)" |
| 1697 |
|
| 1698 |
[builtins fixtures/plugin_attrs.pyi] |
| 1699 |
|
| 1700 |
[case testAttrsClassWithSlots] |
| 1701 |
import attr |
| 1702 |
|
| 1703 |
@attr.define |
| 1704 |
class Define: |
| 1705 |
b: int = attr.ib() |
| 1706 |
|
| 1707 |
def __attrs_post_init__(self) -> None: |
| 1708 |
self.b = 1 |
| 1709 |
self.c = 2 # E: Trying to assign name "c" that is not in "__slots__" of type "__main__.Define" |
| 1710 |
|
| 1711 |
|
| 1712 |
@attr.define(slots=False) |
| 1713 |
class DefineSlotsFalse: |
| 1714 |
b: int = attr.ib() |
| 1715 |
|
| 1716 |
def __attrs_post_init__(self) -> None: |
| 1717 |
self.b = 1 |
| 1718 |
self.c = 2 |
| 1719 |
|
| 1720 |
|
| 1721 |
@attr.s(slots=True) |
| 1722 |
class A: |
| 1723 |
b: int = attr.ib() |
| 1724 |
|
| 1725 |
def __attrs_post_init__(self) -> None: |
| 1726 |
self.b = 1 |
| 1727 |
self.c = 2 # E: Trying to assign name "c" that is not in "__slots__" of type "__main__.A" |
| 1728 |
|
| 1729 |
@attr.dataclass(slots=True) |
| 1730 |
class B: |
| 1731 |
__slots__ = () # would be replaced |
| 1732 |
b: int |
| 1733 |
|
| 1734 |
def __attrs_post_init__(self) -> None: |
| 1735 |
self.b = 1 |
| 1736 |
self.c = 2 # E: Trying to assign name "c" that is not in "__slots__" of type "__main__.B" |
| 1737 |
|
| 1738 |
@attr.dataclass(slots=False) |
| 1739 |
class C: |
| 1740 |
__slots__ = () # would not be replaced |
| 1741 |
b: int |
| 1742 |
|
| 1743 |
def __attrs_post_init__(self) -> None: |
| 1744 |
self.b = 1 # E: Trying to assign name "b" that is not in "__slots__" of type "__main__.C" |
| 1745 |
self.c = 2 # E: Trying to assign name "c" that is not in "__slots__" of type "__main__.C" |
| 1746 |
[builtins fixtures/plugin_attrs.pyi] |
| 1747 |
|
| 1748 |
[case testAttrsClassWithSlotsDerivedFromNonSlots] |
| 1749 |
import attrs |
| 1750 |
|
| 1751 |
class A: |
| 1752 |
pass |
| 1753 |
|
| 1754 |
@attrs.define(slots=True) |
| 1755 |
class B(A): |
| 1756 |
x: int |
| 1757 |
|
| 1758 |
def __attrs_post_init__(self) -> None: |
| 1759 |
self.y = 42 |
| 1760 |
|
| 1761 |
[builtins fixtures/plugin_attrs.pyi] |
| 1762 |
|
| 1763 |
[case testRuntimeSlotsAttr] |
| 1764 |
from attr import dataclass |
| 1765 |
|
| 1766 |
@dataclass(slots=True) |
| 1767 |
class Some: |
| 1768 |
x: int |
| 1769 |
y: str |
| 1770 |
z: bool |
| 1771 |
|
| 1772 |
reveal_type(Some.__slots__) # N: Revealed type is "tuple[builtins.str, builtins.str, builtins.str]" |
| 1773 |
|
| 1774 |
@dataclass(slots=True) |
| 1775 |
class Other: |
| 1776 |
x: int |
| 1777 |
y: str |
| 1778 |
|
| 1779 |
reveal_type(Other.__slots__) # N: Revealed type is "tuple[builtins.str, builtins.str]" |
| 1780 |
|
| 1781 |
|
| 1782 |
@dataclass |
| 1783 |
class NoSlots: |
| 1784 |
x: int |
| 1785 |
y: str |
| 1786 |
|
| 1787 |
NoSlots.__slots__ # E: "type[NoSlots]" has no attribute "__slots__" |
| 1788 |
[builtins fixtures/plugin_attrs.pyi] |
| 1789 |
|
| 1790 |
[case testAttrsWithMatchArgs] |
| 1791 |
import attr |
| 1792 |
|
| 1793 |
@attr.s(match_args=True, auto_attribs=True) |
| 1794 |
class ToMatch: |
| 1795 |
x: int |
| 1796 |
y: int |
| 1797 |
# Not included: |
| 1798 |
z: int = attr.field(kw_only=True) |
| 1799 |
i: int = attr.field(init=False) |
| 1800 |
|
| 1801 |
reveal_type(ToMatch(x=1, y=2, z=3).__match_args__) # N: Revealed type is "tuple[Literal['x']?, Literal['y']?]" |
| 1802 |
reveal_type(ToMatch(1, 2, z=3).__match_args__) # N: Revealed type is "tuple[Literal['x']?, Literal['y']?]" |
| 1803 |
[builtins fixtures/plugin_attrs.pyi] |
| 1804 |
|
| 1805 |
[case testAttrsWithMatchArgsDefaultCase] |
| 1806 |
import attr |
| 1807 |
|
| 1808 |
@attr.s(auto_attribs=True) |
| 1809 |
class ToMatch1: |
| 1810 |
x: int |
| 1811 |
y: int |
| 1812 |
|
| 1813 |
t1: ToMatch1 |
| 1814 |
reveal_type(t1.__match_args__) # N: Revealed type is "tuple[Literal['x']?, Literal['y']?]" |
| 1815 |
|
| 1816 |
@attr.define |
| 1817 |
class ToMatch2: |
| 1818 |
x: int |
| 1819 |
y: int |
| 1820 |
|
| 1821 |
t2: ToMatch2 |
| 1822 |
reveal_type(t2.__match_args__) # N: Revealed type is "tuple[Literal['x']?, Literal['y']?]" |
| 1823 |
[builtins fixtures/plugin_attrs.pyi] |
| 1824 |
|
| 1825 |
[case testAttrsWithMatchArgsOverrideExisting] |
| 1826 |
import attr |
| 1827 |
from typing import Final |
| 1828 |
|
| 1829 |
@attr.s(match_args=True, auto_attribs=True) |
| 1830 |
class ToMatch: |
| 1831 |
__match_args__: Final = ('a', 'b') |
| 1832 |
x: int |
| 1833 |
y: int |
| 1834 |
|
| 1835 |
# It works the same way runtime does: |
| 1836 |
reveal_type(ToMatch(x=1, y=2).__match_args__) # N: Revealed type is "tuple[Literal['a']?, Literal['b']?]" |
| 1837 |
|
| 1838 |
@attr.s(auto_attribs=True) |
| 1839 |
class WithoutMatch: |
| 1840 |
__match_args__: Final = ('a', 'b') |
| 1841 |
x: int |
| 1842 |
y: int |
| 1843 |
|
| 1844 |
reveal_type(WithoutMatch(x=1, y=2).__match_args__) # N: Revealed type is "tuple[Literal['a']?, Literal['b']?]" |
| 1845 |
[builtins fixtures/plugin_attrs.pyi] |
| 1846 |
|
| 1847 |
[case testAttrsMultipleInheritance] |
| 1848 |
import attr |
| 1849 |
|
| 1850 |
@attr.s |
| 1851 |
class A: |
| 1852 |
x = attr.ib(type=int) |
| 1853 |
|
| 1854 |
@attr.s |
| 1855 |
class B: |
| 1856 |
y = attr.ib(type=int) |
| 1857 |
|
| 1858 |
class AB(A, B): |
| 1859 |
pass |
| 1860 |
[builtins fixtures/plugin_attrs.pyi] |
| 1861 |
[typing fixtures/typing-full.pyi] |
| 1862 |
|
| 1863 |
[case testAttrsForwardReferenceInTypeVarBound] |
| 1864 |
from typing import TypeVar, Generic |
| 1865 |
import attr |
| 1866 |
|
| 1867 |
T = TypeVar("T", bound="C") |
| 1868 |
|
| 1869 |
@attr.define |
| 1870 |
class D(Generic[T]): |
| 1871 |
x: int |
| 1872 |
|
| 1873 |
class C: |
| 1874 |
pass |
| 1875 |
[builtins fixtures/plugin_attrs.pyi] |
| 1876 |
|
| 1877 |
[case testComplexTypeInAttrIb] |
| 1878 |
import a |
| 1879 |
|
| 1880 |
[file a.py] |
| 1881 |
import attr |
| 1882 |
import b |
| 1883 |
from typing import Callable |
| 1884 |
|
| 1885 |
@attr.s |
| 1886 |
class C: |
| 1887 |
a = attr.ib(type=Lst[int]) # E: Name "Lst" is not defined |
| 1888 |
# Note that for this test, the 'Value of type "int" is not indexable' errors are silly, |
| 1889 |
# and a consequence of Callable etc. being set to an int in the test stub. |
| 1890 |
b = attr.ib(type=Callable[[], C]) # E: Value of type "int" is not indexable |
| 1891 |
[file b.py] |
| 1892 |
import attr |
| 1893 |
import a |
| 1894 |
from typing import List as Lst, Optional |
| 1895 |
|
| 1896 |
@attr.s |
| 1897 |
class D: |
| 1898 |
a = attr.ib(type=Lst[int]) |
| 1899 |
b = attr.ib(type=Optional[int]) # E: Value of type "int" is not indexable |
| 1900 |
[builtins fixtures/list.pyi] |
| 1901 |
|
| 1902 |
[case testAttrsGenericInheritanceSpecialCase1] |
| 1903 |
import attr |
| 1904 |
from typing import Generic, TypeVar, List |
| 1905 |
|
| 1906 |
T = TypeVar("T") |
| 1907 |
|
| 1908 |
@attr.define |
| 1909 |
class Parent(Generic[T]): |
| 1910 |
x: List[T] |
| 1911 |
|
| 1912 |
@attr.define |
| 1913 |
class Child1(Parent["Child2"]): ... |
| 1914 |
|
| 1915 |
@attr.define |
| 1916 |
class Child2(Parent["Child1"]): ... |
| 1917 |
|
| 1918 |
def f(c: Child2) -> None: |
| 1919 |
reveal_type(Child1([c]).x) # N: Revealed type is "builtins.list[__main__.Child2]" |
| 1920 |
|
| 1921 |
def g(c: Child1) -> None: |
| 1922 |
reveal_type(Child2([c]).x) # N: Revealed type is "builtins.list[__main__.Child1]" |
| 1923 |
[builtins fixtures/list.pyi] |
| 1924 |
|
| 1925 |
[case testAttrsGenericInheritanceSpecialCase2] |
| 1926 |
import attr |
| 1927 |
from typing import Generic, TypeVar |
| 1928 |
|
| 1929 |
T = TypeVar("T") |
| 1930 |
|
| 1931 |
# A subclass might be analyzed before base in import cycles. They are |
| 1932 |
# defined here in reversed order to simulate this. |
| 1933 |
|
| 1934 |
@attr.define |
| 1935 |
class Child1(Parent["Child2"]): |
| 1936 |
x: int |
| 1937 |
|
| 1938 |
@attr.define |
| 1939 |
class Child2(Parent["Child1"]): |
| 1940 |
y: int |
| 1941 |
|
| 1942 |
@attr.define |
| 1943 |
class Parent(Generic[T]): |
| 1944 |
key: str |
| 1945 |
|
| 1946 |
Child1(x=1, key='') |
| 1947 |
Child2(y=1, key='') |
| 1948 |
[builtins fixtures/list.pyi] |
| 1949 |
|
| 1950 |
[case testAttrsUnsupportedConverterWithDisallowUntypedDefs] |
| 1951 |
# flags: --disallow-untyped-defs |
| 1952 |
import attr |
| 1953 |
from typing import Mapping, Any, Union |
| 1954 |
|
| 1955 |
def default_if_none(factory: Any) -> Any: pass |
| 1956 |
|
| 1957 |
@attr.s(slots=True, frozen=True) |
| 1958 |
class C: |
| 1959 |
name: Union[str, None] = attr.ib(default=None) |
| 1960 |
options: Mapping[str, Mapping[str, Any]] = attr.ib( |
| 1961 |
default=None, converter=default_if_none(factory=dict) \ |
| 1962 |
# E: Unsupported converter, only named functions, types and lambdas are currently supported |
| 1963 |
) |
| 1964 |
[builtins fixtures/plugin_attrs.pyi] |
| 1965 |
|
| 1966 |
[case testAttrsUnannotatedConverter] |
| 1967 |
import attr |
| 1968 |
|
| 1969 |
def foo(value): |
| 1970 |
return value.split() |
| 1971 |
|
| 1972 |
@attr.s |
| 1973 |
class Bar: |
| 1974 |
field = attr.ib(default=None, converter=foo) |
| 1975 |
|
| 1976 |
reveal_type(Bar) # N: Revealed type is "def (field: Any =) -> __main__.Bar" |
| 1977 |
bar = Bar("Hello") |
| 1978 |
reveal_type(bar.field) # N: Revealed type is "Any" |
| 1979 |
|
| 1980 |
[builtins fixtures/tuple.pyi] |
| 1981 |
|
| 1982 |
[case testAttrsLambdaConverter] |
| 1983 |
import attr |
| 1984 |
|
| 1985 |
@attr.s |
| 1986 |
class Bar: |
| 1987 |
name: str = attr.ib(converter=lambda s: s.lower()) |
| 1988 |
|
| 1989 |
reveal_type(Bar) # N: Revealed type is "def (name: Any) -> __main__.Bar" |
| 1990 |
bar = Bar("Hello") |
| 1991 |
reveal_type(bar.name) # N: Revealed type is "builtins.str" |
| 1992 |
|
| 1993 |
[builtins fixtures/tuple.pyi] |
| 1994 |
|
| 1995 |
[case testAttrsNestedClass] |
| 1996 |
from typing import List |
| 1997 |
import attr |
| 1998 |
|
| 1999 |
@attr.s |
| 2000 |
class C: |
| 2001 |
@attr.s |
| 2002 |
class D: |
| 2003 |
pass |
| 2004 |
x = attr.ib(type=List[D]) |
| 2005 |
|
| 2006 |
c = C(x=[C.D()]) |
| 2007 |
reveal_type(c.x) # N: Revealed type is "builtins.list[__main__.C.D]" |
| 2008 |
[builtins fixtures/list.pyi] |
| 2009 |
|
| 2010 |
[case testRedefinitionInFrozenClassNoCrash] |
| 2011 |
import attr |
| 2012 |
|
| 2013 |
@attr.s |
| 2014 |
class MyData: |
| 2015 |
is_foo: bool = attr.ib() |
| 2016 |
|
| 2017 |
@staticmethod # E: Name "is_foo" already defined on line 5 |
| 2018 |
def is_foo(string: str) -> bool: ... |
| 2019 |
[builtins fixtures/classmethod.pyi] |
| 2020 |
|
| 2021 |
[case testOverrideWithPropertyInFrozenClassNoCrash] |
| 2022 |
from attrs import frozen |
| 2023 |
|
| 2024 |
@frozen(kw_only=True) |
| 2025 |
class Base: |
| 2026 |
name: str |
| 2027 |
|
| 2028 |
@frozen(kw_only=True) |
| 2029 |
class Sub(Base): |
| 2030 |
first_name: str |
| 2031 |
last_name: str |
| 2032 |
|
| 2033 |
@property |
| 2034 |
def name(self) -> str: ... |
| 2035 |
[builtins fixtures/plugin_attrs.pyi] |
| 2036 |
|
| 2037 |
[case testOverrideWithPropertyInFrozenClassChecked] |
| 2038 |
from attrs import frozen |
| 2039 |
|
| 2040 |
@frozen(kw_only=True) |
| 2041 |
class Base: |
| 2042 |
name: str |
| 2043 |
|
| 2044 |
@frozen(kw_only=True) |
| 2045 |
class Sub(Base): |
| 2046 |
first_name: str |
| 2047 |
last_name: str |
| 2048 |
|
| 2049 |
@property |
| 2050 |
def name(self) -> int: ... # E: Signature of "name" incompatible with supertype "Base" \ |
| 2051 |
# N: Superclass: \ |
| 2052 |
# N: str \ |
| 2053 |
# N: Subclass: \ |
| 2054 |
# N: int |
| 2055 |
|
| 2056 |
# This matches runtime semantics |
| 2057 |
reveal_type(Sub) # N: Revealed type is "def (*, name: builtins.str, first_name: builtins.str, last_name: builtins.str) -> __main__.Sub" |
| 2058 |
[builtins fixtures/plugin_attrs.pyi] |
| 2059 |
|
| 2060 |
[case testFinalInstanceAttribute] |
| 2061 |
from attrs import define |
| 2062 |
from typing import Final |
| 2063 |
|
| 2064 |
@define |
| 2065 |
class C: |
| 2066 |
a: Final[int] |
| 2067 |
|
| 2068 |
reveal_type(C) # N: Revealed type is "def (a: builtins.int) -> __main__.C" |
| 2069 |
|
| 2070 |
C(1).a = 2 # E: Cannot assign to final attribute "a" |
| 2071 |
|
| 2072 |
[builtins fixtures/property.pyi] |
| 2073 |
|
| 2074 |
[case testFinalInstanceAttributeInheritance] |
| 2075 |
from attrs import define |
| 2076 |
from typing import Final |
| 2077 |
|
| 2078 |
@define |
| 2079 |
class C: |
| 2080 |
a: Final[int] |
| 2081 |
|
| 2082 |
@define |
| 2083 |
class D(C): |
| 2084 |
b: Final[str] |
| 2085 |
|
| 2086 |
reveal_type(D) # N: Revealed type is "def (a: builtins.int, b: builtins.str) -> __main__.D" |
| 2087 |
|
| 2088 |
D(1, "").a = 2 # E: Cannot assign to final attribute "a" |
| 2089 |
D(1, "").b = "2" # E: Cannot assign to final attribute "b" |
| 2090 |
|
| 2091 |
[builtins fixtures/property.pyi] |
| 2092 |
|
| 2093 |
[case testEvolve] |
| 2094 |
import attr |
| 2095 |
|
| 2096 |
class Base: |
| 2097 |
pass |
| 2098 |
|
| 2099 |
class Derived(Base): |
| 2100 |
pass |
| 2101 |
|
| 2102 |
class Other: |
| 2103 |
pass |
| 2104 |
|
| 2105 |
@attr.s(auto_attribs=True) |
| 2106 |
class C: |
| 2107 |
name: str |
| 2108 |
b: Base |
| 2109 |
|
| 2110 |
c = C(name='foo', b=Derived()) |
| 2111 |
c = attr.evolve(c) |
| 2112 |
c = attr.evolve(c, name='foo') |
| 2113 |
c = attr.evolve(c, 'foo') # E: Too many positional arguments for "evolve" of "C" |
| 2114 |
c = attr.evolve(c, b=Derived()) |
| 2115 |
c = attr.evolve(c, b=Base()) |
| 2116 |
c = attr.evolve(c, b=Other()) # E: Argument "b" to "evolve" of "C" has incompatible type "Other"; expected "Base" |
| 2117 |
c = attr.evolve(c, name=42) # E: Argument "name" to "evolve" of "C" has incompatible type "int"; expected "str" |
| 2118 |
c = attr.evolve(c, foobar=42) # E: Unexpected keyword argument "foobar" for "evolve" of "C" |
| 2119 |
|
| 2120 |
# test passing instance as 'inst' kw |
| 2121 |
c = attr.evolve(inst=c, name='foo') |
| 2122 |
c = attr.evolve(not_inst=c, name='foo') # E: Missing positional argument "inst" in call to "evolve" |
| 2123 |
|
| 2124 |
# test determining type of first argument's expression from something that's not NameExpr |
| 2125 |
def f() -> C: |
| 2126 |
return c |
| 2127 |
|
| 2128 |
c = attr.evolve(f(), name='foo') |
| 2129 |
|
| 2130 |
[builtins fixtures/plugin_attrs.pyi] |
| 2131 |
|
| 2132 |
[case testEvolveFromNonAttrs] |
| 2133 |
import attr |
| 2134 |
|
| 2135 |
attr.evolve(42, name='foo') # E: Argument 1 to "evolve" has incompatible type "int"; expected an attrs class |
| 2136 |
attr.evolve(None, name='foo') # E: Argument 1 to "evolve" has incompatible type "None"; expected an attrs class |
| 2137 |
[case testEvolveFromAny] |
| 2138 |
from typing import Any |
| 2139 |
import attr |
| 2140 |
|
| 2141 |
any: Any = 42 |
| 2142 |
ret = attr.evolve(any, name='foo') |
| 2143 |
reveal_type(ret) # N: Revealed type is "Any" |
| 2144 |
|
| 2145 |
[typing fixtures/typing-medium.pyi] |
| 2146 |
|
| 2147 |
[case testEvolveGeneric] |
| 2148 |
import attrs |
| 2149 |
from typing import Generic, TypeVar |
| 2150 |
|
| 2151 |
T = TypeVar('T') |
| 2152 |
|
| 2153 |
@attrs.define |
| 2154 |
class A(Generic[T]): |
| 2155 |
x: T |
| 2156 |
|
| 2157 |
|
| 2158 |
a = A(x=42) |
| 2159 |
reveal_type(a) # N: Revealed type is "__main__.A[builtins.int]" |
| 2160 |
a2 = attrs.evolve(a, x=42) |
| 2161 |
reveal_type(a2) # N: Revealed type is "__main__.A[builtins.int]" |
| 2162 |
a2 = attrs.evolve(a, x='42') # E: Argument "x" to "evolve" of "A[int]" has incompatible type "str"; expected "int" |
| 2163 |
reveal_type(a2) # N: Revealed type is "__main__.A[builtins.int]" |
| 2164 |
|
| 2165 |
[builtins fixtures/plugin_attrs.pyi] |
| 2166 |
|
| 2167 |
[case testEvolveUnion] |
| 2168 |
from typing import Generic, TypeVar |
| 2169 |
import attrs |
| 2170 |
|
| 2171 |
T = TypeVar('T') |
| 2172 |
|
| 2173 |
|
| 2174 |
@attrs.define |
| 2175 |
class A(Generic[T]): |
| 2176 |
x: T # exercises meet(T=int, int) = int |
| 2177 |
y: bool # exercises meet(bool, int) = bool |
| 2178 |
z: str # exercises meet(str, bytes) = Never |
| 2179 |
w: dict # exercises meet(dict, Never) = Never |
| 2180 |
|
| 2181 |
|
| 2182 |
@attrs.define |
| 2183 |
class B: |
| 2184 |
x: int |
| 2185 |
y: bool |
| 2186 |
z: bytes |
| 2187 |
|
| 2188 |
|
| 2189 |
a_or_b: A[int] | B |
| 2190 |
a2 = attrs.evolve(a_or_b, x=42, y=True) |
| 2191 |
a2 = attrs.evolve(a_or_b, x=42, y=True, z='42') # E: Argument "z" to "evolve" of "A[int] | B" has incompatible type "str"; expected "Never" |
| 2192 |
a2 = attrs.evolve(a_or_b, x=42, y=True, w={}) # E: Argument "w" to "evolve" of "A[int] | B" has incompatible type "dict[Never, Never]"; expected "Never" |
| 2193 |
|
| 2194 |
[builtins fixtures/plugin_attrs.pyi] |
| 2195 |
|
| 2196 |
[case testEvolveUnionOfTypeVar] |
| 2197 |
import attrs |
| 2198 |
from typing import TypeVar |
| 2199 |
|
| 2200 |
@attrs.define |
| 2201 |
class A: |
| 2202 |
x: int |
| 2203 |
y: int |
| 2204 |
z: str |
| 2205 |
w: dict |
| 2206 |
|
| 2207 |
|
| 2208 |
class B: |
| 2209 |
pass |
| 2210 |
|
| 2211 |
TA = TypeVar('TA', bound=A) |
| 2212 |
TB = TypeVar('TB', bound=B) |
| 2213 |
|
| 2214 |
def f(b_or_t: TA | TB | int) -> None: |
| 2215 |
a2 = attrs.evolve(b_or_t) # E: Argument 1 to "evolve" has type "TA | TB | int" whose item "TB" is not bound to an attrs class \ |
| 2216 |
# E: Argument 1 to "evolve" has incompatible type "TA | TB | int" whose item "int" is not an attrs class |
| 2217 |
|
| 2218 |
|
| 2219 |
[builtins fixtures/plugin_attrs.pyi] |
| 2220 |
|
| 2221 |
[case testEvolveTypeVarBound] |
| 2222 |
import attrs |
| 2223 |
from typing import TypeVar |
| 2224 |
|
| 2225 |
@attrs.define |
| 2226 |
class A: |
| 2227 |
x: int |
| 2228 |
|
| 2229 |
@attrs.define |
| 2230 |
class B(A): |
| 2231 |
pass |
| 2232 |
|
| 2233 |
TA = TypeVar('TA', bound=A) |
| 2234 |
|
| 2235 |
def f(t: TA) -> TA: |
| 2236 |
t2 = attrs.evolve(t, x=42) |
| 2237 |
reveal_type(t2) # N: Revealed type is "TA`-1" |
| 2238 |
t3 = attrs.evolve(t, x='42') # E: Argument "x" to "evolve" of "TA" has incompatible type "str"; expected "int" |
| 2239 |
return t2 |
| 2240 |
|
| 2241 |
f(A(x=42)) |
| 2242 |
f(B(x=42)) |
| 2243 |
|
| 2244 |
[builtins fixtures/plugin_attrs.pyi] |
| 2245 |
|
| 2246 |
[case testEvolveTypeVarBoundNonAttrs] |
| 2247 |
import attrs |
| 2248 |
from typing import Union, TypeVar |
| 2249 |
|
| 2250 |
TInt = TypeVar('TInt', bound=int) |
| 2251 |
TAny = TypeVar('TAny') |
| 2252 |
TNone = TypeVar('TNone', bound=None) |
| 2253 |
TUnion = TypeVar('TUnion', bound=Union[str, int]) |
| 2254 |
|
| 2255 |
def f(t: TInt) -> None: |
| 2256 |
_ = attrs.evolve(t, x=42) # E: Argument 1 to "evolve" has a variable type "TInt" not bound to an attrs class |
| 2257 |
|
| 2258 |
def g(t: TAny) -> None: |
| 2259 |
_ = attrs.evolve(t, x=42) # E: Argument 1 to "evolve" has a variable type "TAny" not bound to an attrs class |
| 2260 |
|
| 2261 |
def h(t: TNone) -> None: |
| 2262 |
_ = attrs.evolve(t, x=42) # E: Argument 1 to "evolve" has a variable type "TNone" not bound to an attrs class |
| 2263 |
|
| 2264 |
def x(t: TUnion) -> None: |
| 2265 |
_ = attrs.evolve(t, x=42) # E: Argument 1 to "evolve" has incompatible type "TUnion" whose item "str" is not an attrs class \ |
| 2266 |
# E: Argument 1 to "evolve" has incompatible type "TUnion" whose item "int" is not an attrs class |
| 2267 |
|
| 2268 |
[builtins fixtures/plugin_attrs.pyi] |
| 2269 |
|
| 2270 |
[case testEvolveTypeVarConstrained] |
| 2271 |
import attrs |
| 2272 |
from typing import TypeVar |
| 2273 |
|
| 2274 |
@attrs.define |
| 2275 |
class A: |
| 2276 |
x: int |
| 2277 |
|
| 2278 |
@attrs.define |
| 2279 |
class B: |
| 2280 |
x: str # conflicting with A.x |
| 2281 |
|
| 2282 |
T = TypeVar('T', A, B) |
| 2283 |
|
| 2284 |
def f(t: T) -> T: |
| 2285 |
t2 = attrs.evolve(t, x=42) # E: Argument "x" to "evolve" of "B" has incompatible type "int"; expected "str" |
| 2286 |
reveal_type(t2) # N: Revealed type is "__main__.A" # N: Revealed type is "__main__.B" |
| 2287 |
t2 = attrs.evolve(t, x='42') # E: Argument "x" to "evolve" of "A" has incompatible type "str"; expected "int" |
| 2288 |
return t2 |
| 2289 |
|
| 2290 |
f(A(x=42)) |
| 2291 |
f(B(x='42')) |
| 2292 |
|
| 2293 |
[builtins fixtures/plugin_attrs.pyi] |
| 2294 |
|
| 2295 |
[case testEvolveVariants] |
| 2296 |
from typing import Any |
| 2297 |
import attr |
| 2298 |
import attrs |
| 2299 |
|
| 2300 |
|
| 2301 |
@attr.s(auto_attribs=True) |
| 2302 |
class C: |
| 2303 |
name: str |
| 2304 |
|
| 2305 |
c = C(name='foo') |
| 2306 |
|
| 2307 |
c = attr.assoc(c, name='test') |
| 2308 |
c = attr.assoc(c, name=42) # E: Argument "name" to "assoc" of "C" has incompatible type "int"; expected "str" |
| 2309 |
|
| 2310 |
c = attrs.evolve(c, name='test') |
| 2311 |
c = attrs.evolve(c, name=42) # E: Argument "name" to "evolve" of "C" has incompatible type "int"; expected "str" |
| 2312 |
|
| 2313 |
c = attrs.assoc(c, name='test') |
| 2314 |
c = attrs.assoc(c, name=42) # E: Argument "name" to "assoc" of "C" has incompatible type "int"; expected "str" |
| 2315 |
|
| 2316 |
[builtins fixtures/plugin_attrs.pyi] |
| 2317 |
[typing fixtures/typing-medium.pyi] |
| 2318 |
|
| 2319 |
[case testFrozenInheritFromGeneric] |
| 2320 |
from typing import Generic, TypeVar |
| 2321 |
from attrs import field, frozen |
| 2322 |
|
| 2323 |
T = TypeVar('T') |
| 2324 |
|
| 2325 |
def f(s: str) -> int: |
| 2326 |
... |
| 2327 |
|
| 2328 |
@frozen |
| 2329 |
class A(Generic[T]): |
| 2330 |
x: T |
| 2331 |
y: int = field(converter=f) |
| 2332 |
|
| 2333 |
@frozen |
| 2334 |
class B(A[int]): |
| 2335 |
pass |
| 2336 |
|
| 2337 |
b = B(42, 'spam') |
| 2338 |
reveal_type(b.x) # N: Revealed type is "builtins.int" |
| 2339 |
reveal_type(b.y) # N: Revealed type is "builtins.int" |
| 2340 |
|
| 2341 |
[builtins fixtures/plugin_attrs.pyi] |
| 2342 |
|
| 2343 |
[case testDefaultHashability] |
| 2344 |
from attrs import define |
| 2345 |
|
| 2346 |
@define |
| 2347 |
class A: |
| 2348 |
a: int |
| 2349 |
|
| 2350 |
reveal_type(A.__hash__) # N: Revealed type is "None" |
| 2351 |
|
| 2352 |
[builtins fixtures/plugin_attrs.pyi] |
| 2353 |
|
| 2354 |
[case testFrozenHashability] |
| 2355 |
from attrs import frozen |
| 2356 |
|
| 2357 |
@frozen |
| 2358 |
class A: |
| 2359 |
a: int |
| 2360 |
|
| 2361 |
reveal_type(A.__hash__) # N: Revealed type is "def (self: builtins.object) -> builtins.int" |
| 2362 |
|
| 2363 |
[builtins fixtures/plugin_attrs.pyi] |
| 2364 |
|
| 2365 |
[case testManualHashHashability] |
| 2366 |
from attrs import define |
| 2367 |
|
| 2368 |
@define(hash=True) |
| 2369 |
class A: |
| 2370 |
a: int |
| 2371 |
|
| 2372 |
reveal_type(A.__hash__) # N: Revealed type is "def (self: builtins.object) -> builtins.int" |
| 2373 |
|
| 2374 |
[builtins fixtures/plugin_attrs.pyi] |
| 2375 |
|
| 2376 |
[case testManualUnsafeHashHashability] |
| 2377 |
from attrs import define |
| 2378 |
|
| 2379 |
@define(unsafe_hash=True) |
| 2380 |
class A: |
| 2381 |
a: int |
| 2382 |
|
| 2383 |
reveal_type(A.__hash__) # N: Revealed type is "def (self: builtins.object) -> builtins.int" |
| 2384 |
|
| 2385 |
[builtins fixtures/plugin_attrs.pyi] |
| 2386 |
|
| 2387 |
[case testSubclassingHashability] |
| 2388 |
from attrs import define |
| 2389 |
|
| 2390 |
@define(unsafe_hash=True) |
| 2391 |
class A: |
| 2392 |
a: int |
| 2393 |
|
| 2394 |
@define |
| 2395 |
class B(A): |
| 2396 |
pass |
| 2397 |
|
| 2398 |
reveal_type(B.__hash__) # N: Revealed type is "None" |
| 2399 |
|
| 2400 |
[builtins fixtures/plugin_attrs.pyi] |
| 2401 |
|
| 2402 |
[case testManualOwnHashability] |
| 2403 |
from attrs import define, frozen |
| 2404 |
|
| 2405 |
@define |
| 2406 |
class A: |
| 2407 |
a: int |
| 2408 |
def __hash__(self) -> int: |
| 2409 |
... |
| 2410 |
|
| 2411 |
reveal_type(A.__hash__) # N: Revealed type is "def (self: __main__.A) -> builtins.int" |
| 2412 |
|
| 2413 |
[builtins fixtures/plugin_attrs.pyi] |
| 2414 |
|
| 2415 |
[case testSubclassDefaultLosesHashability] |
| 2416 |
from attrs import define, frozen |
| 2417 |
|
| 2418 |
@define |
| 2419 |
class A: |
| 2420 |
a: int |
| 2421 |
def __hash__(self) -> int: |
| 2422 |
... |
| 2423 |
|
| 2424 |
@define |
| 2425 |
class B(A): |
| 2426 |
pass |
| 2427 |
|
| 2428 |
reveal_type(B.__hash__) # N: Revealed type is "None" |
| 2429 |
|
| 2430 |
[builtins fixtures/plugin_attrs.pyi] |
| 2431 |
|
| 2432 |
[case testSubclassEqFalseKeepsHashability] |
| 2433 |
from attrs import define, frozen |
| 2434 |
|
| 2435 |
@define |
| 2436 |
class A: |
| 2437 |
a: int |
| 2438 |
def __hash__(self) -> int: |
| 2439 |
... |
| 2440 |
|
| 2441 |
@define(eq=False) |
| 2442 |
class B(A): |
| 2443 |
pass |
| 2444 |
|
| 2445 |
reveal_type(B.__hash__) # N: Revealed type is "def (self: __main__.A) -> builtins.int" |
| 2446 |
|
| 2447 |
[builtins fixtures/plugin_attrs.pyi] |
| 2448 |
|
| 2449 |
[case testSubclassingFrozenHashability] |
| 2450 |
from attrs import define, frozen |
| 2451 |
|
| 2452 |
@define |
| 2453 |
class A: |
| 2454 |
a: int |
| 2455 |
|
| 2456 |
@frozen |
| 2457 |
class B(A): |
| 2458 |
pass |
| 2459 |
|
| 2460 |
reveal_type(B.__hash__) # N: Revealed type is "def (self: builtins.object) -> builtins.int" |
| 2461 |
|
| 2462 |
[builtins fixtures/plugin_attrs.pyi] |
| 2463 |
|
| 2464 |
[case testSubclassingFrozenHashOffHashability] |
| 2465 |
from attrs import define, frozen |
| 2466 |
|
| 2467 |
@define |
| 2468 |
class A: |
| 2469 |
a: int |
| 2470 |
def __hash__(self) -> int: |
| 2471 |
... |
| 2472 |
|
| 2473 |
@frozen(unsafe_hash=False) |
| 2474 |
class B(A): |
| 2475 |
pass |
| 2476 |
|
| 2477 |
reveal_type(B.__hash__) # N: Revealed type is "None" |
| 2478 |
|
| 2479 |
[builtins fixtures/plugin_attrs.pyi] |
| 2480 |
|
| 2481 |
[case testUnsafeHashPrecedence] |
| 2482 |
from attrs import define, frozen |
| 2483 |
|
| 2484 |
@define(unsafe_hash=True, hash=False) |
| 2485 |
class A: |
| 2486 |
pass |
| 2487 |
reveal_type(A.__hash__) # N: Revealed type is "def (self: builtins.object) -> builtins.int" |
| 2488 |
|
| 2489 |
@define(unsafe_hash=False, hash=True) |
| 2490 |
class B: |
| 2491 |
pass |
| 2492 |
reveal_type(B.__hash__) # N: Revealed type is "None" |
| 2493 |
|
| 2494 |
[builtins fixtures/plugin_attrs.pyi] |
| 2495 |
|
| 2496 |
[case testAttrsStrictOptionalSetProperly] |
| 2497 |
from typing import Generic, Optional, TypeVar |
| 2498 |
|
| 2499 |
import attr |
| 2500 |
|
| 2501 |
T = TypeVar("T") |
| 2502 |
|
| 2503 |
@attr.mutable() |
| 2504 |
class Parent(Generic[T]): |
| 2505 |
run_type: Optional[int] = None |
| 2506 |
|
| 2507 |
@attr.mutable() |
| 2508 |
class Child(Parent[float]): |
| 2509 |
pass |
| 2510 |
|
| 2511 |
Parent(run_type = None) |
| 2512 |
c = Child(run_type = None) |
| 2513 |
reveal_type(c.run_type) # N: Revealed type is "builtins.int | None" |
| 2514 |
[builtins fixtures/plugin_attrs.pyi] |