Skip to main content

A tool to automatically remove extra calls to keys().

Project description

unkey

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.0
    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 unsedsirable 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.

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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unkey-0.0.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

unkey-0.0.0-py2.py3-none-any.whl (6.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file unkey-0.0.0.tar.gz.

File metadata

  • Download URL: unkey-0.0.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.10

File hashes

Hashes for unkey-0.0.0.tar.gz
Algorithm Hash digest
SHA256 76397d9ce7099a4c47f1be106b1800003154ecae287b591cd6cdd534a3771aa1
MD5 573e5b164ce2d016a5d8c9bf2d0befc5
BLAKE2b-256 a5435ef0cdbc09531ffc96acc9110f91aca290f87fa8fe5d652b3b62f5e4f396

See more details on using hashes here.

File details

Details for the file unkey-0.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: unkey-0.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.10

File hashes

Hashes for unkey-0.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 be26b94050868f1e2c5601798fcff18dcc9814021fd55b211c60e713acbf03af
MD5 dffdd17580d7a9512864db1e9017d4f1
BLAKE2b-256 3b99cf6e1a6ff966655c671621328ce434de6e6d7a69aa29c49a4aef59ebc6e0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page