Debian Codegraph @

test-data/unit/check-slots.test

raw
1 [case testSlotsDefinitionWithStrAndListAndTuple]
2 class A:
3 __slots__ = "a"
4 def __init__(self) -> None:
5 self.a = 1
6 self.b = 2 # E: Trying to assign name "b" that is not in "__slots__" of type "__main__.A"
7
8 class B:
9 __slots__ = ("a", "b")
10 def __init__(self) -> None:
11 self.a = 1
12 self.b = 2
13 self.c = 3 # E: Trying to assign name "c" that is not in "__slots__" of type "__main__.B"
14
15 class C:
16 __slots__ = ['c']
17 def __init__(self) -> None:
18 self.a = 1 # E: Trying to assign name "a" that is not in "__slots__" of type "__main__.C"
19 self.c = 3
20
21 class WithVariable:
22 __fields__ = ['a', 'b']
23 __slots__ = __fields__
24
25 def __init__(self) -> None:
26 self.a = 1
27 self.b = 2
28 self.c = 3
29 [builtins fixtures/list.pyi]
30
31
32 [case testSlotsDefinitionWithDict]
33 class D:
34 __slots__ = {'key': 'docs'}
35 def __init__(self) -> None:
36 self.key = 1
37 self.missing = 2 # E: Trying to assign name "missing" that is not in "__slots__" of type "__main__.D"
38 [builtins fixtures/dict.pyi]
39
40
41 [case testSlotsDefinitionWithDynamicDict]
42 slot_kwargs = {'b': 'docs'}
43 class WithDictKwargs:
44 __slots__ = {'a': 'docs', **slot_kwargs}
45 def __init__(self) -> None:
46 self.a = 1
47 self.b = 2
48 self.c = 3
49 [builtins fixtures/dict.pyi]
50
51
52 [case testSlotsDefinitionWithSet]
53 class E:
54 __slots__ = {'e'}
55 def __init__(self) -> None:
56 self.e = 1
57 self.missing = 2 # E: Trying to assign name "missing" that is not in "__slots__" of type "__main__.E"
58 [builtins fixtures/set.pyi]
59
60
61 [case testSlotsDefinitionOutsideOfClass]
62 __slots__ = ("a", "b")
63 class A:
64 def __init__(self) -> None:
65 self.x = 1
66 self.y = 2
67 [builtins fixtures/tuple.pyi]
68
69
70 [case testSlotsDefinitionWithClassVar]
71 class A:
72 __slots__ = ('a',)
73 b = 4
74
75 def __init__(self) -> None:
76 self.a = 1
77
78 # You cannot override class-level variables, but you can use them:
79 b = self.b
80 self.b = 2 # E: Trying to assign name "b" that is not in "__slots__" of type "__main__.A"
81
82 self.c = 3 # E: Trying to assign name "c" that is not in "__slots__" of type "__main__.A"
83
84 A.b = 1
85 [builtins fixtures/tuple.pyi]
86
87
88 [case testSlotsDefinitionMultipleVars1]
89 class A:
90 __slots__ = __fields__ = ("a", "b")
91 def __init__(self) -> None:
92 self.x = 1
93 self.y = 2
94 [builtins fixtures/tuple.pyi]
95
96
97 [case testSlotsDefinitionMultipleVars2]
98 class A:
99 __fields__ = __slots__ = ("a", "b")
100 def __init__(self) -> None:
101 self.x = 1
102 self.y = 2
103 [builtins fixtures/tuple.pyi]
104
105
106 [case testSlotsAssignmentEmptySlots]
107 class A:
108 __slots__ = ()
109 def __init__(self) -> None:
110 self.a = 1
111 self.b = 2
112
113 a = A()
114 a.a = 1
115 a.b = 2
116 a.missing = 2
117 [out]
118 main:4: error: Trying to assign name "a" that is not in "__slots__" of type "__main__.A"
119 main:5: error: Trying to assign name "b" that is not in "__slots__" of type "__main__.A"
120 main:8: error: Trying to assign name "a" that is not in "__slots__" of type "__main__.A"
121 main:9: error: Trying to assign name "b" that is not in "__slots__" of type "__main__.A"
122 main:10: error: "A" has no attribute "missing"
123 [builtins fixtures/tuple.pyi]
124
125
126 [case testSlotsAssignmentWithSuper]
127 class A:
128 __slots__ = ("a",)
129 class B(A):
130 __slots__ = ("b", "c")
131
132 def __init__(self) -> None:
133 self.a = 1
134 self.b = 2
135 self._one = 1
136
137 b = B()
138 b.a = 1
139 b.b = 2
140 b.c = 3
141 b._one = 1
142 b._two = 2
143 [out]
144 main:9: error: Trying to assign name "_one" that is not in "__slots__" of type "__main__.B"
145 main:14: error: "B" has no attribute "c"
146 main:15: error: Trying to assign name "_one" that is not in "__slots__" of type "__main__.B"
147 main:16: error: "B" has no attribute "_two"
148 [builtins fixtures/tuple.pyi]
149
150
151 [case testSlotsAssignmentWithSuperDuplicateSlots]
152 class A:
153 __slots__ = ("a",)
154 class B(A):
155 __slots__ = ("a", "b",)
156
157 def __init__(self) -> None:
158 self.a = 1
159 self.b = 2
160 self._one = 1 # E: Trying to assign name "_one" that is not in "__slots__" of type "__main__.B"
161 [builtins fixtures/tuple.pyi]
162
163
164 [case testSlotsAssignmentWithMixin]
165 class A:
166 __slots__ = ("a",)
167 class Mixin:
168 __slots__ = ("m",)
169 class B(A, Mixin):
170 __slots__ = ("b",)
171
172 def __init__(self) -> None:
173 self.a = 1
174 self.m = 2
175 self._one = 1
176
177 b = B()
178 b.a = 1
179 b.m = 2
180 b.b = 2
181 b._two = 2
182 [out]
183 main:5: error: Class "B" has incompatible disjoint bases
184 main:11: error: Trying to assign name "_one" that is not in "__slots__" of type "__main__.B"
185 main:16: error: "B" has no attribute "b"
186 main:17: error: "B" has no attribute "_two"
187 [builtins fixtures/tuple.pyi]
188
189
190 [case testSlotsAssignmentWithSlottedSuperButNoChildSlots]
191 class A:
192 __slots__ = ("a",)
193 class B(A):
194 def __init__(self) -> None:
195 self.a = 1
196 self.b = 1
197
198 b = B()
199 b.a = 1
200 b.b = 2
201 [builtins fixtures/tuple.pyi]
202
203
204 [case testSlotsAssignmentWithoutSuperSlots]
205 class A:
206 pass # no slots
207 class B(A):
208 __slots__ = ("a", "b")
209
210 def __init__(self) -> None:
211 self.a = 1
212 self.b = 2
213 self.missing = 3
214
215 b = B()
216 b.a = 1
217 b.b = 2
218 b.missing = 3
219 b.extra = 4 # E: "B" has no attribute "extra"
220 [builtins fixtures/tuple.pyi]
221
222
223 [case testSlotsAssignmentWithoutSuperMixingSlots]
224 class A:
225 __slots__ = ()
226 class Mixin:
227 pass # no slots
228 class B(A, Mixin):
229 __slots__ = ("a", "b")
230
231 def __init__(self) -> None:
232 self.a = 1
233 self.b = 2
234 self.missing = 3
235
236 b = B()
237 b.a = 1
238 b.b = 2
239 b.missing = 3
240 b.extra = 4 # E: "B" has no attribute "extra"
241 [builtins fixtures/tuple.pyi]
242
243
244 [case testSlotsAssignmentWithExplicitSetattr]
245 class A:
246 __slots__ = ("a",)
247
248 def __init__(self) -> None:
249 self.a = 1
250 self.b = 2
251
252 def __setattr__(self, k, v) -> None:
253 ...
254
255 a = A()
256 a.a = 1
257 a.b = 2
258 a.c = 3
259 [builtins fixtures/tuple.pyi]
260
261
262 [case testSlotsAssignmentWithParentSetattr]
263 class Parent:
264 __slots__ = ()
265
266 def __setattr__(self, k, v) -> None:
267 ...
268
269 class A(Parent):
270 __slots__ = ("a",)
271
272 def __init__(self) -> None:
273 self.a = 1
274 self.b = 2
275
276 a = A()
277 a.a = 1
278 a.b = 2
279 a.c = 3
280 [builtins fixtures/tuple.pyi]
281
282
283 [case testSlotsAssignmentWithProps]
284 from typing import Any
285
286 custom_prop: Any
287
288 class A:
289 __slots__ = ("a",)
290
291 @property
292 def first(self) -> int:
293 ...
294
295 @first.setter
296 def first(self, arg: int) -> None:
297 ...
298
299 class B(A):
300 __slots__ = ("b",)
301
302 def __init__(self) -> None:
303 self.a = 1
304 self.b = 2
305 self.c = 3
306
307 @property
308 def second(self) -> int:
309 ...
310
311 @second.setter
312 def second(self, arg: int) -> None:
313 ...
314
315 def get_third(self) -> int:
316 ...
317
318 def set_third(self, arg: int) -> None:
319 ...
320
321 third = custom_prop(get_third, set_third)
322
323 b = B()
324 b.a = 1
325 b.b = 2
326 b.c = 3
327 b.first = 1
328 b.second = 2
329 b.third = 3
330 b.extra = 'extra'
331 [out]
332 main:22: error: Trying to assign name "c" that is not in "__slots__" of type "__main__.B"
333 main:43: error: Trying to assign name "c" that is not in "__slots__" of type "__main__.B"
334 main:47: error: "B" has no attribute "extra"
335 [builtins fixtures/property.pyi]
336
337
338 [case testSlotsAssignmentWithUnionProps]
339 from typing import Any, Callable, Union
340
341 custom_obj: Any
342
343 class custom_property(object):
344 def __set__(self, *args, **kwargs):
345 ...
346
347 class A:
348 __slots__ = ("a",)
349
350 def __init__(self) -> None:
351 self.a = 1
352
353 b: custom_property
354 c: Union[Any, custom_property]
355 d: Union[Callable, custom_property]
356 e: Callable
357
358 a = A()
359 a.a = 1
360 a.b = custom_obj
361 a.c = custom_obj
362 a.d = custom_obj
363 a.e = custom_obj
364 [out]
365 [builtins fixtures/dict.pyi]
366
367
368 [case testSlotsAssignmentWithMethodReassign]
369 class A:
370 __slots__ = ()
371
372 def __init__(self) -> None:
373 self.method = lambda: None # E: Cannot assign to a method
374
375 def method(self) -> None:
376 ...
377
378 a = A()
379 a.method = lambda: None # E: Cannot assign to a method
380 [builtins fixtures/tuple.pyi]
381
382
383 [case testSlotsAssignmentWithExplicitDict]
384 class A:
385 __slots__ = ("a",)
386 class B(A):
387 __slots__ = ("__dict__",)
388
389 def __init__(self) -> None:
390 self.a = 1
391 self.b = 2
392
393 b = B()
394 b.a = 1
395 b.b = 2
396 b.c = 3 # E: "B" has no attribute "c"
397 [builtins fixtures/tuple.pyi]
398
399
400 [case testSlotsAssignmentWithExplicitSuperDict]
401 class A:
402 __slots__ = ("__dict__",)
403 class B(A):
404 __slots__ = ("a",)
405
406 def __init__(self) -> None:
407 self.a = 1
408 self.b = 2
409
410 b = B()
411 b.a = 1
412 b.b = 2
413 b.c = 3 # E: "B" has no attribute "c"
414 [builtins fixtures/tuple.pyi]
415
416
417 [case testSlotsAssignmentWithVariable]
418 slot_name = "b"
419 class A:
420 __slots__ = ("a", slot_name)
421 def __init__(self) -> None:
422 self.a = 1
423 self.b = 2
424 self.c = 3
425
426 a = A()
427 a.a = 1
428 a.b = 2
429 a.c = 3
430 a.d = 4 # E: "A" has no attribute "d"
431 [builtins fixtures/tuple.pyi]
432
433
434 [case testSlotsAssignmentMultipleLeftValues]
435 class A:
436 __slots__ = ("a", "b")
437 def __init__(self) -> None:
438 self.a, self.b, self.c = (1, 2, 3) # E: Trying to assign name "c" that is not in "__slots__" of type "__main__.A"
439 [builtins fixtures/tuple.pyi]
440
441
442 [case testSlotsAssignmentMultipleAssignments]
443 class A:
444 __slots__ = ("a",)
445 def __init__(self) -> None:
446 self.a = self.b = self.c = 1
447 [out]
448 main:4: error: Trying to assign name "b" that is not in "__slots__" of type "__main__.A"
449 main:4: error: Trying to assign name "c" that is not in "__slots__" of type "__main__.A"
450 [builtins fixtures/tuple.pyi]
451
452
453 [case testSlotsWithTupleCall]
454 class A:
455 # TODO: for now this way of writing tuples are not recognised
456 __slots__ = tuple(("a", "b"))
457
458 def __init__(self) -> None:
459 self.a = 1
460 self.b = 2
461 self.missing = 3
462 [builtins fixtures/tuple.pyi]
463
464
465 [case testSlotsWithListCall]
466 class A:
467 # TODO: for now this way of writing lists are not recognised
468 __slots__ = list(("a", "b"))
469
470 def __init__(self) -> None:
471 self.a = 1
472 self.b = 2
473 self.missing = 3
474 [builtins fixtures/list.pyi]
475
476
477 [case testSlotsWithSetCall]
478 class A:
479 # TODO: for now this way of writing sets are not recognised
480 __slots__ = set(("a", "b"))
481
482 def __init__(self) -> None:
483 self.a = 1
484 self.b = 2
485 self.missing = 3
486 [builtins fixtures/set.pyi]
487
488
489 [case testSlotsWithDictCall]
490 class A:
491 # TODO: for now this way of writing dicts are not recognised
492 __slots__ = dict((("a", "docs"), ("b", "docs")))
493
494 def __init__(self) -> None:
495 self.a = 1
496 self.b = 2
497 self.missing = 3
498 [builtins fixtures/dict.pyi]
499
500 [case testSlotsNotInClass]
501 # Shouldn't be triggered
502 __slots__ = [1, 2]
503 reveal_type(__slots__) # N: Revealed type is "builtins.list[builtins.int]"
504
505 def foo() -> None:
506 __slots__ = 1
507 reveal_type(__slots__) # N: Revealed type is "builtins.int"
508
509 [case testSlotsEmptyList]
510 class A:
511 __slots__ = []
512 reveal_type(__slots__) # N: Revealed type is "builtins.list[builtins.str]"
513
514 reveal_type(A.__slots__) # N: Revealed type is "builtins.list[builtins.str]"
515
516 [case testSlotsEmptySet]
517 class A:
518 __slots__ = set()
519 reveal_type(__slots__) # N: Revealed type is "builtins.set[builtins.str]"
520
521 reveal_type(A.__slots__) # N: Revealed type is "builtins.set[builtins.str]"
522 [builtins fixtures/set.pyi]
523
524 [case testSlotsWithAny]
525 from typing import Any
526
527 some_obj: Any
528
529 class A:
530 # You can do anything with `Any`:
531 __slots__ = some_obj
532
533 def __init__(self) -> None:
534 self.a = 1
535 self.b = 2
536 self.missing = 3
537 [builtins fixtures/tuple.pyi]
538
539 [case testSlotsWithClassVar]
540 from typing import ClassVar
541 class X:
542 __slots__ = ('a',)
543 a: int
544 x = X()
545 X.a # E: "a" in __slots__ conflicts with class variable access
546 x.a
547 [builtins fixtures/tuple.pyi]
548
549 [case testSlotsOnSelfType]
550 from typing_extensions import Self
551
552 class X:
553 __slots__ = ("foo",)
554 foo: int
555
556 def method1(self: Self) -> Self:
557 self.bar = 0 # E: Trying to assign name "bar" that is not in "__slots__" of type "__main__.X"
558 return self
559
560 def method2(self) -> Self:
561 self.bar = 0 # E: Trying to assign name "bar" that is not in "__slots__" of type "__main__.X"
562 return self
563 [builtins fixtures/tuple.pyi]