Skip to main content

Add your description here

Project description

📦 typeric

typeric is a practical type utility toolkit for Python, focused on clarity, safety, and ergonomics, it was originally built to make my own development experience smoother, but I hope it proves useful to others as well. It currently provides a lightweight, pattern-matchable Result type — similar to Rust's Result — with plans to include more common type patterns and error-handling abstractions.

pip install typeric

🚀 Features

  • ✅ Functional-style Result type: Ok(value) and Err(error)
  • 🧩 Pattern matching support (__match_args__)
  • 🔒 Immutable with .map() / .map_err() / .unwrap() / .unwrap_or() helpers
  • 🔧 Clean type signatures: Result[T, E] (with default E = Exception)
  • 🛠️ Built for extensibility — more type tools coming

🔍 Quick Example

from typeric.result import Result, Ok, Err

def parse_number(text: str) -> Result[int, str]:
    try:
        return Ok(int(text))
    except ValueError:
        return Err("Not a number")

match parse_number("42"):
    case Ok(value):
        print("Parsed:", value)
    case Err(error):
        print("Failed:", error)

📂 Real-World Example

from functools import partial
import hashlib
from pathlib import Path
from typing import BinaryIO
from typeric.result import Result, Ok, Err


def get_md5(file_obj: BinaryIO) -> Result[str, Exception]:
    try:
        md5 = hashlib.md5()
        while chunk := file_obj.read(8096):
            md5.update(chunk)
        file_obj.seek(0)
        return Ok(md5.hexdigest())
    except Exception as e:
        return Err(e)


def is_exist(element: str, file_sets: set[str], auto_add: bool = True) -> bool:
    exist = element in file_sets
    if not exist and auto_add:
        file_sets.add(element)
    return exist


def file_exist(file_obj: BinaryIO, file_sets: set[str], auto_add: bool = True) -> Result[bool, Exception]:
    match get_md5(file_obj):
        case Ok(md5):
            print("md5:", md5)
        case Err(e):
            print("error occurred:", e)
    func = partial(is_exist, file_sets=file_sets, auto_add=auto_add)
    return get_md5(file_obj).map(func=func)

✅ Test

def test_file() -> None:
    file_set: set[str] = set()
    files = [Path("test1.pdf"), Path("test1.pdf"), Path("test2.pdf")]
    for file in files:
        with open(file, "rb") as f:
            result = file_exist(f, file_set)
            assert result.is_ok()

Run tests with:

uv pytest -v

📦 Roadmap

  • Validated type for batch error collection
  • Async Result

📄 License

MIT

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

typeric-0.1.0.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typeric-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file typeric-0.1.0.tar.gz.

File metadata

  • Download URL: typeric-0.1.0.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.7

File hashes

Hashes for typeric-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cf7019995fef867e19fe25a8a6c1d365066d2667a698c77778ebecaf85466db3
MD5 c719afa5a168830191268c49c9c19ae8
BLAKE2b-256 79333416300467f7ed326ad2798936dea58a82ceafbe2b9b104533fb617674a1

See more details on using hashes here.

File details

Details for the file typeric-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: typeric-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.7

File hashes

Hashes for typeric-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9e6491308a7c0fb1445ff431a39212057da3abdfbce0b06dc6abec77f506b23c
MD5 080cf9caa3f8239e981f5a1ba765c4d0
BLAKE2b-256 eedc811effb6b2d0ad3f0714490539f90f99266b1add7e3a92a193f050b416f3

See more details on using hashes here.

Supported by

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