Skip to content
Write short and fully-typed lambdas where you need them.
Python
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs
lambdas
tests/test_callable
typesafety
.editorconfig
.gitignore
.readthedocs.yml
.travis.yml
CHANGELOG.md
CONTRIBUTING.md
LICENSE
README.md
poetry.lock
pyproject.toml
setup.cfg

README.md

lambdas logo


Build Status Coverage Status Documentation Status Python Version wemake-python-styleguide Checked with mypy


Write short and fully-typed lambdas where you need them.

Features

  • Allows to write lambdas as _
  • Fully typed with annotations and checked with mypy, PEP561 compatible
  • Has a bunch of helpers for better composition
  • Easy to start: has lots of docs, tests, and tutorials

Installation

pip install lambdas

You also need to configure mypy correctly and install our plugin:

# In setup.cfg or mypy.ini:
[mypy]
plugins =
  lambdas.contrib.mypy.lambdas_plugin

We recommend to use the same mypy settings we use.

Examples

Imagine that you need to sort an array of dictionaries like so:

scores = [
    {'name': 'Nikita', 'score': 2},
    {'name': 'Oleg', 'score': 1},
    {'name': 'Pavel', 'score': 4},
]

print(sorted(scores, key=lambda item: item['score']))

And it works perfectly fine. Except, that you have to do a lot of typing for such a simple operation.

That's where lambdas helper steps in:

from lambdas import _

scores = [
    {'name': 'Nikita', 'score': 2},
    {'name': 'Oleg', 'score': 1},
    {'name': 'Pavel', 'score': 4},
]

print(sorted(scores, key=_['score']))

It might really save you a lot of effort, when you use a lot of lambda functions. Like when using returns library.

Work in progress:

  • _.method() is not supported yet for the same reason
  • TypedDicts are not tested with __getitem__
  • __getitem__ does not work with list and tuples (collections), only dicts (mappings)
  • complex extressions like _ * 2 + 1 are not tested

For now you will have to use regular lamdbas in these cases.

You can’t perform that action at this time.