Giter VIP home page Giter VIP logo

com2ann's Introduction

com2ann

Build Status Checked with mypy

Tool for translation of type comments to type annotations in Python.

The tool requires Python 3.8 to run. But the supported target code version is Python 3.4+ (can be specified with --python-minor-version).

Currently, the tool translates function and assignment type comments to type annotations. For example this code:

from typing import Optional, Final

MAX_LEVEL = 80  # type: Final

class Template:
    default = None  # type: Optional[str]

    def apply(self, value, **opts):
        # type: (str, **bool) -> str
        ...

will be translated to:

from typing import Optional, Final

MAX_LEVEL: Final = 80

class Template:
    default: Optional[str] = None

    def apply(self, value: str, **opts: bool) -> str:
        ...

The philosophy of the tool is to be minimally invasive, and preserve original formatting as much as possible. This is why the tool doesn't use un-parse.

The only (optional) formatting code modification is wrapping long function signatures. To specify the maximal length, use --wrap-signatures MAX_LENGTH. The signatures are wrapped one argument per line (after each comma), for example:

    def apply(self,
              value: str,
              **opts: bool) -> str:
        ...

For working with stubs, there are two additional options for assignments: --drop-ellipsis and --drop-none. They will result in omitting the redundant right hand sides. For example, this:

var = ...  # type: List[int]
class Test:
    attr = None  # type: str

will be translated with such options to:

var: List[int]
class Test:
    attr: str

Usage

$ com2ann --help

usage: com2ann [-h] [-o OUTFILE] [-s] [-n] [-e] [-i] [-w WRAP_SIGNATURES]
               [-v PYTHON_MINOR_VERSION]
               infile

Helper module to translate type comments to type annotations. The key idea of
this module is to perform the translation while preserving the original
formatting as much as possible. We try to be not opinionated about code
formatting and therefore work at the source code and tokenizer level instead
of modifying AST and using un-parse. We are especially careful about
assignment statements, and keep the placement of additional (non-type)
comments. For function definitions, we might introduce some formatting
modifications, if the original formatting was too tricky.

positional arguments:
  infile                input file or directory for translation, must contain
                        no syntax errors; if --outfile is not given,
                        translation is made *in place*

optional arguments:
  -h, --help            show this help message and exit
  -o OUTFILE, --outfile OUTFILE
                        output file or directory, will be overwritten if
                        exists, defaults to input file or directory
  -s, --silent          do not print summary for line numbers of translated
                        and rejected comments
  -n, --drop-none       drop any None as assignment value during translation
                        if it is annotated by a type comment
  -e, --drop-ellipsis   drop any Ellipsis (...) as assignment value during
                        translation if it is annotated by a type comment
  -i, --add-future-imports
                        add 'from __future__ import annotations' to any file
                        where type comments were successfully translated
  -w WRAP_SIGNATURES, --wrap-signatures WRAP_SIGNATURES
                        wrap function headers that are longer than given
                        length
  -v PYTHON_MINOR_VERSION, --python-minor-version PYTHON_MINOR_VERSION
                        Python 3 minor version to use to parse the files

com2ann's People

Contributors

ilevkivskyi avatar cclauss avatar ewjoachim avatar bityob avatar zac-hd avatar ostr00000 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.