Package: typeright
typeright: Generate Python Type AnnotationsInsert PEP 484 type annotations into your python source code.Optionstyperight currently supports 4 approaches (aka "fixers") for inserting
annotations, listed below.Multiple fixers can be used in tandem: if an annotation exists, or is
newly added by a previous fixer, all subsequent fixers will skip the location.
The one exception to this is the docstring fixer, which will overwrite existing
annotations if they do not agree with the types found in the docstrings, if any.
In other words, when enabled, the docstrings are considered the source of truth for types.The fixers are listed below in the order that they are called.Read types from a json filejson file options:
  Read type info from a json file

  --type-info FILE      JSON input file
  --max-line-drift N    Maximum allowed line drift when inserting annotation (can be useful for custom codecs)
  --uses-signature      JSON input uses a signature format
  -s, --only-simple     Only annotate functions with trivial typesIf you have a tool that is able to generate type information, you can output
it in a format compatible with typeright and it will insert them as annotations.
The most common case for this would be if you're usingPyAnnotateto inspect types at
runtime (this project is itself a fork of PyAnnotate, focused solely on annotation
generation).  Another scenario might be generating type info based on doxygen
xml files for a C extension, and adding these to stubs created by mypy'sstubgentool.Generate types from a commandcommand options:
  Generate type info by calling an external program

  --command COMMAND, -c COMMAND
                        Command to generate JSON info for a call siteWith this option, you can use the mypy daemon,dmypy, to generate annotations.
This tool will analyze your code to determine likely types for a function
based on the types of objects passed to it throughout your code.To use this, first start the mypy daemon:dmypy runNext, invoketyperightand pass it the command to run:typeright --command='dmypy suggest --json {filename}:{lineno}' path/It will run the given command on any function inpath/that does not have annotations.
I prefer to also pass--no-anyto ensure high quality suggestions only.Convert types from docstringsdocstring options:
  Generate type info by parsing docstrings

  --doc-format {auto,google,numpy,off,rest}
                        Specify the docstring convention used within files to be converted ('auto' automatically determines the format by inspecting each docstring but it is faster and more reliable to specify this explicitly)
  --doc-default-return-type TYPE
                        Default type to use for undocumented return values (defaults to 'Any'Maintaining types in docstrings can be desirable for a few reasons:Your project has existing docstrings with types that are already mostly correctYou find it easier to maintain and comprehend types specified alongside the
description of an argumenttyperight can parse the three major docstring conventions to find type info:numpy,googleandrestructuredTextRegardless of the docstring convention you choose, the types declared within your
docstrings should following the guidelines inPEP 484,
especially use of thetypingmodule, where necessary.Set all types to AnyOne approach to typing an exiting project is to start by blanketing your code
with types, so that you can enablemypy --disallow-untyped-defsstraight out of the gate. This gets the boilerplate out of the way, and
"encourages" developers to add types to all new modules.any options:
  -a, --auto-any        Annotate everything with 'Any'Output format optionsThere are options to control how to generate the type annotations:output format options:
  --annotation-style {auto,py2,py3}
                        Choose annotation style, py2 for Python 2 with comments, py3 for Python 3 with annotation syntax. The default will be determined by the version of the current python interpreter
  --py2-comment-style {auto,multi,single}
                        Choose comment style, multi adds a comment per argument, single produces one type comment for all arguments, and auto chooses between the two styles based on the number of arguments and length of commentsOther optionsother options:
  -p, --print-function  Assume print is a function
  -w, --write           Write output files
  -j N, --processes N   Use N parallel processes (default no parallelism)
  -v, --verbose         More verbose output
  -q, --quiet           Don't show diffs
  -o OUTPUT_DIR, --output-dir OUTPUT_DIR
                        Put output files in this directory instead of overwriting the input files
  -W, --write-unchanged-files
                        Also write files even if no changes were required (useful with --output-dir); implies -w.Configurationtyperight will read defaults from a configuration file namedtyperight.ini,
orsetup.cfgin the current directory.For example:[typeright]files=typerightcommand=dmypy suggest --json --no-any {filename}:{lineno}docstring_format=numpywrite=trueInstallationThis should work for Python 2.7 as well as for Python 3.6 and higher.pip install typerightUsing as a pre-commit hookWe usepre-committo fixup code prior to committing
or pushing.To set it up,cdto this repo and run:pip install pre-commit
pre-commit installTo manually run pre-commit on staged files:pre-commit runTo manually run pre-commit on all files:pre-commit run -aTestingTo run the unit tests, use pytest:pytestAcknowledgmentsThis project was forked from PyAnnotate, after some encouragement from
Guido van Rossum, because PyAnnotate is no longer being actively maintained.Here are the original acknowledgments:Tony GrueSergei VorobevJukka LehtosaloGuido van RossumLicence etc.License: Apache 2.0.Copyright attribution: Copyright (c) 2017 Dropbox, Inc, and Chad Dombrova 2020.