Debian Codegraph @

test-data/unit/reports.test

raw
1 -- Tests for reports
2 --
3 -- This file follows syntax of cmdline.test.
4
5 [case testConfigErrorUnknownReport]
6 # cmd: mypy -c pass
7 [file mypy.ini]
8 \[mypy]
9 bad_report = .
10 [out]
11 mypy.ini: [mypy]: Unrecognized report type: bad_report
12 == Return code: 0
13
14 [case testCoberturaParser]
15 # cmd: mypy --cobertura-xml-report build pkg
16 [file pkg/__init__.py]
17 [file pkg/a.py]
18 from typing import Dict
19
20 def foo() -> Dict:
21 z = {'hello': 'world'}
22 return z
23 [file pkg/subpkg/__init__.py]
24 [file pkg/subpkg/a.py]
25 def bar() -> str:
26 return 'world'
27 def untyped_function():
28 return 42
29 [outfile build/cobertura.xml]
30 <coverage timestamp="$TIMESTAMP" version="$VERSION" line-rate="0.7500" branch-rate="0" lines-covered="6" lines-valid="8">
31 <sources>
32 <source>$PWD</source>
33 </sources>
34 <packages>
35 <package complexity="1.0" name="pkg" branch-rate="0" line-rate="1.0000">
36 <classes>
37 <class complexity="1.0" filename="pkg/__init__.py" name="__init__.py" branch-rate="0" line-rate="1.0">
38 <methods/>
39 <lines/>
40 </class>
41 <class complexity="1.0" filename="pkg/a.py" name="a.py" branch-rate="0" line-rate="1.0000">
42 <methods/>
43 <lines>
44 <line branch="false" hits="1" number="1" precision="precise"/>
45 <line branch="true" hits="1" number="3" precision="imprecise" condition-coverage="50% (1/2)"/>
46 <line branch="false" hits="1" number="4" precision="precise"/>
47 <line branch="false" hits="1" number="5" precision="precise"/>
48 </lines>
49 </class>
50 </classes>
51 </package>
52 <package complexity="1.0" name="pkg.subpkg" branch-rate="0" line-rate="0.5000">
53 <classes>
54 <class complexity="1.0" filename="pkg/subpkg/__init__.py" name="__init__.py" branch-rate="0" line-rate="1.0">
55 <methods/>
56 <lines/>
57 </class>
58 <class complexity="1.0" filename="pkg/subpkg/a.py" name="a.py" branch-rate="0" line-rate="0.5000">
59 <methods/>
60 <lines>
61 <line branch="false" hits="1" number="1" precision="precise"/>
62 <line branch="false" hits="1" number="2" precision="precise"/>
63 <line branch="false" hits="0" number="3" precision="any"/>
64 <line branch="false" hits="0" number="4" precision="any"/>
65 </lines>
66 </class>
67 </classes>
68 </package>
69 </packages>
70 </coverage>
71
72 [case testCoberturaStarUnpacking]
73 # cmd: mypy --cobertura-xml-report build a.py
74 [file a.py]
75 from typing import TypedDict
76
77 class MyDict(TypedDict):
78 a: int
79
80 def foo(a: int) -> MyDict:
81 return {"a": a}
82 md: MyDict = MyDict(**foo(42))
83 [outfile build/cobertura.xml]
84 <coverage timestamp="$TIMESTAMP" version="$VERSION" line-rate="1.0000" branch-rate="0" lines-covered="6" lines-valid="6">
85 <sources>
86 <source>$PWD</source>
87 </sources>
88 <packages>
89 <package complexity="1.0" name="a" branch-rate="0" line-rate="1.0000">
90 <classes>
91 <class complexity="1.0" filename="a.py" name="a.py" branch-rate="0" line-rate="1.0000">
92 <methods/>
93 <lines>
94 <line branch="false" hits="1" number="1" precision="precise"/>
95 <line branch="false" hits="1" number="3" precision="precise"/>
96 <line branch="false" hits="1" number="4" precision="precise"/>
97 <line branch="false" hits="1" number="6" precision="precise"/>
98 <line branch="false" hits="1" number="7" precision="precise"/>
99 <line branch="false" hits="1" number="8" precision="precise"/>
100 </lines>
101 </class>
102 </classes>
103 </package>
104 </packages>
105 </coverage>
106
107 [case testAnyExprReportDivisionByZero]
108 # cmd: mypy --any-exprs-report=out -c 'pass'
109
110 [case testClassDefIsNotTreatedAsEmpty]
111 # cmd: mypy --html-report report n.py
112 [file n.py]
113 class A(object):
114 pass # line indented with tab; hex 1f here: ()
115
116 [file report/mypy-html.css]
117 [file report/index.html]
118 [outfile report/html/n.py.html]
119 <html>
120 <head>
121 <meta charset="UTF-8">
122 <link rel="stylesheet" type="text/css" href="../mypy-html.css">
123 </head>
124 <body>
125 <h2>n</h2>
126 <table>
127 <caption>n.py</caption>
128 <tbody><tr>
129 <td class="table-lines"><pre><span id="L1" class="lineno"><a class="lineno" href="#L1">1</a></span>
130 <span id="L2" class="lineno"><a class="lineno" href="#L2">2</a></span>
131 </pre></td>
132 <td class="table-code"><pre><span class="line-precise" title="No Anys on this line!">class A(object):</span>
133 <span class="line-precise" title="No Anys on this line!"> pass # line indented with tab; hex 1f here: (?)</span>
134 </pre></td>
135 </tr></tbody>
136 </table>
137 </body>
138 </html>
139
140 [case testNoCrashRecursiveAliasInReport]
141 # cmd: mypy --any-exprs-report report n.py
142
143 [file n.py]
144 from typing import Union, List, Any, TypeVar
145
146 Nested = List[Union[Any, Nested]]
147 T = TypeVar("T")
148 NestedGen = List[Union[T, NestedGen[T]]]
149
150 x: Nested
151 y: NestedGen[int]
152 z: NestedGen[Any]
153
154 [file report/any-exprs.txt]
155 [outfile report/types-of-anys.txt]
156 Name Unannotated Explicit Unimported Omitted Generics Error Special Form Implementation Artifact
157 -----------------------------------------------------------------------------------------------------------------
158 n 0 2 0 8 0 0 0
159 -----------------------------------------------------------------------------------------------------------------
160 Total 0 2 0 8 0 0 0
161
162 [case testTypeVarTreatedAsEmptyLine]
163 # cmd: mypy --html-report report n.py
164
165 [file n.py]
166 from typing import TypeVar
167
168 T = TypeVar('T')
169
170 [file report/mypy-html.css]
171 [file report/index.html]
172 [outfile report/html/n.py.html]
173 <html>
174 <head>
175 <meta charset="UTF-8">
176 <link rel="stylesheet" type="text/css" href="../mypy-html.css">
177 </head>
178 <body>
179 <h2>n</h2>
180 <table>
181 <caption>n.py</caption>
182 <tbody><tr>
183 <td class="table-lines"><pre><span id="L1" class="lineno"><a class="lineno" href="#L1">1</a></span>
184 <span id="L2" class="lineno"><a class="lineno" href="#L2">2</a></span>
185 <span id="L3" class="lineno"><a class="lineno" href="#L3">3</a></span>
186 </pre></td>
187 <td class="table-code"><pre><span class="line-precise" title="No Anys on this line!">from typing import TypeVar</span>
188 <span class="line-empty" title="No Anys on this line!"></span>
189 <span class="line-empty" title="No Anys on this line!">T = TypeVar('T')</span>
190 </pre></td>
191 </tr></tbody>
192 </table>
193 </body>
194 </html>
195
196 [case testUnreachableCodeMarkedAsAny]
197 # cmd: mypy --html-report report n.py
198
199 [file any.py]
200 from typing import Any
201 def any_f(x: Any) -> None:
202 pass
203
204 [file n.py]
205 from any import any_f
206 def bar(x):
207 # type: (str) -> None
208 any_f(x)
209 assert False
210 any_f(x)
211
212 [file report/mypy-html.css]
213 [file report/index.html]
214 [outfile report/html/n.py.html]
215 <html>
216 <head>
217 <meta charset="UTF-8">
218 <link rel="stylesheet" type="text/css" href="../mypy-html.css">
219 </head>
220 <body>
221 <h2>n</h2>
222 <table>
223 <caption>n.py</caption>
224 <tbody><tr>
225 <td class="table-lines"><pre><span id="L1" class="lineno"><a class="lineno" href="#L1">1</a></span>
226 <span id="L2" class="lineno"><a class="lineno" href="#L2">2</a></span>
227 <span id="L3" class="lineno"><a class="lineno" href="#L3">3</a></span>
228 <span id="L4" class="lineno"><a class="lineno" href="#L4">4</a></span>
229 <span id="L5" class="lineno"><a class="lineno" href="#L5">5</a></span>
230 <span id="L6" class="lineno"><a class="lineno" href="#L6">6</a></span>
231 </pre></td>
232 <td class="table-code"><pre><span class="line-precise" title="No Anys on this line!">from any import any_f</span>
233 <span class="line-precise" title="No Anys on this line!">def bar(x):</span>
234 <span class="line-empty" title="No Anys on this line!"> # type: (str) -&gt; None</span>
235 <span class="line-any" title="Any Types on this line:
236 Explicit (x1)"> any_f(x)</span>
237 <span class="line-precise" title="No Anys on this line!"> assert False</span>
238 <span class="line-unanalyzed" title="No Anys on this line!"> any_f(x)</span>
239 </pre></td>
240 </tr></tbody>
241 </table>
242 </body>
243 </html>
244
245 [case testHtmlReportMemberExprNoUnanalyzed]
246 # cmd: mypy --html-report report n.py
247
248 [file n.py]
249 import sys
250
251 old_stdout = sys.stdout
252
253 [file report/mypy-html.css]
254 [file report/index.html]
255 [outfile report/html/n.py.html]
256 <html>
257 <head>
258 <meta charset="UTF-8">
259 <link rel="stylesheet" type="text/css" href="../mypy-html.css">
260 </head>
261 <body>
262 <h2>n</h2>
263 <table>
264 <caption>n.py</caption>
265 <tbody><tr>
266 <td class="table-lines"><pre><span id="L1" class="lineno"><a class="lineno" href="#L1">1</a></span>
267 <span id="L2" class="lineno"><a class="lineno" href="#L2">2</a></span>
268 <span id="L3" class="lineno"><a class="lineno" href="#L3">3</a></span>
269 </pre></td>
270 <td class="table-code"><pre><span class="line-precise" title="No Anys on this line!">import sys</span>
271 <span class="line-empty" title="No Anys on this line!"></span>
272 <span class="line-precise" title="No Anys on this line!">old_stdout = sys.stdout</span>
273 </pre></td>
274 </tr></tbody>
275 </table>
276 </body>
277 </html>
278
279 [case testAnyExprReportIncludesDeadCode]
280 # cmd: mypy --any-exprs-report report i.py j.py
281
282 [file i.py]
283 def bar(x):
284 # type: (str) -> None
285 print(x)
286 assert False
287 print(x) # dead code!
288
289 [file j.py]
290 def bar(x):
291 # type: (str) -> None
292 print(x)
293 assert False
294
295 [file report/types-of-anys.txt]
296 [outfile report/any-exprs.txt]
297 Name Anys Exprs Coverage
298 ---------------------------------
299 i 1 6 83.33%
300 j 0 5 100.00%
301 ---------------------------------
302 Total 1 11 90.91%
303
304 [case testAnyExprReportHigherKindedTypesAreNotAny]
305 # cmd: mypy --any-exprs-report report i.py
306
307 [file i.py]
308 from enum import Enum
309 from typing import NewType, NamedTuple, TypedDict, TypeVar
310
311 T = TypeVar('T') # no error
312
313 def f(t: T) -> T:
314 return t
315
316 Point = NamedTuple('Point', [('x', int), ('y', int)]) # no error
317
318 def origin() -> Point:
319 return Point(x=0, y=0)
320
321 NT = NewType('NT', int) # no error
322
323 def nt() -> NT:
324 return NT(1)
325
326 E = Enum('E', '1, 2, 3') # no error
327
328 def k(s: E) -> None: pass
329
330 Movie = TypedDict('Movie', {'name': str, 'year': int})
331
332 def g(m: Movie) -> Movie:
333 return m
334
335 [file report/types-of-anys.txt]
336 [outfile report/any-exprs.txt]
337 Name Anys Exprs Coverage
338 ---------------------------------
339 i 0 14 100.00%
340 ---------------------------------
341 Total 0 14 100.00%
342
343
344 [case testAnyExpressionsReportTypesOfAny]
345 # cmd: mypy --any-exprs-report report n.py
346
347 [file n.py]
348 from typing import Any, List
349 from nonexistent import C # type: ignore
350
351 def any_f(x: Any) -> None: # Explicit
352 pass
353
354 def a(x) -> None: # Unannotated
355 any_f(x)
356
357 x: Any = 2 # Explicit
358 y: C = None # Unimported
359
360 def b() -> List: # Omitted Generics
361 return [1, 2, 3]
362
363 g = 1
364 z = g.does_not_exist() # type: ignore # Error
365
366
367 [file report/any-exprs.txt]
368 [outfile report/types-of-anys.txt]
369 Name Unannotated Explicit Unimported Omitted Generics Error Special Form Implementation Artifact
370 -----------------------------------------------------------------------------------------------------------------
371 n 2 3 1 1 3 0 0
372 -----------------------------------------------------------------------------------------------------------------
373 Total 2 3 1 1 3 0 0
374
375 [case testAnyExpressionsReportUnqualifiedError]
376 # cmd: mypy --any-exprs-report report n.py
377
378 [file n.py]
379 z = does_not_exist() # type: ignore # Error
380
381 [file report/any-exprs.txt]
382 [outfile report/types-of-anys.txt]
383 Name Unannotated Explicit Unimported Omitted Generics Error Special Form Implementation Artifact
384 -----------------------------------------------------------------------------------------------------------------
385 n 0 0 0 0 3 0 0
386 -----------------------------------------------------------------------------------------------------------------
387 Total 0 0 0 0 3 0 0
388
389 [case testAnyExpressionsReportUntypedDef]
390 # cmd: mypy --any-exprs-report report n.py
391
392 [file n.py]
393 def foo():
394 x = 0
395 f = 0
396
397 [file report/any-exprs.txt]
398 [outfile report/types-of-anys.txt]
399 Name Unannotated Explicit Unimported Omitted Generics Error Special Form Implementation Artifact
400 -----------------------------------------------------------------------------------------------------------------
401 n 0 0 0 0 0 0 0
402 -----------------------------------------------------------------------------------------------------------------
403 Total 0 0 0 0 0 0 0
404 [case testTrickyCoverage]
405 # cmd: mypy --linecoverage-report=report n.py
406 [file n.py]
407 def blah(x): return x
408
409 @blah
410
411 def f(x: int) -> None: pass
412
413 class Foo:
414 @blah
415 #hi
416 def f(self, x: int) -> None:
417 pass
418
419 @blah
420 class Z(object):
421 pass
422
423 [case testCoverageIgnoresCache]
424 -- Performs two runs to verify that cached information does not prevent
425 -- modules from being included in reports.
426 # cmd: mypy --linecount-report report a.py
427 [file a.py]
428 empty = False
429 [out]
430 [out2]
431 [outfile report/linecount.txt]
432 1 1 0 0 total
433 1 1 0 0 a
434
435 [case testAnyExprReportIgnoresSpecialForms]
436 # cmd: mypy --any-exprs-report report i.py j.py k.py l.py
437
438 [file i.py]
439 async def some_function() -> None:
440 pass
441
442 [file j.py]
443 from typing import Any
444
445 async def some_function() -> Any:
446 pass
447
448 [file k.py]
449 from typing import NamedTuple
450
451 def a() -> None:
452 _FuzzyMatch(0, 0)
453
454 _FuzzyMatch = NamedTuple('_FuzzyMatch', [
455 ('match_length', int),
456 ('start_pos', int),
457 ])
458
459 def b() -> None:
460 _FuzzyMatch(0, 0)
461
462 [file l.py]
463 async def some_function(x) -> None:
464 pass
465
466 [file report/any-exprs.txt]
467 [outfile report/types-of-anys.txt]
468 Name Unannotated Explicit Unimported Omitted Generics Error Special Form Implementation Artifact
469 -----------------------------------------------------------------------------------------------------------------
470 i 0 0 0 0 0 0 0
471 j 0 1 0 0 0 0 0
472 k 0 0 0 0 0 0 0
473 l 1 0 0 0 0 0 0
474 -----------------------------------------------------------------------------------------------------------------
475 Total 1 1 0 0 0 0 0
476
477 [case testSpecialAnyHtmlReport]
478 # cmd: mypy --html-report report n.py
479 [file n.py]
480 from typing import Callable
481
482 SourceToDisplay = Callable[[int], int]
483 DisplayToSource = Callable[[int], int]
484
485 [file report/mypy-html.css]
486 [file report/index.html]
487 [outfile report/html/n.py.html]
488 <html>
489 <head>
490 <meta charset="UTF-8">
491 <link rel="stylesheet" type="text/css" href="../mypy-html.css">
492 </head>
493 <body>
494 <h2>n</h2>
495 <table>
496 <caption>n.py</caption>
497 <tbody><tr>
498 <td class="table-lines"><pre><span id="L1" class="lineno"><a class="lineno" href="#L1">1</a></span>
499 <span id="L2" class="lineno"><a class="lineno" href="#L2">2</a></span>
500 <span id="L3" class="lineno"><a class="lineno" href="#L3">3</a></span>
501 <span id="L4" class="lineno"><a class="lineno" href="#L4">4</a></span>
502 </pre></td>
503 <td class="table-code"><pre><span class="line-precise" title="No Anys on this line!">from typing import Callable</span>
504 <span class="line-empty" title="No Anys on this line!"></span>
505 <span class="line-precise" title="No Anys on this line!">SourceToDisplay = Callable[[int], int]</span>
506 <span class="line-precise" title="No Anys on this line!">DisplayToSource = Callable[[int], int]</span>
507 </pre></td>
508 </tr></tbody>
509 </table>
510 </body>
511 </html>
512
513 [case testHtmlReportOnNamespacePackagesWithExplicitBases]
514 # cmd: mypy --html-report report -p folder
515 [file folder/subfolder/something.py]
516 class Something:
517 pass
518 [file folder/main.py]
519 from .subfolder.something import Something
520 print(Something())
521 [file folder/__init__.py]
522 [file mypy.ini]
523 \[mypy]
524 explicit_package_bases = True
525 namespace_packages = True
526
527 [file report/mypy-html.css]
528 [file report/index.html]
529 [outfile report/html/folder/subfolder/something.py.html]
530 <html>
531 <head>
532 <meta charset="UTF-8">
533 <link rel="stylesheet" type="text/css" href="../../../mypy-html.css">
534 </head>
535 <body>
536 <h2>folder.subfolder.something</h2>
537 <table>
538 <caption>folder/subfolder/something.py</caption>
539 <tbody><tr>
540 <td class="table-lines"><pre><span id="L1" class="lineno"><a class="lineno" href="#L1">1</a></span>
541 <span id="L2" class="lineno"><a class="lineno" href="#L2">2</a></span>
542 </pre></td>
543 <td class="table-code"><pre><span class="line-precise" title="No Anys on this line!">class Something:</span>
544 <span class="line-precise" title="No Anys on this line!"> pass</span>
545 </pre></td>
546 </tr></tbody>
547 </table>
548 </body>
549 </html>
550
551 [case testLinecountReportCrashOnNamespacePackages]
552 # cmd: mypy --linecount-report report -p folder
553 -- Regression test for https://github.com/python/mypy/issues/15979
554 [file folder/subfolder/something.py]
555 class Something:
556 pass
557
558 [case testReportIsADirectoryErrorCrashOnNamespacePackages]
559 # cmd: mypy --linecoverage-report report -p folder
560 -- Regression test for https://github.com/python/mypy/issues/18128
561 -- "IsADirectoryError for namespace packages when using --linecoverage-report"
562 [file folder/subfolder/something.py]
563 class Something:
564 pass
565
566 [case testReportCoberturaCrashOnNamespacePackages]
567 # cmd: mypy --cobertura-xml-report report -p folder
568 -- Regression test for https://github.com/python/mypy/issues/19843
569 [file folder/subfolder/something.py]
570 -- This output is not important, but due to the way tests are run we need to check it.
571 [outfile report/cobertura.xml]
572 <coverage timestamp="$TIMESTAMP" version="$VERSION" line-rate="1.0" branch-rate="0" lines-covered="0" lines-valid="0">
573 <sources>
574 <source>$PWD</source>
575 </sources>
576 <packages>
577 <package complexity="1.0" name="folder" branch-rate="0" line-rate="1.0">
578 <classes>
579 <class complexity="1.0" filename="folder" name="folder" branch-rate="0" line-rate="1.0">
580 <methods/>
581 <lines/>
582 </class>
583 <class complexity="1.0" filename="folder/subfolder" name="subfolder" branch-rate="0" line-rate="1.0">
584 <methods/>
585 <lines/>
586 </class>
587 </classes>
588 </package>
589 <package complexity="1.0" name="folder.subfolder" branch-rate="0" line-rate="1.0">
590 <classes>
591 <class complexity="1.0" filename="folder/subfolder/something.py" name="something.py" branch-rate="0" line-rate="1.0">
592 <methods/>
593 <lines/>
594 </class>
595 </classes>
596 </package>
597 </packages>
598 </coverage>