Name | unkey JSON |
Version |
0.0.2
JSON |
| download |
home_page | https://github.com/mxr/unkey |
Summary | A tool to automatically remove extra calls to keys(). |
upload_time | 2024-10-22 20:44:46 |
maintainer | None |
docs_url | None |
author | Max R |
requires_python | >=3.10 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# `unkey`
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/mxr/unkey/main.svg)](https://results.pre-commit.ci/latest/github/mxr/unkey/main)
A tool and pre-commit hook to automatically remove extra calls to `keys()`.
## Installation
`pip install unkey`
## As a pre-commit hook
See [pre-commit][pre-commit] for instructions
Sample `.pre-commit-config.yaml`:
```yaml
- repo: https://github.com/mxr/unkey
rev: v0.0.2
hooks:
- id: unkey
```
## Overview
### Summary
Iterating over a Python dictionary will iterate over its keys, so calls to
`keys()` are often not needed. Removing these calls keeps your code terser and
more readable.
### Excluding Code
`unkey` does not support an equivalent of flake8's `# noqa` or mypy's `#type:
ignore` to stop rewriting. Until this feature is available, use an intermediate
variable to prevent rewriting.
```python
# will be rewritten
min({1: 2, 3: 4}.keys()) # becomes min({1: 2, 3: 4})
# will not be rewritten
keys = {1: 2, 3: 4}.keys()
min(keys)
```
### Disclaimer
Since AST parsing does not always tell us the type of an object, there may be
false positives and undesirable rewrites or bugs. With that said the tool is
designed to err on the side of not rewriting rather than being very clever and
rewriting as much as possible. To exclude rewrite, see the above section. PRs
are always welcome to help out!
## Features
### `builtins`
Rewrites builtin calls that have iterable arguments
```diff
# simple cases
-min({1: 2, 3: 4}.keys())
+min({1: 2, 3: 4})
-min(d.keys())
+min(d)
-min(f().keys())
+min(f())
# more complex cases
-min(d1().x.y(1, 2 ,3, (4, 5)).keys())
+min(d1().x.y(1, 2, 3, (4, 5)))
```
### `zip`
Rewrites relevant arguments in `zip()`
```diff
-zip(d.keys(), {}.keys(), f().keys(), [1, 2, 3])
+zip(d, {}, f(), [1, 2, 3])
```
### `map` / `filter`
Rewrites relevant arguments in `map` and `filter`
```diff
-map(lambda x: x*2, d.keys())
+map(lambda x: x*2, d)
-filter(None, d.keys())
+filteR(None, d)
```
### `in`
Rewrites relevant comparisons using `in`
```diff
-if x in d.keys():
+if x in d:
pass
```
### comprehensions
Rewrites relevant list/dict/set comprehensions and generator expressions
```diff
-[x for x in d.keys()]
+[x for x in d]
-(x for x in d.keys())
+(x for x in d)
-{x for x in d.keys()}
+{x for x in d}
-{x: x for x in d.keys()}
+{x: x for x in d}
```
For additional linting in this space check out [`flake8-comprehensions`][flake8-comprehensions].
### iteration
Rewrites iteration
```diff
-for _ in d.keys(): pass
+for _ in d: pass
-for _ in {}.keys(): pass
+for _ in {}: pass
-for _ in f().keys(): pass
+for _ in f(): pass
```
## Acknowledgements
This tool would not be possible without guidance and tools from [Anthony
Sottile][asottile], specifically, [`pyupgrade`][pyupgrade] and
[`pre-commit`][pre-commit]. `unkey` is heavily adapted from the former and code
is attributed wherever possible. Thank you!
[asottile]: https://github.com/asottile
[flake8-comprehensions]: https://pypi.org/project/flake8-comprehensions/
[pre-commit]: https://pre-commit.com
[pyupgrade]: https://pypi.org/project/pyupgrade/
Raw data
{
"_id": null,
"home_page": "https://github.com/mxr/unkey",
"name": "unkey",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": null,
"author": "Max R",
"author_email": "maxr@outlook.com",
"download_url": "https://files.pythonhosted.org/packages/a7/31/353086c466c8c6d62f7bb15fb365df57c2c904104d8fd1d045628bff104e/unkey-0.0.2.tar.gz",
"platform": null,
"description": "# `unkey`\n\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/mxr/unkey/main.svg)](https://results.pre-commit.ci/latest/github/mxr/unkey/main)\n\nA tool and pre-commit hook to automatically remove extra calls to `keys()`.\n\n## Installation\n\n`pip install unkey`\n\n## As a pre-commit hook\n\nSee [pre-commit][pre-commit] for instructions\n\nSample `.pre-commit-config.yaml`:\n\n```yaml\n- repo: https://github.com/mxr/unkey\n rev: v0.0.2\n hooks:\n - id: unkey\n```\n\n## Overview\n\n### Summary\n\nIterating over a Python dictionary will iterate over its keys, so calls to\n`keys()` are often not needed. Removing these calls keeps your code terser and\nmore readable.\n\n### Excluding Code\n\n`unkey` does not support an equivalent of flake8's `# noqa` or mypy's `#type:\nignore` to stop rewriting. Until this feature is available, use an intermediate\nvariable to prevent rewriting.\n\n```python\n# will be rewritten\nmin({1: 2, 3: 4}.keys()) # becomes min({1: 2, 3: 4})\n\n# will not be rewritten\nkeys = {1: 2, 3: 4}.keys()\nmin(keys)\n```\n\n### Disclaimer\n\nSince AST parsing does not always tell us the type of an object, there may be\nfalse positives and undesirable rewrites or bugs. With that said the tool is\ndesigned to err on the side of not rewriting rather than being very clever and\nrewriting as much as possible. To exclude rewrite, see the above section. PRs\nare always welcome to help out!\n\n## Features\n\n### `builtins`\n\nRewrites builtin calls that have iterable arguments\n\n```diff\n # simple cases\n-min({1: 2, 3: 4}.keys())\n+min({1: 2, 3: 4})\n\n-min(d.keys())\n+min(d)\n\n-min(f().keys())\n+min(f())\n\n # more complex cases\n-min(d1().x.y(1, 2 ,3, (4, 5)).keys())\n+min(d1().x.y(1, 2, 3, (4, 5)))\n```\n\n### `zip`\n\nRewrites relevant arguments in `zip()`\n\n```diff\n-zip(d.keys(), {}.keys(), f().keys(), [1, 2, 3])\n+zip(d, {}, f(), [1, 2, 3])\n```\n\n### `map` / `filter`\n\nRewrites relevant arguments in `map` and `filter`\n\n```diff\n-map(lambda x: x*2, d.keys())\n+map(lambda x: x*2, d)\n\n-filter(None, d.keys())\n+filteR(None, d)\n```\n\n### `in`\n\nRewrites relevant comparisons using `in`\n\n```diff\n-if x in d.keys():\n+if x in d:\n pass\n```\n\n### comprehensions\n\nRewrites relevant list/dict/set comprehensions and generator expressions\n\n```diff\n-[x for x in d.keys()]\n+[x for x in d]\n\n-(x for x in d.keys())\n+(x for x in d)\n\n-{x for x in d.keys()}\n+{x for x in d}\n\n-{x: x for x in d.keys()}\n+{x: x for x in d}\n```\n\nFor additional linting in this space check out [`flake8-comprehensions`][flake8-comprehensions].\n\n### iteration\n\nRewrites iteration\n\n```diff\n-for _ in d.keys(): pass\n+for _ in d: pass\n\n-for _ in {}.keys(): pass\n+for _ in {}: pass\n\n-for _ in f().keys(): pass\n+for _ in f(): pass\n```\n\n## Acknowledgements\n\nThis tool would not be possible without guidance and tools from [Anthony\nSottile][asottile], specifically, [`pyupgrade`][pyupgrade] and\n[`pre-commit`][pre-commit]. `unkey` is heavily adapted from the former and code\nis attributed wherever possible. Thank you!\n\n[asottile]: https://github.com/asottile\n[flake8-comprehensions]: https://pypi.org/project/flake8-comprehensions/\n[pre-commit]: https://pre-commit.com\n[pyupgrade]: https://pypi.org/project/pyupgrade/\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A tool to automatically remove extra calls to keys().",
"version": "0.0.2",
"project_urls": {
"Homepage": "https://github.com/mxr/unkey"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "81e273382e3386d4a5c609df3b776fbcb73bcbbd6303bc67505f5d730e547762",
"md5": "d48f46abfacf5e25ccf32baf70df7723",
"sha256": "50f1fc1fcecf98313a5465747a0d8b0f55c7aa8a9bdb2b94d4d2e8d9bd95d1a3"
},
"downloads": -1,
"filename": "unkey-0.0.2-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "d48f46abfacf5e25ccf32baf70df7723",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": ">=3.10",
"size": 6195,
"upload_time": "2024-10-22T20:44:45",
"upload_time_iso_8601": "2024-10-22T20:44:45.361559Z",
"url": "https://files.pythonhosted.org/packages/81/e2/73382e3386d4a5c609df3b776fbcb73bcbbd6303bc67505f5d730e547762/unkey-0.0.2-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a731353086c466c8c6d62f7bb15fb365df57c2c904104d8fd1d045628bff104e",
"md5": "9536103ddaa9534f9d30ddba9109eb8e",
"sha256": "5841b4c8ed08bde133235d6d24db7ef17486f042f63a323571f3c2ba549b4954"
},
"downloads": -1,
"filename": "unkey-0.0.2.tar.gz",
"has_sig": false,
"md5_digest": "9536103ddaa9534f9d30ddba9109eb8e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 6209,
"upload_time": "2024-10-22T20:44:46",
"upload_time_iso_8601": "2024-10-22T20:44:46.971728Z",
"url": "https://files.pythonhosted.org/packages/a7/31/353086c466c8c6d62f7bb15fb365df57c2c904104d8fd1d045628bff104e/unkey-0.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-22 20:44:46",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mxr",
"github_project": "unkey",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "unkey"
}