Skip to main content

**typeric** is a practical type utility toolkit for Python, focused on clarity, safety, and ergonomics

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.2.tar.gz (24.1 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.2-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for typeric-0.1.2.tar.gz
Algorithm Hash digest
SHA256 bf1db2250e1fccc61d128a54a52a44e0019bd649a92fcca18c0de68821548c6d
MD5 405584371b4edacf8ea1fb0ced1863a7
BLAKE2b-256 080315ab03f5e76f1213a9d81f976cff91075e5807adf1629d7984e017bd76d7

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for typeric-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3deaadeb6a85b38e9936f28b666043affaaa3f76ff622fa30589cd11312c4d54
MD5 217f9a4e835c2e6aaab8b18231bbaa3d
BLAKE2b-256 5fc3e8fa6e4744450a54a805852bc54c913a450ed3dc96cbf68ea8f18207b7ba

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