autotyping


Nameautotyping JSON
Version 24.3.0 PyPI version JSON
download
home_pageNone
SummaryA tool for autoadding simple type annotations.
upload_time2024-03-25 20:24:50
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords annotations typing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            When I refactor code I often find myself tediously adding type
annotations that are obvious from context: functions that don't
return anything, boolean flags, etcetera. That's where autotyping
comes in: it automatically adds those types and inserts the right
annotations.

# Usage

Here's how to use it:

- `pip install autotyping`
- `python -m autotyping /path/to/my/code`

By default it does nothing; you have to add flags to make it do
more transformations. The following are supported:

- Annotating return types:
  - `--none-return`: add a `-> None` return type to functions without any
    return, yield, or raise in their body
  - `--scalar-return`: add a return annotation to functions that only return
    literal bool, str, bytes, int, or float objects.
- Annotating parameter types:
  - `--bool-param`: add a `: bool` annotation to any function
    parameter with a default of `True` or `False`
  - `--int-param`, `--float-param`, `--str-param`, `--bytes-param`: add
    an annotation to any parameter for which the default is a literal int,
    float, str, or bytes object
  - `--annotate-optional foo:bar.Baz`: for any parameter of the form
    `foo=None`, add `Baz`, imported from `bar`, as the type. For example,
    use `--annotate-optional uid:my_types.Uid` to annotate any `uid` in your
    codebase with a `None` default as `Optional[my_types.Uid]`.
  - `--annotate-named-param foo:bar.Baz`: annotate any parameter with no
    default that is named `foo` with `bar.Baz`. For example, use
    `--annotate-named-param uid:my_types.Uid` to annotate any `uid`
    parameter in your codebase with no default as `my_types.Uid`.
  - `--guess-common-names`: infer certain parameter types from their names
    based on common patterns in open-source Python code. For example, infer
    that a `verbose` parameter is of type `bool`.
- Annotating magical methods:
  - `--annotate-magics`: add type annotation to certain magic methods.
    Currently this does the following:
    - `__str__` returns `str`
    - `__repr__` returns `str`
    - `__len__` returns `int`
    - `__length_hint__` returns `int`
    - `__init__` returns `None`
    - `__del__` returns `None`
    - `__bool__` returns `bool`
    - `__bytes__` returns `bytes`
    - `__format__` returns `str`
    - `__contains__` returns `bool`
    - `__complex__` returns `complex`
    - `__int__` returns `int`
    - `__float__` returns `float`
    - `__index__` returns `int`
    - `__exit__`: the three parameters are `Optional[Type[BaseException]]`,
      `Optional[BaseException]`, and `Optional[TracebackType]`
    - `__aexit__`: same as `__exit__`
  - `--annotate-imprecise-magics`: add imprecise type annotations for
    some additional magic methods. Currently this adds `typing.Iterator`
    return annotations to `__iter__`, `__await__`, and `__reversed__`.
    These annotations should have a generic parameter to indicate what
    you're iterating over, but that's too hard for autotyping to figure
    out.
- External integrations
  - `--pyanalyze-report`: takes types suggested by
    [pyanalyze](https://github.com/quora/pyanalyze)'s `suggested_parameter_type`
    and `suggested_return_type` codes and applies them. You can generate these
    with a command like:
    `pyanalyze --json-output failures.json -e suggested_return_type -e suggested_parameter_type -v .`
  - `--only-without-imports`: only apply pyanalyze suggestions that do not require
    new imports. This is useful because suggestions that require imports may need
    more manual work.

There are two shortcut flags to enable multiple transformations at once:

- `--safe` enables changes that should always be safe. This includes
  `--none-return`, `--scalar-return`, and `--annotate-magics`.
- `--aggressive` enables riskier changes that are more likely to produce
  new type checker errors. It includes all of `--safe` as well as `--bool-param`,
  `--int-param`, `--float-param`, `--str-param`, `--bytes-param`, and
  `--annotate-imprecise-magics`.

# LibCST

Autotyping is built as a LibCST codemod; see the
[LibCST documentation](https://libcst.readthedocs.io/en/latest/codemods_tutorial.html)
for more information on how to use codemods.

If you wish to run things through the `libcst.tool` interface, you can do this like so:
- Make sure you have a `.libcst.codemod.yaml` with `'autotyping'` in the `modules` list.
  For an example, see the `.libcst.codemod.yaml` in this repo.
- Run `python -m libcst.tool codemod autotyping.AutotypeCommand /path/to/my/code`

# Limitations

Autotyping is intended to be a simple tool that uses heuristics to find
annotations that would be tedious to add by hand. The heuristics may fail,
and after you run autotyping you should run a type checker to verify that
the types it added are correct.

Known limitations:

- autotyping does not model code flow through a function, so it may miss
  implicit `None` returns

# Changelog

## 24.3.0 (March 25, 2024)

- Add simpler ways to invoke autotyping. Now, it is possible to simply use
  `python3 -m autotyping` to invoke the tool. (Thanks to Shantanu Jain.)
- Drop support for Python 3.7; add support for Python 3.12. (Thanks to Hugo
  van Kemenade.)
- Infer return types for some more magic methods. (Thanks to Dhruv Manilawala.)

## 23.3.0 (March 3, 2023)

- Fix crash on certain argument names like `iterables` (contributed by
  Marco Gorelli)

## 23.2.0 (February 3, 2023)

- Add `--guess-common-names` (contributed by John Litborn)
- Fix the `--safe` and `--aggressive` flags so they don't take
  ignored arguments
- `--length-hint` should return `int` (contributed by Nikita Sobolev)
- Fix bug in import adding (contributed by Shantanu)

## 22.9.0 (September 5, 2022)

- Add `--safe` and `--aggressive`
- Add `--pyanalyze-report`
- Do not add `None` return types to methods marked with `@abstractmethod` and
  to methods in stub files
- Improve type inference:
  - `"string" % ...` is always `str`
  - `b"bytes" % ...` is always `bytes`
  - An `and` or `or` operator where left and right sides are of the same type
    returns that type
  - `is`, `is not`, `in`, and `not in` always return `bool`

## 21.12.0 (December 21, 2021)

- Initial PyPI release

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "autotyping",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "annotations, typing",
    "author": null,
    "author_email": "Jelle Zijlstra <jelle.zijlstra@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/4e/fa/02954d71fce8d2da90effebc7e40be1e0221301f1b872a02fe4d7379aa2b/autotyping-24.3.0.tar.gz",
    "platform": null,
    "description": "When I refactor code I often find myself tediously adding type\nannotations that are obvious from context: functions that don't\nreturn anything, boolean flags, etcetera. That's where autotyping\ncomes in: it automatically adds those types and inserts the right\nannotations.\n\n# Usage\n\nHere's how to use it:\n\n- `pip install autotyping`\n- `python -m autotyping /path/to/my/code`\n\nBy default it does nothing; you have to add flags to make it do\nmore transformations. The following are supported:\n\n- Annotating return types:\n  - `--none-return`: add a `-> None` return type to functions without any\n    return, yield, or raise in their body\n  - `--scalar-return`: add a return annotation to functions that only return\n    literal bool, str, bytes, int, or float objects.\n- Annotating parameter types:\n  - `--bool-param`: add a `: bool` annotation to any function\n    parameter with a default of `True` or `False`\n  - `--int-param`, `--float-param`, `--str-param`, `--bytes-param`: add\n    an annotation to any parameter for which the default is a literal int,\n    float, str, or bytes object\n  - `--annotate-optional foo:bar.Baz`: for any parameter of the form\n    `foo=None`, add `Baz`, imported from `bar`, as the type. For example,\n    use `--annotate-optional uid:my_types.Uid` to annotate any `uid` in your\n    codebase with a `None` default as `Optional[my_types.Uid]`.\n  - `--annotate-named-param foo:bar.Baz`: annotate any parameter with no\n    default that is named `foo` with `bar.Baz`. For example, use\n    `--annotate-named-param uid:my_types.Uid` to annotate any `uid`\n    parameter in your codebase with no default as `my_types.Uid`.\n  - `--guess-common-names`: infer certain parameter types from their names\n    based on common patterns in open-source Python code. For example, infer\n    that a `verbose` parameter is of type `bool`.\n- Annotating magical methods:\n  - `--annotate-magics`: add type annotation to certain magic methods.\n    Currently this does the following:\n    - `__str__` returns `str`\n    - `__repr__` returns `str`\n    - `__len__` returns `int`\n    - `__length_hint__` returns `int`\n    - `__init__` returns `None`\n    - `__del__` returns `None`\n    - `__bool__` returns `bool`\n    - `__bytes__` returns `bytes`\n    - `__format__` returns `str`\n    - `__contains__` returns `bool`\n    - `__complex__` returns `complex`\n    - `__int__` returns `int`\n    - `__float__` returns `float`\n    - `__index__` returns `int`\n    - `__exit__`: the three parameters are `Optional[Type[BaseException]]`,\n      `Optional[BaseException]`, and `Optional[TracebackType]`\n    - `__aexit__`: same as `__exit__`\n  - `--annotate-imprecise-magics`: add imprecise type annotations for\n    some additional magic methods. Currently this adds `typing.Iterator`\n    return annotations to `__iter__`, `__await__`, and `__reversed__`.\n    These annotations should have a generic parameter to indicate what\n    you're iterating over, but that's too hard for autotyping to figure\n    out.\n- External integrations\n  - `--pyanalyze-report`: takes types suggested by\n    [pyanalyze](https://github.com/quora/pyanalyze)'s `suggested_parameter_type`\n    and `suggested_return_type` codes and applies them. You can generate these\n    with a command like:\n    `pyanalyze --json-output failures.json -e suggested_return_type -e suggested_parameter_type -v .`\n  - `--only-without-imports`: only apply pyanalyze suggestions that do not require\n    new imports. This is useful because suggestions that require imports may need\n    more manual work.\n\nThere are two shortcut flags to enable multiple transformations at once:\n\n- `--safe` enables changes that should always be safe. This includes\n  `--none-return`, `--scalar-return`, and `--annotate-magics`.\n- `--aggressive` enables riskier changes that are more likely to produce\n  new type checker errors. It includes all of `--safe` as well as `--bool-param`,\n  `--int-param`, `--float-param`, `--str-param`, `--bytes-param`, and\n  `--annotate-imprecise-magics`.\n\n# LibCST\n\nAutotyping is built as a LibCST codemod; see the\n[LibCST documentation](https://libcst.readthedocs.io/en/latest/codemods_tutorial.html)\nfor more information on how to use codemods.\n\nIf you wish to run things through the `libcst.tool` interface, you can do this like so:\n- Make sure you have a `.libcst.codemod.yaml` with `'autotyping'` in the `modules` list.\n  For an example, see the `.libcst.codemod.yaml` in this repo.\n- Run `python -m libcst.tool codemod autotyping.AutotypeCommand /path/to/my/code`\n\n# Limitations\n\nAutotyping is intended to be a simple tool that uses heuristics to find\nannotations that would be tedious to add by hand. The heuristics may fail,\nand after you run autotyping you should run a type checker to verify that\nthe types it added are correct.\n\nKnown limitations:\n\n- autotyping does not model code flow through a function, so it may miss\n  implicit `None` returns\n\n# Changelog\n\n## 24.3.0 (March 25, 2024)\n\n- Add simpler ways to invoke autotyping. Now, it is possible to simply use\n  `python3 -m autotyping` to invoke the tool. (Thanks to Shantanu Jain.)\n- Drop support for Python 3.7; add support for Python 3.12. (Thanks to Hugo\n  van Kemenade.)\n- Infer return types for some more magic methods. (Thanks to Dhruv Manilawala.)\n\n## 23.3.0 (March 3, 2023)\n\n- Fix crash on certain argument names like `iterables` (contributed by\n  Marco Gorelli)\n\n## 23.2.0 (February 3, 2023)\n\n- Add `--guess-common-names` (contributed by John Litborn)\n- Fix the `--safe` and `--aggressive` flags so they don't take\n  ignored arguments\n- `--length-hint` should return `int` (contributed by Nikita Sobolev)\n- Fix bug in import adding (contributed by Shantanu)\n\n## 22.9.0 (September 5, 2022)\n\n- Add `--safe` and `--aggressive`\n- Add `--pyanalyze-report`\n- Do not add `None` return types to methods marked with `@abstractmethod` and\n  to methods in stub files\n- Improve type inference:\n  - `\"string\" % ...` is always `str`\n  - `b\"bytes\" % ...` is always `bytes`\n  - An `and` or `or` operator where left and right sides are of the same type\n    returns that type\n  - `is`, `is not`, `in`, and `not in` always return `bool`\n\n## 21.12.0 (December 21, 2021)\n\n- Initial PyPI release\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A tool for autoadding simple type annotations.",
    "version": "24.3.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/JelleZijlstra/autotyping/issues",
        "Homepage": "https://github.com/JelleZijlstra/autotyping"
    },
    "split_keywords": [
        "annotations",
        " typing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ee819490ea3aa9546e88f168e421aa0e3243b08f317c7eeee77a9270097f3fef",
                "md5": "7c241622eab5b4d90d0158c540f376e7",
                "sha256": "0637343190b858be19e00ff20a2f0344426357b9f8b849e28862d1a01cf12fe7"
            },
            "downloads": -1,
            "filename": "autotyping-24.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7c241622eab5b4d90d0158c540f376e7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 12399,
            "upload_time": "2024-03-25T20:24:49",
            "upload_time_iso_8601": "2024-03-25T20:24:49.290280Z",
            "url": "https://files.pythonhosted.org/packages/ee/81/9490ea3aa9546e88f168e421aa0e3243b08f317c7eeee77a9270097f3fef/autotyping-24.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4efa02954d71fce8d2da90effebc7e40be1e0221301f1b872a02fe4d7379aa2b",
                "md5": "094a409fba47927ed6aa0a85b93175fe",
                "sha256": "bce5bffec7033b6487d45d57c409adf4065a1835f60113a6186c180bc61da4e4"
            },
            "downloads": -1,
            "filename": "autotyping-24.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "094a409fba47927ed6aa0a85b93175fe",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 16053,
            "upload_time": "2024-03-25T20:24:50",
            "upload_time_iso_8601": "2024-03-25T20:24:50.808479Z",
            "url": "https://files.pythonhosted.org/packages/4e/fa/02954d71fce8d2da90effebc7e40be1e0221301f1b872a02fe4d7379aa2b/autotyping-24.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-25 20:24:50",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "JelleZijlstra",
    "github_project": "autotyping",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "autotyping"
}
        
Elapsed time: 0.20915s