{"id":15472090,"url":"https://github.com/jellezijlstra/autotyping","last_synced_at":"2025-05-16T18:05:43.125Z","repository":{"id":41301330,"uuid":"380079543","full_name":"JelleZijlstra/autotyping","owner":"JelleZijlstra","description":"Automatically add simple type annotations to your code","archived":false,"fork":false,"pushed_at":"2024-09-23T12:33:10.000Z","size":96,"stargazers_count":250,"open_issues_count":3,"forks_count":20,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-31T04:03:37.707Z","etag":null,"topics":["mypy","python","typing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JelleZijlstra.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-25T00:09:16.000Z","updated_at":"2025-03-10T06:33:09.000Z","dependencies_parsed_at":"2024-03-23T03:21:37.269Z","dependency_job_id":"21589d18-7ae8-4b17-8ab8-9f3153d9e21f","html_url":"https://github.com/JelleZijlstra/autotyping","commit_stats":{"total_commits":59,"total_committers":13,"mean_commits":4.538461538461538,"dds":"0.27118644067796616","last_synced_commit":"4dcb8d9dcb3dceedce8de4df074d18de0b7cfe3c"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JelleZijlstra%2Fautotyping","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JelleZijlstra%2Fautotyping/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JelleZijlstra%2Fautotyping/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JelleZijlstra%2Fautotyping/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JelleZijlstra","download_url":"https://codeload.github.com/JelleZijlstra/autotyping/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247595332,"owners_count":20963943,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mypy","python","typing"],"created_at":"2024-10-02T02:26:34.995Z","updated_at":"2025-04-07T05:06:17.473Z","avatar_url":"https://github.com/JelleZijlstra.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"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\nAutotyping can be called directly from the CLI, be used as a [pre-commit hook](#pre-commit-hook) or run via the [`libcst` interface](#LibCST) as a codemod.\nHere's how to use it from the CLI:\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 `-\u003e 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\n# pre-commit hook\n\nPre-commit hooks are scripts that runs automatically before a commit is made,\nwhich makes them really handy for checking and enforcing code-formatting \n(or in this case, typing)\n\n1. To add `autotyping` as a [pre-commit](https://pre-commit.com/) hook, \nyou will first need to install pre-commit if you haven't already:\n```\npip install pre-commit\n```\n\n2. After that, create or update the `.pre-commit-config.yaml` file at the root\nof your repository and add in:\n\n```yaml\n- repos:\n  - repo: https://github.com/JelleZijlstra/autotyping\n    rev: 24.9.0\n    hooks:\n      - id: autotyping\n        stages: [commit]\n        types: [python]\n        args: [--safe] # or alternatively, --aggressive, or any of the other flags mentioned above \n```\n\n3. Finally, run the following command to install the pre-commit hook\nin your repository:\n\n```\npre-commit install\n```\n\nNow whenever you commit changes, autotyping will automatically add\ntype annotations to your code!\n\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.9.0 (September 23, 2024)\n\n- Add pre-commit support. (Thanks to Akshit Tyagi and Matthew Akram.)\n- Add missing dependency. (Thanks to Stefane Fermigier.)\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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjellezijlstra%2Fautotyping","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjellezijlstra%2Fautotyping","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjellezijlstra%2Fautotyping/lists"}