dropbox / pyannotate
- пятница, 17 ноября 2017 г. в 03:15:16
Auto-generate PEP-484 annotations
Insert annotations into your source code based on call arguments and return types observed at runtime.
For license and copyright see the end of this file.
See also the example directory.
from pyannotate_runtime import collect_types
to your testcollect_types.init_types_collection()
collect_types.resume()
and
collect_types.pause()
collect_types.dump_stats(filename)
All calls between the pause()
and resume()
calls will be analyzed
and the observed types will be written (in JSON form) to the filename
you pass to dump_stats()
. You can have multiple pause/resume pairs
per dump call.
The command-line tool pyannotate
can add annotations into your
source code based on the annotations collected in phase 1. The key
arguments are:
--type-info FILE
to tell it the file you passed to dump_stats()
-w
to make the tool actually update your files.
(Use git or some other way to keep a backup.)At this point you should probably run mypy and iterate. You probably will have to tweak the changes to make mypy completely happy.
This should work for Python 2.7 as well as for Python 3.6 and higher. (It works for Python 3.4 and 3.5 as well, except for Python 3.5.1.)
pip install pyannotate
This installs several items:
A runtime module, pyannotate_runtime/collect_types.py, which collects and dumps types observed at runtime using a profiling hook.
A library package, pyannotate_tools, containing code that can read the data dumped by the runtime module and insert annotations into your source code.
An entry point, pyannotate, which runs the library package on your files.
For dependencies, see setup.py and requirements.txt.
To run the unit tests, use pytest:
pytest
We'd love your help with some of these issues:
The following people contributed significantly to this tool: