Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

unkey

pre-commit.ci status

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 for instructions

Sample .pre-commit-config.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.

# 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

 # 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()

-zip(d.keys(), {}.keys(), f().keys(), [1, 2, 3])
+zip(d, {}, f(), [1, 2, 3])

map / filter

Rewrites relevant arguments in map and filter

-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

-if x in d.keys():
+if x in d:
     pass

comprehensions

Rewrites relevant list/dict/set comprehensions and generator expressions

-[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.

iteration

Rewrites iteration

-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, specifically, pyupgrade and pre-commit. unkey is heavily adapted from the former and code is attributed wherever possible. Thank you!

Metadata

Release files for unkey 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unkey 0.0.2
File Size Uploaded Provenance Actions
unkey-0.0.2.tar.gz 6.2 kB Without provenance Details for unkey-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for unkey 0.0.2
File Interpreter ABI Platform Actions
unkey-0.0.2-py2.py3-none-any.whl Python 3, Python 2 none any Details for unkey-0.0.2-py2.py3-none-any.whl

Total release size: 12.4 kB

Release files / unkey-0.0.2.tar.gz

Download URL unkey-0.0.2.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5841b4c8ed08bde133235d6d24db7ef17486f042f63a323571f3c2ba549b4954
BLAKE2b-256 checksum
How to use checksums
a731353086c466c8c6d62f7bb15fb365df57c2c904104d8fd1d045628bff104e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.15

Release files / unkey-0.0.2-py2.py3-none-any.whl

Download URL unkey-0.0.2-py2.py3-none-any.whl
Size 6.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
50f1fc1fcecf98313a5465747a0d8b0f55c7aa8a9bdb2b94d4d2e8d9bd95d1a3
BLAKE2b-256 checksum
How to use checksums
81e273382e3386d4a5c609df3b776fbcb73bcbbd6303bc67505f5d730e547762
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.15

Security for unkey 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page