Debian Codegraph @

test-data/unit/check-custom-plugin.test

raw
1 -- Test cases for user-defined plugins
2 --
3 -- Note: Plugins used by tests live under test-data/unit/plugins. Defining
4 -- plugin files in test cases does not work reliably.
5
6 [case testFunctionPluginFile]
7 # flags: --config-file tmp/mypy.ini
8 def f() -> str: ...
9 reveal_type(f()) # N: Revealed type is "builtins.int"
10 [file mypy.ini]
11 \[mypy]
12 plugins=<ROOT>/test-data/unit/plugins/fnplugin.py
13
14 [case testFunctionPluginFilePyProjectTOML]
15 # flags: --config-file tmp/pyproject.toml
16 def f() -> str: ...
17 reveal_type(f()) # N: Revealed type is "builtins.int"
18 [file pyproject.toml]
19 \[tool.mypy]
20 plugins='<ROOT>/test-data/unit/plugins/fnplugin.py'
21
22 [case testFunctionPlugin]
23 # flags: --config-file tmp/mypy.ini
24 def f() -> str: ...
25 reveal_type(f()) # N: Revealed type is "builtins.int"
26 [file mypy.ini]
27 \[mypy]
28 plugins=fnplugin
29
30 [case testFunctionPluginPyProjectTOML]
31 # flags: --config-file tmp/pyproject.toml
32 def f() -> str: ...
33 reveal_type(f()) # N: Revealed type is "builtins.int"
34 [file pyproject.toml]
35 \[tool.mypy]
36 plugins = 'fnplugin'
37
38 [case testFunctionPluginFullnameIsNotNone]
39 # flags: --config-file tmp/mypy.ini
40 from typing import Callable, TypeVar
41 f: Callable[[], None]
42 T = TypeVar('T')
43 def g(x: T) -> T: return x # This strips out the name of a callable
44 g(f)()
45 [file mypy.ini]
46 \[mypy]
47 plugins=<ROOT>/test-data/unit/plugins/fnplugin.py
48
49 [case testFunctionPluginFullnameIsNotNonePyProjectTOML]
50 # flags: --config-file tmp/pyproject.toml
51 from typing import Callable, TypeVar
52 f: Callable[[], None]
53 T = TypeVar('T')
54 def g(x: T) -> T: return x # This strips out the name of a callable
55 g(f)()
56 [file pyproject.toml]
57 \[tool.mypy]
58 plugins="<ROOT>/test-data/unit/plugins/fnplugin.py"
59
60 [case testTwoPlugins]
61 # flags: --config-file tmp/mypy.ini
62 def f(): ...
63 def g(): ...
64 def h(): ...
65 reveal_type(f()) # N: Revealed type is "builtins.int"
66 reveal_type(g()) # N: Revealed type is "builtins.str"
67 reveal_type(h()) # N: Revealed type is "Any"
68 [file mypy.ini]
69 \[mypy]
70 plugins=<ROOT>/test-data/unit/plugins/fnplugin.py,
71 <ROOT>/test-data/unit/plugins/plugin2.py
72
73 [case testTwoPluginsPyProjectTOML]
74 # flags: --config-file tmp/pyproject.toml
75 def f(): ...
76 def g(): ...
77 def h(): ...
78 reveal_type(f()) # N: Revealed type is "builtins.int"
79 reveal_type(g()) # N: Revealed type is "builtins.str"
80 reveal_type(h()) # N: Revealed type is "Any"
81 [file pyproject.toml]
82 \[tool.mypy]
83 plugins=['<ROOT>/test-data/unit/plugins/fnplugin.py',
84 '<ROOT>/test-data/unit/plugins/plugin2.py'
85 ]
86
87 [case testTwoPluginsMixedType]
88 # flags: --config-file tmp/mypy.ini
89 def f(): ...
90 def g(): ...
91 def h(): ...
92 reveal_type(f()) # N: Revealed type is "builtins.int"
93 reveal_type(g()) # N: Revealed type is "builtins.str"
94 reveal_type(h()) # N: Revealed type is "Any"
95 [file mypy.ini]
96 \[mypy]
97 plugins=<ROOT>/test-data/unit/plugins/fnplugin.py, plugin2
98
99 [case testTwoPluginsMixedTypePyProjectTOML]
100 # flags: --config-file tmp/pyproject.toml
101 def f(): ...
102 def g(): ...
103 def h(): ...
104 reveal_type(f()) # N: Revealed type is "builtins.int"
105 reveal_type(g()) # N: Revealed type is "builtins.str"
106 reveal_type(h()) # N: Revealed type is "Any"
107 [file pyproject.toml]
108 \[tool.mypy]
109 plugins=['<ROOT>/test-data/unit/plugins/fnplugin.py', 'plugin2']
110
111 [case testMissingPluginFile]
112 # flags: --config-file tmp/mypy.ini
113 [file mypy.ini]
114 \[mypy]
115 plugins=missing.py
116 [out]
117 tmp/mypy.ini:2: error: Can't find plugin "tmp/missing.py"
118 --' (work around syntax highlighting)
119
120 [case testMissingPlugin]
121 # flags: --config-file tmp/mypy.ini
122 [file mypy.ini]
123 \[mypy]
124 plugins=missing
125 [out]
126 tmp/mypy.ini:2: error: Error importing plugin "missing": No module named 'missing'
127
128 [case testMultipleSectionsDefinePlugin]
129 # flags: --config-file tmp/mypy.ini
130 [file mypy.ini]
131 \[acme]
132 plugins=acmeplugin
133 \[mypy]
134 plugins=missing.py
135 \[another]
136 plugins=another_plugin
137 [out]
138 tmp/mypy.ini:4: error: Can't find plugin "tmp/missing.py"
139 --' (work around syntax highlighting)
140
141 [case testInvalidPluginExtension]
142 # flags: --config-file tmp/mypy.ini
143 [file mypy.ini]
144 \[mypy]
145 plugins=dir/badext.pyi
146 [file dir/badext.pyi]
147 [out]
148 tmp/mypy.ini:2: error: Plugin "badext.pyi" does not have a .py extension
149
150 [case testMissingPluginEntryPoint]
151 # flags: --config-file tmp/mypy.ini
152 [file mypy.ini]
153 \[mypy]
154 plugins = <ROOT>/test-data/unit/plugins/noentry.py
155 [out]
156 tmp/mypy.ini:2: error: Plugin "<ROOT>/test-data/unit/plugins/noentry.py" does not define entry point function "plugin"
157
158 [case testCustomPluginEntryPointFile]
159 # flags: --config-file tmp/mypy.ini
160 def f() -> str: ...
161 reveal_type(f()) # N: Revealed type is "builtins.int"
162 [file mypy.ini]
163 \[mypy]
164 plugins=<ROOT>/test-data/unit/plugins/customentry.py:register
165
166 [case testCustomPluginEntryPointFileTrailingComma]
167 # flags: --config-file tmp/mypy.ini
168 def f() -> str: ...
169 reveal_type(f()) # N: Revealed type is "builtins.int"
170 [file mypy.ini]
171 \[mypy]
172 plugins =
173 <ROOT>/test-data/unit/plugins/customentry.py:register,
174
175 [case testCustomPluginEntryPoint]
176 # flags: --config-file tmp/mypy.ini
177 def f() -> str: ...
178 reveal_type(f()) # N: Revealed type is "builtins.int"
179 [file mypy.ini]
180 \[mypy]
181 plugins=customentry:register
182
183 [case testInvalidPluginEntryPointReturnValue]
184 # flags: --config-file tmp/mypy.ini
185 def f(): pass
186 f()
187 [file mypy.ini]
188 \[mypy]
189
190 plugins=<ROOT>/test-data/unit/plugins/badreturn.py
191 [out]
192 tmp/mypy.ini:3: error: Type object expected as the return value of "plugin"; got None (in <ROOT>/test-data/unit/plugins/badreturn.py)
193
194 [case testInvalidPluginEntryPointReturnValue2]
195 # flags: --config-file tmp/mypy.ini
196 def f(): pass
197 f()
198 [file mypy.ini]
199 \[mypy]
200 plugins=<ROOT>/test-data/unit/plugins/badreturn2.py
201 [out]
202 tmp/mypy.ini:2: error: Return value of "plugin" must be a subclass of "mypy.plugin.Plugin" (in <ROOT>/test-data/unit/plugins/badreturn2.py)
203
204 [case testAttributeTypeHookPlugin]
205 # flags: --config-file tmp/mypy.ini
206 from typing import Callable
207 from m import Signal, DerivedSignal
208 s: Signal[Callable[[int], None]] = Signal()
209 s(1)
210 s('') # E: Argument 1 has incompatible type "str"; expected "int"
211
212 ds: DerivedSignal[Callable[[int], None]] = DerivedSignal()
213 ds('') # E: Argument 1 has incompatible type "str"; expected "int"
214 [file m.py]
215 from typing import TypeVar, Generic, Callable
216 T = TypeVar('T', bound=Callable[..., None])
217 class Signal(Generic[T]):
218 __call__: Callable[..., None] # This type is replaced by the plugin
219
220 class DerivedSignal(Signal[T]): ...
221 [file mypy.ini]
222 \[mypy]
223 plugins=<ROOT>/test-data/unit/plugins/attrhook.py
224
225 [case testAttributeTypeHookPluginUntypedDecoratedGetattr]
226 # flags: --config-file tmp/mypy.ini
227 from m import Magic, DerivedMagic
228
229 magic = Magic()
230 reveal_type(magic.magic_field) # N: Revealed type is "builtins.str"
231 reveal_type(magic.non_magic_method()) # N: Revealed type is "builtins.int"
232 reveal_type(magic.non_magic_field) # N: Revealed type is "builtins.int"
233 magic.nonexistent_field # E: Field does not exist
234 reveal_type(magic.fallback_example) # N: Revealed type is "Any"
235 reveal_type(magic.no_assignment_field) # N: Revealed type is "builtins.float"
236 magic.no_assignment_field = "bad" # E: Cannot assign to field
237
238 derived = DerivedMagic()
239 reveal_type(derived.magic_field) # N: Revealed type is "builtins.str"
240 derived.nonexistent_field # E: Field does not exist
241 reveal_type(derived.fallback_example) # N: Revealed type is "Any"
242
243 [file m.py]
244 from typing import Any, Callable
245
246 def decorator(f):
247 pass
248
249 class Magic:
250 # Triggers plugin infrastructure:
251 @decorator
252 def __getattr__(self, x: Any) -> Any: ...
253 def non_magic_method(self) -> int: ...
254 non_magic_field: int
255 no_assignment_field: float
256
257 class DerivedMagic(Magic): ...
258 [file mypy.ini]
259 \[mypy]
260 plugins=<ROOT>/test-data/unit/plugins/attrhook2.py
261
262 [case testAttributeTypeHookPluginDecoratedGetattr]
263 # flags: --config-file tmp/mypy.ini
264 from m import Magic, DerivedMagic
265
266 magic = Magic()
267 reveal_type(magic.magic_field) # N: Revealed type is "builtins.str"
268 reveal_type(magic.non_magic_method()) # N: Revealed type is "builtins.int"
269 reveal_type(magic.non_magic_field) # N: Revealed type is "builtins.int"
270 magic.nonexistent_field # E: Field does not exist
271 reveal_type(magic.fallback_example) # N: Revealed type is "builtins.bool"
272
273 derived = DerivedMagic()
274 reveal_type(derived.magic_field) # N: Revealed type is "builtins.str"
275 derived.nonexistent_field # E: Field does not exist
276 reveal_type(derived.fallback_example) # N: Revealed type is "builtins.bool"
277
278 [file m.py]
279 from typing import Any, Callable
280
281 def decorator(f: Callable) -> Callable[[Any, str], bool]:
282 pass
283
284 class Magic:
285 # Triggers plugin infrastructure:
286 @decorator
287 def __getattr__(self, x: Any) -> Any: ...
288 def non_magic_method(self) -> int: ...
289 non_magic_field: int
290
291 class DerivedMagic(Magic): ...
292 [file mypy.ini]
293 \[mypy]
294 plugins=<ROOT>/test-data/unit/plugins/attrhook2.py
295
296 [case testAttributeHookPluginForDynamicClass]
297 # flags: --config-file tmp/mypy.ini
298 from m import Magic, DerivedMagic
299
300 magic = Magic()
301 reveal_type(magic.magic_field) # N: Revealed type is "builtins.str"
302 reveal_type(magic.non_magic_method()) # N: Revealed type is "builtins.int"
303 reveal_type(magic.non_magic_field) # N: Revealed type is "builtins.int"
304 magic.nonexistent_field # E: Field does not exist
305 reveal_type(magic.fallback_example) # N: Revealed type is "Any"
306
307 derived = DerivedMagic()
308 reveal_type(derived.magic_field) # N: Revealed type is "builtins.str"
309 derived.nonexistent_field # E: Field does not exist
310 reveal_type(magic.fallback_example) # N: Revealed type is "Any"
311
312 [file m.py]
313 from typing import Any
314 class Magic:
315 # Triggers plugin infrastructure:
316 def __getattr__(self, x: Any) -> Any: ...
317 def non_magic_method(self) -> int: ...
318 non_magic_field: int
319
320 class DerivedMagic(Magic): ...
321
322 [file mypy.ini]
323 \[mypy]
324 plugins=<ROOT>/test-data/unit/plugins/attrhook2.py
325
326 [case testTypeAnalyzeHookPlugin]
327 # flags: --config-file tmp/mypy.ini
328 from typing import Callable
329 from mypy_extensions import DefaultArg
330 from m import Signal
331 s: Signal[[int, DefaultArg(str, 'x')]] = Signal()
332 reveal_type(s) # N: Revealed type is "m.Signal[def (builtins.int, x: builtins.str =)]"
333 s.x # E: "Signal[Callable[[int, str], None]]" has no attribute "x"
334 ss: Signal[int, str] # E: Invalid "Signal" type (expected "Signal[[t, ...]]")
335 [file m.py]
336 from typing import TypeVar, Generic, Callable
337 T = TypeVar('T', bound=Callable[..., None])
338 class Signal(Generic[T]):
339 __call__: Callable[..., None]
340 [file mypy.ini]
341 \[mypy]
342 plugins=<ROOT>/test-data/unit/plugins/type_anal_hook.py
343 [builtins fixtures/dict.pyi]
344
345 [case testFunctionPluginHookForClass]
346 # flags: --config-file tmp/mypy.ini
347 import mod
348 from mod import AttrInt
349
350 Alias = AttrInt
351 AnotherAlias = mod.Attr
352
353 class C:
354 x = Alias()
355 y = mod.AttrInt(required=True)
356 z = AnotherAlias(int, required=False)
357
358 c = C()
359 reveal_type(c.x) # N: Revealed type is "builtins.int | None"
360 reveal_type(c.y) # N: Revealed type is "builtins.int"
361 reveal_type(c.z) # N: Revealed type is "builtins.int | None"
362
363 [file mod.py]
364 from typing import Generic, TypeVar, Type
365 T = TypeVar('T')
366
367 class Attr(Generic[T]):
368 def __init__(self, tp: Type[T], required: bool = False) -> None:
369 pass
370 def __get__(self, instance: object, owner: type) -> T:
371 pass
372
373 class AttrInt(Attr[int]):
374 def __init__(self, required: bool = False) -> None:
375 pass
376
377 [file mypy.ini]
378 \[mypy]
379 plugins=<ROOT>/test-data/unit/plugins/class_callable.py
380 [builtins fixtures/bool.pyi]
381 [out]
382
383 [case testFunctionPluginHookForReturnedCallable]
384 # flags: --config-file tmp/mypy.ini
385 from m import decorator1, decorator2
386 @decorator1()
387 def f() -> None: pass
388 @decorator2()
389 def g() -> None: pass
390 reveal_type(f) # N: Revealed type is "def (*Any, **Any) -> builtins.str"
391 reveal_type(g) # N: Revealed type is "def (*Any, **Any) -> builtins.int"
392 [file m.py]
393 from typing import Callable
394 def decorator1() -> Callable[..., Callable[..., int]]: pass
395 def decorator2() -> Callable[..., Callable[..., int]]: pass
396 [file mypy.ini]
397 \[mypy]
398 plugins=<ROOT>/test-data/unit/plugins/named_callable.py
399
400 [case testFunctionMethodContextsHasArgNames]
401 # flags: --config-file tmp/mypy.ini
402 from mod import Class, func
403
404 reveal_type(Class().method(arg1=1, arg2=2, classname='builtins.str')) # N: Revealed type is "builtins.str"
405 reveal_type(Class.myclassmethod(arg1=1, arg2=2, classname='builtins.str')) # N: Revealed type is "builtins.str"
406 reveal_type(Class.mystaticmethod(arg1=1, arg2=2, classname='builtins.str')) # N: Revealed type is "builtins.str"
407 reveal_type(Class.method(self=Class(), arg1=1, arg2=2, classname='builtins.str')) # N: Revealed type is "builtins.str"
408 reveal_type(func(arg1=1, arg2=2, classname='builtins.str')) # N: Revealed type is "builtins.str"
409
410 [file mod.py]
411 from typing import Any
412 class Class:
413 def method(self, classname: str, arg1: Any, arg2: Any) -> Any:
414 pass
415 @classmethod
416 def myclassmethod(cls, classname: str, arg1: Any, arg2: Any):
417 pass
418 @staticmethod
419 def mystaticmethod(classname: str, arg1: Any, arg2: Any):
420 pass
421 def func(classname: str, arg1: Any, arg2: Any) -> Any:
422 pass
423
424 [file mypy.ini]
425 \[mypy]
426 plugins=<ROOT>/test-data/unit/plugins/arg_names.py
427 [builtins fixtures/classmethod.pyi]
428
429 [case testFunctionMethodContextsHasArgNamesPositionals]
430 # flags: --config-file tmp/mypy.ini
431 from mod import Class, func
432
433 reveal_type(Class().method('builtins.str', arg1=1, arg2=2)) # N: Revealed type is "builtins.str"
434 reveal_type(Class.myclassmethod('builtins.str', arg1=1, arg2=2)) # N: Revealed type is "builtins.str"
435 reveal_type(Class.mystaticmethod('builtins.str', arg1=1, arg2=2)) # N: Revealed type is "builtins.str"
436 reveal_type(Class.method(Class(), 'builtins.str', arg1=1, arg2=2)) # N: Revealed type is "builtins.str"
437 reveal_type(func('builtins.str', arg1=1, arg2=2)) # N: Revealed type is "builtins.str"
438
439 [file mod.py]
440 from typing import Any
441 class Class:
442 def method(self, classname: str, arg1: Any, arg2: Any) -> Any:
443 pass
444 @classmethod
445 def myclassmethod(cls, classname: str, arg1: Any, arg2: Any):
446 pass
447 @staticmethod
448 def mystaticmethod(classname: str, arg1: Any, arg2: Any):
449 pass
450 def func(classname: str, arg1: Any, arg2: Any) -> Any:
451 pass
452
453 [file mypy.ini]
454 \[mypy]
455 plugins=<ROOT>/test-data/unit/plugins/arg_names.py
456 [builtins fixtures/classmethod.pyi]
457
458 [case testFunctionMethodContextsHasArgNamesInitMethod]
459 # flags: --config-file tmp/mypy.ini
460 from mod import ClassInit, Outer
461
462 reveal_type(ClassInit('builtins.str')) # N: Revealed type is "builtins.str"
463 reveal_type(ClassInit(classname='builtins.str')) # N: Revealed type is "builtins.str"
464 reveal_type(Outer.NestedClassInit(classname='builtins.str')) # N: Revealed type is "builtins.str"
465 [file mod.py]
466 from typing import Any
467 class ClassInit:
468 def __init__(self, classname: str):
469 pass
470 class Outer:
471 class NestedClassInit:
472 def __init__(self, classname: str):
473 pass
474
475 [file mypy.ini]
476 \[mypy]
477 plugins=<ROOT>/test-data/unit/plugins/arg_names.py
478
479 [case testFunctionMethodContextsHasArgNamesUnfilledArguments]
480 # flags: --config-file tmp/mypy.ini
481 from mod import ClassUnfilled, func_unfilled
482
483 reveal_type(ClassUnfilled().method(classname='builtins.str', arg1=1)) # N: Revealed type is "builtins.str"
484 reveal_type(ClassUnfilled().method(arg2=1, classname='builtins.str')) # N: Revealed type is "builtins.str"
485 reveal_type(ClassUnfilled().method('builtins.str')) # N: Revealed type is "builtins.str"
486 reveal_type(func_unfilled(classname='builtins.str', arg1=1)) # N: Revealed type is "builtins.str"
487 reveal_type(func_unfilled(arg2=1, classname='builtins.str')) # N: Revealed type is "builtins.str"
488 reveal_type(func_unfilled('builtins.str')) # N: Revealed type is "builtins.str"
489
490 [file mod.py]
491 from typing import Any
492 class ClassUnfilled:
493 def method(self, classname: str, arg1: Any = None, arg2: Any = None) -> Any:
494 pass
495 def func_unfilled(classname: str, arg1: Any = None, arg2: Any = None) -> Any:
496 pass
497
498 [file mypy.ini]
499 \[mypy]
500 plugins=<ROOT>/test-data/unit/plugins/arg_names.py
501
502 [case testFunctionMethodContextsHasArgNamesStarExpressions]
503 # flags: --config-file tmp/mypy.ini
504 from mod import ClassStarExpr, func_star_expr
505
506 reveal_type(ClassStarExpr().method(classname='builtins.str', arg1=1)) # N: Revealed type is "builtins.str"
507 reveal_type(ClassStarExpr().method('builtins.str', arg1=1)) # N: Revealed type is "builtins.str"
508 reveal_type(ClassStarExpr().method('builtins.str', arg1=1, arg2=1)) # N: Revealed type is "builtins.str"
509 reveal_type(ClassStarExpr().method('builtins.str', 2, 3, 4, arg1=1, arg2=1)) # N: Revealed type is "builtins.str"
510 reveal_type(func_star_expr(classname='builtins.str', arg1=1)) # N: Revealed type is "builtins.str"
511 reveal_type(func_star_expr('builtins.str', arg1=1)) # N: Revealed type is "builtins.str"
512 reveal_type(func_star_expr('builtins.str', 2, 3, 4, arg1=1, arg2=2)) # N: Revealed type is "builtins.str"
513
514 [file mod.py]
515 from typing import Any
516 class ClassStarExpr:
517 def method(self, classname: str, *args, **kwargs) -> Any:
518 pass
519 def func_star_expr(classname: str, *args, **kwargs) -> Any:
520 pass
521
522 [file mypy.ini]
523 \[mypy]
524 plugins=<ROOT>/test-data/unit/plugins/arg_names.py
525 [builtins fixtures/dict.pyi]
526
527 [case testFunctionMethodContextArgNamesForInheritedMethods]
528 # flags: --config-file tmp/mypy.ini
529 from mod import ClassChild
530
531 reveal_type(ClassChild().method(classname='builtins.str', arg1=1, arg2=1)) # N: Revealed type is "builtins.str"
532 reveal_type(ClassChild().method(arg1=1, classname='builtins.str', arg2=1)) # N: Revealed type is "builtins.str"
533 reveal_type(ClassChild().method('builtins.str', arg1=1, arg2=1)) # N: Revealed type is "builtins.str"
534 reveal_type(ClassChild.myclassmethod('builtins.str')) # N: Revealed type is "builtins.str"
535 [file mod.py]
536 from typing import Any
537 class Base:
538 def method(self, classname: str, arg1: Any, arg2: Any) -> Any:
539 pass
540 @classmethod
541 def myclassmethod(cls, classname: str) -> Any:
542 pass
543 class ClassChild(Base):
544 pass
545
546 [file mypy.ini]
547 \[mypy]
548 plugins=<ROOT>/test-data/unit/plugins/arg_names.py
549 [builtins fixtures/classmethod.pyi]
550
551 [case testMethodSignatureHook]
552 # flags: --config-file tmp/mypy.ini
553 from typing import Iterator
554
555 class Foo:
556 # Test that method signature hooks are applied in various cases: explicit method calls, and
557 # implicit dunder method calls through language syntax.
558 # The plugin's method signature hook should turn all str occurrences into int.
559 def __init__(self) -> None: ...
560 def __getitem__(self, index: str) -> str: ...
561 def __setitem__(self, index: str, value: str) -> None: ...
562 def __iter__(self) -> Iterator[str]: ...
563 def __next__(self) -> str: ...
564 def __call__(self, *args: str) -> str: ...
565 def m(self, arg: str) -> str: ...
566
567 foo = Foo()
568 reveal_type(foo.m(2)) # N: Revealed type is "builtins.int"
569 reveal_type(foo[3]) # N: Revealed type is "builtins.int"
570 reveal_type(foo(4, 5, 6)) # N: Revealed type is "builtins.int"
571 foo[4] = 5
572 for x in foo:
573 reveal_type(x) # N: Revealed type is "builtins.int"
574
575 [file mypy.ini]
576 \[mypy]
577 plugins=<ROOT>/test-data/unit/plugins/method_sig_hook.py
578 [builtins fixtures/tuple.pyi]
579
580 [case testMethodSignatureHookNamesFullyQualified]
581 # flags: --config-file tmp/mypy.ini
582 from typing import NamedTuple, TypedDict
583
584 class FullyQualifiedTestClass:
585 @classmethod
586 def class_method(self) -> str: ...
587 def instance_method(self) -> str: ...
588
589 class FullyQualifiedTestTypedDict(TypedDict):
590 foo: str
591
592 FullyQualifiedTestNamedTuple = NamedTuple('FullyQualifiedTestNamedTuple', [('foo', str)])
593
594 # Check the return types to ensure that the method signature hook is called in each case
595 reveal_type(FullyQualifiedTestClass.class_method()) # N: Revealed type is "builtins.int"
596 reveal_type(FullyQualifiedTestClass().instance_method()) # N: Revealed type is "builtins.int"
597 reveal_type(FullyQualifiedTestNamedTuple('')._asdict()) # N: Revealed type is "builtins.int"
598
599 [file mypy.ini]
600 \[mypy]
601 plugins=<ROOT>/test-data/unit/plugins/fully_qualified_test_hook.py
602 [builtins fixtures/classmethod.pyi]
603 [typing fixtures/typing-typeddict.pyi]
604
605 [case testDynamicClassPlugin]
606 # flags: --config-file tmp/mypy.ini
607 from mod import declarative_base, Column, Instr
608
609 Base = declarative_base()
610
611 class Model(Base):
612 x: Column[int]
613 class Other:
614 x: Column[int]
615
616 reveal_type(Model().x) # N: Revealed type is "mod.Instr[builtins.int]"
617 reveal_type(Other().x) # N: Revealed type is "mod.Column[builtins.int]"
618 [file mod.py]
619 from typing import Generic, TypeVar
620 def declarative_base(): ...
621
622 T = TypeVar('T')
623
624 class Column(Generic[T]): ...
625 class Instr(Generic[T]): ...
626
627 [file mypy.ini]
628 \[mypy]
629 plugins=<ROOT>/test-data/unit/plugins/dyn_class.py
630
631 [case testDynamicClassPluginChainCall]
632 # flags: --config-file tmp/mypy.ini
633 from mod import declarative_base, Column, Instr
634
635 Base = declarative_base().with_optional_xxx()
636
637 class Model(Base):
638 x: Column[int]
639
640 reveal_type(Model().x) # N: Revealed type is "mod.Instr[builtins.int]"
641 [file mod.py]
642 from typing import Generic, TypeVar
643
644 class Base:
645 def with_optional_xxx(self) -> Base: ...
646
647 def declarative_base() -> Base: ...
648
649 T = TypeVar('T')
650
651 class Column(Generic[T]): ...
652 class Instr(Generic[T]): ...
653
654 [file mypy.ini]
655 \[mypy]
656 plugins=<ROOT>/test-data/unit/plugins/dyn_class.py
657
658 [case testDynamicClassPluginChainedAssignment]
659 # flags: --config-file tmp/mypy.ini
660 from mod import declarative_base
661
662 Base1 = Base2 = declarative_base()
663
664 class C1(Base1): ...
665 class C2(Base2): ...
666 [file mod.py]
667 def declarative_base(): ...
668 [file mypy.ini]
669 \[mypy]
670 plugins=<ROOT>/test-data/unit/plugins/dyn_class.py
671
672 [case testDynamicClassPluginNegatives]
673 # flags: --config-file tmp/mypy.ini
674 from mod import non_declarative_base
675
676 Bad1 = non_declarative_base()
677
678 class C1(Bad1): ... # E: Variable "__main__.Bad1" is not valid as a type \
679 # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases \
680 # E: Invalid base class "Bad1"
681 [file mod.py]
682 def non_declarative_base(): ...
683 [file mypy.ini]
684 \[mypy]
685 plugins=<ROOT>/test-data/unit/plugins/dyn_class.py
686
687 [case testDynamicClassHookFromClassMethod]
688 # flags: --config-file tmp/mypy.ini
689
690 from mod import QuerySet, Manager, GenericQuerySet
691
692 MyManager = Manager.from_queryset(QuerySet)
693 ManagerFromGenericQuerySet = GenericQuerySet[int].as_manager()
694
695 reveal_type(MyManager()) # N: Revealed type is "__main__.MyManager"
696 reveal_type(MyManager().attr) # N: Revealed type is "builtins.str"
697 reveal_type(ManagerFromGenericQuerySet()) # N: Revealed type is "__main__.ManagerFromGenericQuerySet"
698 reveal_type(ManagerFromGenericQuerySet().attr) # N: Revealed type is "builtins.int"
699 queryset: GenericQuerySet[int] = ManagerFromGenericQuerySet()
700
701 def func(manager: MyManager) -> None:
702 reveal_type(manager) # N: Revealed type is "__main__.MyManager"
703 reveal_type(manager.attr) # N: Revealed type is "builtins.str"
704
705 func(MyManager())
706
707 [file mod.py]
708 from typing import Generic, TypeVar, Type
709 class QuerySet:
710 attr: str
711 class Manager:
712 @classmethod
713 def from_queryset(cls, queryset_cls: Type[QuerySet]): ...
714 T = TypeVar("T")
715 class GenericQuerySet(Generic[T]):
716 attr: T
717
718 @classmethod
719 def as_manager(cls): ...
720
721 [builtins fixtures/classmethod.pyi]
722 [file mypy.ini]
723 \[mypy]
724 plugins=<ROOT>/test-data/unit/plugins/dyn_class_from_method.py
725
726 [case testBaseClassPluginHookWorksIncremental]
727 # flags: --config-file tmp/mypy.ini
728 import a
729
730 [file a.py]
731 from base import Base
732 class C(Base): ...
733
734 [file a.py.2]
735 from base import Base
736 class C(Base): ...
737 reveal_type(C().__magic__)
738 Base.__magic__
739
740 [file base.py]
741 from lib import declarative_base
742 Base = declarative_base()
743
744 [file lib.py]
745 from typing import Any
746 def declarative_base() -> Any: ...
747
748 [file mypy.ini]
749 \[mypy]
750 python_version=3.6
751 plugins=<ROOT>/test-data/unit/plugins/common_api_incremental.py
752 [out]
753 [out2]
754 tmp/a.py:3: note: Revealed type is "builtins.str"
755 tmp/a.py:4: error: "type[Base]" has no attribute "__magic__"
756
757 [case testArgKindsMethod]
758 # flags: --config-file tmp/mypy.ini
759 class Class:
760 def method(self, *args, **kwargs):
761 pass
762
763 Class().method(1, *[2], **{'a': 1}) # E: [[0, 2], [4]]
764 [builtins fixtures/dict.pyi]
765 [file mypy.ini]
766 \[mypy]
767 plugins=<ROOT>/test-data/unit/plugins/arg_kinds.py
768
769 [case testArgKindsFunction]
770 # flags: --config-file tmp/mypy.ini
771 def func(*args, **kwargs):
772 pass
773
774 func(1, 2, [3, 4], *[5, 6, 7], **{'a': 1}) # E: [[0, 0, 0, 2], [4]]
775 [builtins fixtures/dict.pyi]
776 [file mypy.ini]
777 \[mypy]
778 plugins=<ROOT>/test-data/unit/plugins/arg_kinds.py
779
780 [case testHookCallableInstance]
781 # flags: --config-file tmp/mypy.ini
782 from typing import Generic, TypeVar
783 T = TypeVar("T")
784 class Class(Generic[T]):
785 def __init__(self, one: T): ...
786 def __call__(self, two: T) -> int: ...
787 reveal_type(Class("hi")("there")) # N: Revealed type is "builtins.str"
788 instance = Class(3.14)
789 reveal_type(instance(2)) # N: Revealed type is "builtins.float"
790
791 [file mypy.ini]
792 \[mypy]
793 plugins=<ROOT>/test-data/unit/plugins/callable_instance.py
794
795 [case testGetMethodHooksOnUnions]
796 # flags: --config-file tmp/mypy.ini --no-strict-optional
797 from typing import Union
798
799 class Foo:
800 def meth(self, x: str) -> str: ...
801 class Bar:
802 def meth(self, x: int) -> float: ...
803 class Other:
804 meth: int
805
806 x: Union[Foo, Bar, Other]
807 if isinstance(x.meth, int):
808 reveal_type(x.meth) # N: Revealed type is "builtins.int"
809 else:
810 reveal_type(x.meth(int())) # N: Revealed type is "builtins.int"
811
812 [builtins fixtures/isinstancelist.pyi]
813 [file mypy.ini]
814 \[mypy]
815 plugins=<ROOT>/test-data/unit/plugins/union_method.py
816
817 [case testGetMethodHooksOnUnionsStrictOptional]
818 # flags: --config-file tmp/mypy.ini
819 from typing import Union
820
821 class Foo:
822 def meth(self, x: str) -> str: ...
823 class Bar:
824 def meth(self, x: int) -> float: ...
825 class Other:
826 meth: int
827
828 x: Union[Foo, Bar, Other]
829 if isinstance(x.meth, int):
830 reveal_type(x.meth) # N: Revealed type is "builtins.int"
831 else:
832 reveal_type(x.meth(int())) # N: Revealed type is "builtins.int"
833
834 [builtins fixtures/isinstancelist.pyi]
835 [file mypy.ini]
836 \[mypy]
837 plugins=<ROOT>/test-data/unit/plugins/union_method.py
838
839 [case testGetMethodHooksOnUnionsSpecial]
840 # flags: --config-file tmp/mypy.ini
841 from typing import Union
842
843 class Foo:
844 def __getitem__(self, x: str) -> str: ...
845 class Bar:
846 def __getitem__(self, x: int) -> float: ...
847
848 x: Union[Foo, Bar]
849 reveal_type(x[int()]) # N: Revealed type is "builtins.int"
850
851 [builtins fixtures/isinstancelist.pyi]
852 [file mypy.ini]
853 \[mypy]
854 plugins=<ROOT>/test-data/unit/plugins/union_method.py
855
856 [case testPluginDependencies]
857 # flags: --config-file tmp/mypy.ini
858
859 # The top level file here doesn't do anything, but the plugin should add
860 # a dependency on err that will cause err to be processed and an error reported.
861
862 [file err.py]
863 1 + 'lol' # E: Unsupported operand types for + ("int" and "str")
864
865 [file mypy.ini]
866 \[mypy]
867 plugins=<ROOT>/test-data/unit/plugins/depshook.py
868
869 [case testCustomizeMroTrivial]
870 # flags: --config-file tmp/mypy.ini
871 class A: pass
872 [file mypy.ini]
873 \[mypy]
874 plugins=<ROOT>/test-data/unit/plugins/customize_mro.py
875
876 [case testDescriptorMethods]
877 # flags: --config-file tmp/mypy.ini
878
879 class Desc:
880 def __get__(self, obj, cls):
881 pass
882
883 def __set__(self, obj, val):
884 pass
885
886 class Cls:
887 attr = Desc()
888
889 reveal_type(Cls().attr) # N: Revealed type is "builtins.int"
890 reveal_type(Cls.attr) # N: Revealed type is "builtins.str"
891
892 Cls().attr = 3
893 Cls().attr = "foo" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
894
895 [file mypy.ini]
896 \[mypy]
897 plugins=<ROOT>/test-data/unit/plugins/descriptor.py
898
899 [case testFunctionSigPluginFile]
900 # flags: --config-file tmp/mypy.ini
901
902 def dynamic_signature(arg1: str) -> str: ...
903 a: int = 1
904 reveal_type(dynamic_signature(a)) # N: Revealed type is "builtins.int"
905 b: bytes = b'foo'
906 reveal_type(dynamic_signature(b)) # N: Revealed type is "builtins.bytes"
907 [file mypy.ini]
908 \[mypy]
909 plugins=<ROOT>/test-data/unit/plugins/function_sig_hook.py
910
911 [case testPluginCalledCorrectlyWhenMethodInDecorator]
912 # flags: --config-file tmp/mypy.ini
913 from typing import TypeVar, Callable
914
915 T = TypeVar('T')
916 class Foo:
917 def a(self, x: Callable[[], T]) -> Callable[[], T]: ...
918
919 b = Foo()
920
921 @b.a
922 def f() -> None:
923 pass
924
925 reveal_type(f()) # N: Revealed type is "builtins.str"
926
927 [file mypy.ini]
928 \[mypy]
929 plugins=<ROOT>/test-data/unit/plugins/method_in_decorator.py
930
931 [case testClassAttrPluginClassVar]
932 # flags: --config-file tmp/mypy.ini
933
934 from typing import Type
935
936 class Cls:
937 attr = 'test'
938 unchanged = 'test'
939
940 reveal_type(Cls().attr) # N: Revealed type is "builtins.str"
941 reveal_type(Cls.attr) # N: Revealed type is "builtins.int"
942 reveal_type(Cls.unchanged) # N: Revealed type is "builtins.str"
943 x: Type[Cls]
944 reveal_type(x.attr) # N: Revealed type is "builtins.int"
945 [file mypy.ini]
946 \[mypy]
947 plugins=<ROOT>/test-data/unit/plugins/class_attr_hook.py
948
949 [case testClassAttrPluginMethod]
950 # flags: --config-file tmp/mypy.ini
951
952 class Cls:
953 def attr(self) -> None:
954 pass
955
956 reveal_type(Cls.attr) # N: Revealed type is "builtins.int"
957 [file mypy.ini]
958 \[mypy]
959 plugins=<ROOT>/test-data/unit/plugins/class_attr_hook.py
960
961 [case testClassAttrPluginEnum]
962 # flags: --config-file tmp/mypy.ini
963
964 import enum
965
966 class Cls(enum.Enum):
967 attr = 'test'
968
969 reveal_type(Cls.attr) # N: Revealed type is "builtins.int"
970 [builtins fixtures/enum.pyi]
971 [file mypy.ini]
972 \[mypy]
973 plugins=<ROOT>/test-data/unit/plugins/class_attr_hook.py
974
975 [case testClassAttrPluginMetaclassAnyBase]
976 # flags: --config-file tmp/mypy.ini
977
978 from typing import Any, Type
979 class M(type):
980 attr = 'test'
981
982 B: Any
983 class Cls(B, metaclass=M):
984 pass
985
986 reveal_type(Cls.attr) # N: Revealed type is "builtins.int"
987 [file mypy.ini]
988 \[mypy]
989 plugins=<ROOT>/test-data/unit/plugins/class_attr_hook.py
990
991 [case testClassAttrPluginMetaclassRegularBase]
992 # flags: --config-file tmp/mypy.ini
993
994 from typing import Any, Type
995 class M(type):
996 attr = 'test'
997
998 class B:
999 attr = b'bytes'
1000
1001 class Cls(B, metaclass=M):
1002 pass
1003
1004 reveal_type(Cls.attr) # N: Revealed type is "builtins.int"
1005 [file mypy.ini]
1006 \[mypy]
1007 plugins=<ROOT>/test-data/unit/plugins/class_attr_hook.py
1008
1009 [case testClassAttrPluginPartialType]
1010 # flags: --config-file tmp/mypy.ini --no-local-partial-types
1011
1012 class Cls:
1013 attr = None
1014 def f(self) -> int:
1015 return Cls.attr + 1
1016
1017 [file mypy.ini]
1018 \[mypy]
1019 plugins=<ROOT>/test-data/unit/plugins/class_attr_hook.py
1020
1021 [case testAddClassMethodPlugin]
1022 # flags: --config-file tmp/mypy.ini
1023 class BaseAddMethod: pass
1024
1025 class MyClass(BaseAddMethod):
1026 pass
1027
1028 reveal_type(MyClass.foo_classmethod) # N: Revealed type is "def ()"
1029 reveal_type(MyClass.foo_staticmethod) # N: Revealed type is "def (builtins.int) -> builtins.str"
1030
1031 my_class = MyClass()
1032 reveal_type(my_class.foo_classmethod) # N: Revealed type is "def ()"
1033 reveal_type(my_class.foo_staticmethod) # N: Revealed type is "def (builtins.int) -> builtins.str"
1034 [file mypy.ini]
1035 \[mypy]
1036 plugins=<ROOT>/test-data/unit/plugins/add_classmethod.py
1037
1038 [case testAddOverloadedMethodPlugin]
1039 # flags: --config-file tmp/mypy.ini
1040 class AddOverloadedMethod: pass
1041
1042 class MyClass(AddOverloadedMethod):
1043 pass
1044
1045 reveal_type(MyClass.method) # N: Revealed type is "Overload(def (self: __main__.MyClass, arg: builtins.int) -> builtins.str, def (self: __main__.MyClass, arg: builtins.str) -> builtins.int)"
1046 reveal_type(MyClass.clsmethod) # N: Revealed type is "Overload(def (arg: builtins.int) -> builtins.str, def (arg: builtins.str) -> builtins.int)"
1047 reveal_type(MyClass.stmethod) # N: Revealed type is "Overload(def (arg: builtins.int) -> builtins.str, def (arg: builtins.str) -> builtins.int)"
1048
1049 my_class = MyClass()
1050 reveal_type(my_class.method) # N: Revealed type is "Overload(def (arg: builtins.int) -> builtins.str, def (arg: builtins.str) -> builtins.int)"
1051 reveal_type(my_class.clsmethod) # N: Revealed type is "Overload(def (arg: builtins.int) -> builtins.str, def (arg: builtins.str) -> builtins.int)"
1052 reveal_type(my_class.stmethod) # N: Revealed type is "Overload(def (arg: builtins.int) -> builtins.str, def (arg: builtins.str) -> builtins.int)"
1053 [file mypy.ini]
1054 \[mypy]
1055 plugins=<ROOT>/test-data/unit/plugins/add_overloaded_method.py
1056
1057 [case testAddMethodPluginExplicitOverride]
1058 # flags: --python-version 3.12 --config-file tmp/mypy.ini
1059 from typing import override, TypeVar
1060
1061 T = TypeVar('T', bound=type)
1062
1063 def inject_foo(t: T) -> T:
1064 # Imitates:
1065 # t.foo_implicit = some_method
1066 return t
1067
1068 class BaseWithoutFoo: pass
1069
1070 @inject_foo
1071 class ChildWithFoo(BaseWithoutFoo): pass
1072 reveal_type(ChildWithFoo.foo_implicit) # N: Revealed type is "def (self: __main__.ChildWithFoo)"
1073
1074 @inject_foo
1075 class SomeWithFoo(ChildWithFoo): pass
1076 reveal_type(SomeWithFoo.foo_implicit) # N: Revealed type is "def (self: __main__.SomeWithFoo)"
1077
1078 class ExplicitOverride(SomeWithFoo):
1079 @override
1080 def foo_implicit(self) -> None: pass
1081
1082 class ImplicitOverride(SomeWithFoo):
1083 def foo_implicit(self) -> None: pass # E: Method "foo_implicit" is not using @override but is overriding a method in class "__main__.SomeWithFoo"
1084 [file mypy.ini]
1085 \[mypy]
1086 plugins=<ROOT>/test-data/unit/plugins/add_method.py
1087 enable_error_code = explicit-override
1088 [typing fixtures/typing-override.pyi]
1089
1090 [case testCustomErrorCodePlugin]
1091 # flags: --config-file tmp/mypy.ini --show-error-codes
1092 def main() -> int:
1093 return 2
1094
1095 main() # E: Custom error [custom]
1096 reveal_type(1) # N: Revealed type is "Literal[1]?"
1097
1098 [file mypy.ini]
1099 \[mypy]
1100 plugins=<ROOT>/test-data/unit/plugins/custom_errorcode.py
1101
1102
1103 [case testPyprojectPluginsTrailingComma]
1104 # flags: --config-file tmp/pyproject.toml
1105 [file pyproject.toml]
1106 # This test checks that trailing commas in string-based `plugins` are allowed.
1107 \[tool.mypy]
1108 plugins = """
1109 <ROOT>/test-data/unit/plugins/function_sig_hook.py,
1110 <ROOT>/test-data/unit/plugins/method_in_decorator.py,
1111 """
1112 [out]
1113
1114
1115
1116 [case magicMethodReverse]
1117 # flags: --config-file tmp/mypy.ini
1118 from typing import Literal
1119
1120 op1: Literal[3] = 3
1121 op2: Literal[4] = 4
1122 c = op1 + op2
1123 reveal_type(c) # N: Revealed type is "Literal[7]"
1124
1125 [file mypy.ini]
1126 \[mypy]
1127 plugins=<ROOT>/test-data/unit/plugins/magic_method.py
1128 [builtins fixtures/ops.pyi]