All Projects → ilevkivskyi → com2ann

ilevkivskyi / com2ann

Licence: MIT license
Tool for translation type comments to type annotations in Python

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to com2ann

pandas-stubs
Pandas type stubs. Helps you type-check your code.
Stars: ✭ 84 (-26.96%)
Mutual labels:  annotations, type-annotations
py2puml
Generate PlantUML class diagrams to document your Python application.
Stars: ✭ 51 (-55.65%)
Mutual labels:  annotations, type-hints
gum
Repository for the Georgetown University Multilayer Corpus (GUM)
Stars: ✭ 71 (-38.26%)
Mutual labels:  annotations
mython
The Mython extensible variant of the Python programming language.
Stars: ✭ 16 (-86.09%)
Mutual labels:  source-to-source
api-router
👨 RESTful router for your API in Nette Framework (@nette). Created either directly or via annotation.
Stars: ✭ 18 (-84.35%)
Mutual labels:  annotations
claw-compiler
CLAW Compiler for Performance Portability
Stars: ✭ 38 (-66.96%)
Mutual labels:  source-to-source
Exact
An open source online platform for collaborative image labeling of almost everything
Stars: ✭ 47 (-59.13%)
Mutual labels:  annotations
flake8-annotations
Flake8 Type Annotation Checking
Stars: ✭ 117 (+1.74%)
Mutual labels:  type-annotations
php-simple-request
php-simple-request is a request parser library designed to simplify requests validation and filtering using annotations, generating at the same time an object representation from the request data.
Stars: ✭ 15 (-86.96%)
Mutual labels:  annotations
2vcf
convert 23andme or Ancestry.com raw genotype calls into VCF format, with dbSNP annotations
Stars: ✭ 42 (-63.48%)
Mutual labels:  annotations
dataclasses-jsonschema
JSON schema generation from dataclasses
Stars: ✭ 145 (+26.09%)
Mutual labels:  type-hints
simple-preferences
Android Library to simplify SharedPreferences use with code generation.
Stars: ✭ 48 (-58.26%)
Mutual labels:  annotations
aspecio
Aspecio, AOP Proxies for OSGi services
Stars: ✭ 14 (-87.83%)
Mutual labels:  annotations
soap-typescript
SOAP decorators for creating wsdl's and annotating services to provide metadata for node-soap
Stars: ✭ 20 (-82.61%)
Mutual labels:  annotations
zf-dependency-injection
Advanced dependency injection for laminas framework
Stars: ✭ 17 (-85.22%)
Mutual labels:  annotations
cocktail
Traits, Talents & Annotations for NodeJS.
Stars: ✭ 65 (-43.48%)
Mutual labels:  annotations
graphql-metadata
Annotate your graphql schema with lightweight directives
Stars: ✭ 28 (-75.65%)
Mutual labels:  annotations
geantyref
Advanced generic type reflection library with support for working with AnnotatedTypes (for Java 8+)
Stars: ✭ 73 (-36.52%)
Mutual labels:  annotations
dasbus
DBus library in Python 3
Stars: ✭ 52 (-54.78%)
Mutual labels:  type-hints
typed-astunparse
Python 3 AST unparser with type comments support.
Stars: ✭ 27 (-76.52%)
Mutual labels:  type-annotations

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: str) -> 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: str) -> 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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].