Skip to content

Commit

Permalink
Clarify the example in README (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrinwu authored and carljm committed Jan 1, 2018
1 parent 478b3cb commit c30587d
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,10 @@ MonkeyType collects runtime types of function arguments and return values, and
can automatically generate stub files or even add draft type annotations
directly to your Python code based on the types collected at runtime.

Examples
--------

Run a script under call-trace logging of functions and methods in all imported
modules::

$ monkeytype run myscript.py

Or enable call-trace logging for a block of code::

import monkeytype

with monkeytype.trace():
...

By default this will dump call traces into a sqlite database in the file
``monkeytype.sqlite3`` in the current working directory. You can then use the
``monkeytype`` command to generate a stub file for a module, or apply the type
annotations directly to your code::

$ monkeytype stub some.module
$ monkeytype apply some.module
Example
-------

If ``some/module.py`` originally contained::
Say ``some/module.py`` originally contains::

def add(a, b):
return a + b
Expand All @@ -39,24 +19,33 @@ And ``myscript.py`` contains::

add(1, 2)

Then you'd get a stub like this from ``monkeytype stub some.module``::
Now we want to infer the type annotation of ``add`` in ``some/module.py`` by
running ``myscript.py`` with ``MonkeyType``. One way is to run::

$ monkeytype run myscript.py

By default this will dump call traces into a sqlite database in the file
``monkeytype.sqlite3`` in the current working directory. You can then use the
``monkeytype`` command to generate a stub file for a module, or apply the type
annotations directly to your code.

Running ``monkeytype stub some.module`` will output a stub::

def add(a: int, b: int) -> int: ...

And if you run ``monkeytype apply some.module``, ``some/module.py`` will be
modified to::
Running ``monkeytype apply some.module`` will modify ``some/module.py`` to::

def add(a: int, b: int) -> int:
return a + b

This example demonstrates both the value and the limitations of
MonkeyType. With MonkeyType, it's very easy to add type annotations that
MonkeyType. With MonkeyType, it's very easy to add annotations that
reflect the concrete types you use at runtime, but those annotations may not
always match the full intended capability of the functions (e.g. this ``add``
function is capable of handling many more types than just integers, or
MonkeyType may generate a ``List`` annotation where ``Sequence`` or
``Iterable`` would be more appropriate). MonkeyType's annotations are intended
as a first draft, to be checked and corrected by a developer.
always match the full intended capability of the functions. For instance, ``add``
is capable of handling many more types than just integers. Similarly, MonkeyType
may generate a concrete ``List`` annotation where an abstract ``Sequence`` or
``Iterable`` would be more appropriate. MonkeyType's annotations are an
informative first draft, to be checked and corrected by a developer.

Requirements
------------
Expand Down

0 comments on commit c30587d

Please sign in to comment.