Skip to content

Instantly share code, notes, and snippets.

View oliverlambson's full-sized avatar

Oliver Lambson oliverlambson

View GitHub Profile
@oliverlambson
oliverlambson / typed_decorator.md
Last active September 15, 2024 23:26
The right way to type python decorators

Fully-typed python decorators

The right way

from collections.abc import Callable
from functools import wraps


def my_decorator[T, **P](fn: Callable[P, T]) -> Callable[P, T]:
@oliverlambson
oliverlambson / clickdate.md
Last active September 15, 2024 23:32
A `click.Date` type

A click.Date type

What's the problem?

click doesn't have a builtin date type, only a datetime. (They won't be adding one either.)

This litters my cli code with an annoying convert to date chore that I end up using all over the place:

@click.command()
@oliverlambson
oliverlambson / rust-go-result-type-python.md
Last active September 1, 2024 22:25
Rust & Go-style return types in Python

Rust-style Result type in Python

Lightweight, generic result type à la Rust. (Sometimes you just want errors as values.)

Note: There is a full-on package that does this, with all the other Rust niceties of .unwrap_or(), etc. But if all you need is the basics, this is a zero-dependency approach in like 8 lines of actual code.

from dataclasses import dataclass