File: README.md

package info (click to toggle)
pyannotate 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 384 kB
  • sloc: python: 5,001; makefile: 5
file content (77 lines) | stat: -rw-r--r-- 1,470 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
PyAnnotate example
==================

To play with this example, first install PyAnnotate:

```
pip install pyannotate
```

Then run the driver.py file:

```
python driver.py
```

Expected contents of type_info.json (after running driver.py):

```
[
    {
        "path": "gcd.py",
        "line": 1,
        "func_name": "main",
        "type_comments": [
            "() -> None"
        ],
        "samples": 1
    },
    {
        "path": "gcd.py",
        "line": 5,
        "func_name": "gcd",
        "type_comments": [
            "(int, int) -> int"
        ],
        "samples": 2
    }
]
```

Now run the pyannotate tool, like this (note the -w flag -- without
this it won't update the file):

```
pyannotate -w gcd.py
```

Expected output:

```
Refactored gcd.py
--- gcd.py        (original)
+++ gcd.py        (refactored)
@@ -1,8 +1,10 @@
 def main():
+    # type: () -> None
     print(gcd(15, 10))
     print(gcd(45, 12))
 
 def gcd(a, b):
+    # type: (int, int) -> int
     while b:
         a, b = b, a%b
     return a
Files that were modified:
gcd.py
```

Alternative, using pytest
-------------------------

For pytest users, the example_conftest.py file shows how to
automatically configures pytest to collect types when running tests.
The test_gcd.py file contains a simple test to demonstrate this.  Copy
the contents of example_conftest.py to your conftest.py file and run
pytest; it will then generate a type_info.json file like the one
above.