{"id":35155108,"url":"https://github.com/schizza/py-typecheck","last_synced_at":"2026-01-18T17:07:49.623Z","repository":{"id":330852733,"uuid":"1124143123","full_name":"schizza/py-typecheck","owner":"schizza","description":"Tiny runtime checker for typing annotations with an Option-like API.","archived":false,"fork":false,"pushed_at":"2025-12-28T16:57:08.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-12-31T02:46:00.774Z","etag":null,"topics":["python","runtime-typechecking","runtime-types","typechecker","typechecking","typing","validation"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/schizza.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-12-28T12:33:46.000Z","updated_at":"2025-12-28T16:54:14.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/schizza/py-typecheck","commit_stats":null,"previous_names":["schizza/py-typecheck"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/schizza/py-typecheck","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schizza%2Fpy-typecheck","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schizza%2Fpy-typecheck/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schizza%2Fpy-typecheck/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schizza%2Fpy-typecheck/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/schizza","download_url":"https://codeload.github.com/schizza/py-typecheck/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schizza%2Fpy-typecheck/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28400733,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T14:36:09.778Z","status":"ssl_error","status_checked_at":"2026-01-13T14:35:19.697Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python","runtime-typechecking","runtime-types","typechecker","typechecking","typing","validation"],"created_at":"2025-12-28T16:49:01.648Z","updated_at":"2026-01-18T17:07:49.615Z","avatar_url":"https://github.com/schizza.png","language":"Python","readme":"# typecheck-runtime \n\nTiny runtime checker for typing annotations with an Option-like API.\n\n`py-typecheck` solves a very specific problem:\n\n**I have a value (object or Any) and I want to check if\nthe value refers to specific typing annotation.**\n\nDoes not parse data. Does not transforms data.\nJust **checks structure and returns refered value** or \n`None` if validation failed.\n\n\n## Installation\n\n```bash\npip install typecheck-runtime\n\nPython ≥ 3.10\n```\n\n---\n\n## Core idea\n\nMain function is `checked`\n\n```python\nfrom py_typecheck import checked\n```\n\nActs like `Option[T]` or Rust-like `Some(T, None)`:\n - returns value if matches the type\n - returns `None` if validation failed\n\n No exceptions, no side-efects.\n\n---\n\n### Basic usage\n\n```python\nvalue: object = {\"a\": 1}\n\nif (d := checked(value, dict[str, int])) is not None:\n    print(d[\"a\"] + 1)\n```\n\n**Important**\n- always compare against `None` when using `checked`\n- do not rely on truthiness (0, False, [] are valid values!)\n- bool is treated as not matching int (so True wont pass as int)\n\n---\n\n## `checked_or`\n\nIf you want the same runtime check as `checked()`, but with a convenient fallback,\nuse `checked_or()`.\n\n- returns the original value if it matches the target type\n- otherwise returns the provided `default`\n\nThis is especially handy when you want a safe, typed value in one expression\nwithout handling `None`.\n\n```python\nfrom py_typecheck import checked_or\n\nage: object = \"not-a-number\"\n\n# returns 10 because \"not-a-number\" is not an int\nvalue = checked_or(age, int, 10)\n```\n\nWorks with typing constructs the same way as `checked()`:\n\n```python\nfrom py_typecheck import checked_or\n\npayload: object = {\"tags\": [\"a\", \"b\"]}\n\ntags = checked_or(payload, dict[str, list[str]], {\"tags\": []})[\"tags\"]\n```\n\nImportant notes:\n- `checked_or()` relies on `checked()` semantics (including the `bool` vs `int` rule)\n- `default` should already be the correct value you want to use (no coercion happens)\n\n---\n\n## Supported typing constructs\n\n`py-typecheck` supports common runtime-validable constructs from `typing`:\n\n### Primitive types\n\n```python\nchecked(1, int)          # 1\nchecked(\"x\", int)        # None\nchecked(True, int)       # None\n```\nNote: bool is not considered an int in this library.\n\n\n### Union (X | Y, typing.Union)\n\n```python\nchecked(1, int | str)      # 1\nchecked(\"x\", int | str)    # \"x\"\nchecked(1.5, int | str)    # None\n```\n\n### List / Set / FrozenSet\n\n```python\nchecked([1, 2], list[int])           # [1, 2]\nchecked({1, 2}, set[int])            # {1, 2}\nchecked(frozenset({1}), frozenset[int])\n```\n\nNested structures work as expected:\n```python\nchecked([[1, 2], [3]], list[list[int]])\n```\n\n### Dict\n\n```python\nchecked({\"a\": 1}, dict[str, int])\nchecked({\"a\": \"x\"}, dict[str, int])   # None\n```\n\nSupports unions and nesting:\n```python\nchecked({\"a\": [1, 2]}, dict[str, list[int]])\n```\n\n### Tuple\n\nFixed-length:\n```python\nchecked((1, \"x\"), tuple[int, str])\n```\n\nVariadic:\n```python\nchecked((1, 2, 3), tuple[int, ...])\n```\n\n### Literal\n\n```python\nfrom typing import Literal\n\nchecked(True, Literal[True])    # True\nchecked(False, Literal[True])   # None\n```\n\n### Any\n```python\nfrom typing import Any\n\nchecked(object(), Any)   # always matches\n```\n---\n\n## `is_type`\n\nThere is also a boolean helper:\n\n```python\nfrom py_typecheck import is_type\n\nif is_type(value, dict[str, int]):\n    ...\n```\n\n- It returns only True / False.\n- For most code paths, checked is preferred, because it avoids\ntype confusion and double-checking mistakes.\n\n---\n\n### Design principles\n- Explicit is better than clever\n-\tNo exceptions for control flow\n-\tNo implicit casting\n-\tNo data mutation\n-\tNo dependency on dataclasses or models\n-\tOne function, one responsibility\n\nThis is not a validator framework.\nThis is a runtime structural check with a safe return value.\n\n### Non-goals\n\n`py-typecheck` intentionally does not:\n-\tcoerce types (\"1\" → 1)\n-\tfill defaults\n-\tvalidate constraints (ranges, regexes, etc.)\n-\treplace pydantic or attrs\n\nIf you want parsing + validation + coercion → use `Pydantic`.\nIf you want “is this already the right shape?” → **use this**.\n\n---\n\n## Comparison\n\n|Tool|Purpose|\n|-|-|\n|isinstance|Runtime type only|\n|typing|Static type checking|\n|pydantic|Parsing + validation + coercion|\n|py-typecheck|Runtime structural check only|\n\n---\n\n## Development status\n-\tFully typed (basedpyright strict)\n-\tTested against Python 3.10 – 3.13\n-\tCI + lint + coverage\n-\tSmall surface area, easy to audit\n\n---\n\nLicense\nMIT\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschizza%2Fpy-typecheck","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fschizza%2Fpy-typecheck","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschizza%2Fpy-typecheck/lists"}