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.3.tar.gz (24.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.3-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for typeric-0.1.3.tar.gz
Algorithm Hash digest
SHA256 52b784aeab4f273ab3e422508a7f43115961581a49764475bf9447235f8f78ea
MD5 79465343b2e476b107a9507a12814316
BLAKE2b-256 113f6c54251eb74a373780069c4d079d4baee8a3f8dc22085b96e24105e1e257

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for typeric-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 24c0df3302a01abbe836d17fe5f4f10b3cdef3b49f322277bf7150410cd065d8
MD5 70d519df04a359dd1d71d8438924f09e
BLAKE2b-256 79135147c81535439afab77bf0a60fc3a0afd9da5df588783cfb46bac7aac4a0

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