{"id":3306,"library":"typish","title":"Typish: Extended Type Functionality","description":"Typish is a Python library that extends Python's native `typing` module, offering advanced functionality for thorough type checks, instance checks considering generics, and typesafe duck-typing. It provides utilities for introspection of type hints, such as retrieving origins and arguments of generic types, and determining optionality. Currently at version 1.9.3, it maintains a regular release cadence with a focus on bug fixes and compatibility improvements across Python versions.","status":"active","version":"1.9.3","language":"en","source_language":"en","source_url":"https://github.com/ramonhagenaars/typish","tags":["typing","type hints","generics","introspection","duck typing","type checking"],"install":[{"cmd":"pip install typish","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for execution, supports Python 3.5, 3.6, 3.7, 3.8, 3.9 as per PyPI metadata. Later versions (3.9+) have specific bug fixes and compatibility improvements.","package":"python","optional":false},{"reason":"Compatibility with nptyping was added in v1.8.0, indicating it works well with this library for NumPy type hints.","package":"nptyping","optional":true}],"imports":[{"note":"Used for advanced instance checks that respect generics and duck-typing.","symbol":"instance_of","correct":"from typish import instance_of"},{"note":"Retrieves the non-generic base type of a generic type (e.g., `list` from `List[str]`).","symbol":"get_origin","correct":"from typish import get_origin"},{"note":"Checks if a given type is considered optional (e.g., `Optional[str]` or `str | None`).","symbol":"is_optional_type","correct":"from typish import is_optional_type"},{"note":"A dictionary that uses instance checking for keys, useful for dispatching based on type.","symbol":"ClsDict","correct":"from typish import ClsDict"},{"note":"Decorator to capture the type hint of a variable before a function call.","symbol":"hintable","correct":"from typish import hintable"},{"note":"A backwards-compatible variant of `typing.Literal` for older Python versions.","symbol":"Literal","correct":"from typish import Literal"}],"quickstart":{"code":"from typing import Iterable\nfrom typish import instance_of\n\n# Basic instance check with generics\nresult_iterable = instance_of([1, 2, 3], Iterable[int])\nprint(f\"Is [1, 2, 3] an Iterable[int]? {result_iterable}\")\n\nfrom typish import get_origin\nfrom typing import List, Dict\n\n# Get the origin of a generic type\norigin_list = get_origin(List[str])\norigin_dict = get_origin(Dict[str, int])\nprint(f\"Origin of List[str]: {origin_list}\")\nprint(f\"Origin of Dict[str, int]: {origin_dict}\")\n\nfrom typish import is_optional_type, Literal\nfrom typing import Optional, Union\n\n# Check for optional types\nis_opt_str = is_optional_type(Optional[str])\nis_union_none = is_optional_type(Union[int, None])\nprint(f\"Is Optional[str] optional? {is_opt_str}\")\nprint(f\"Is Union[int, None] optional? {is_union_none}\")\n\n# Using typish.Literal for older Python compatibility (if needed)\nfrom typish import Literal\ndef process_status(status: Literal['success', 'failure']):\n    return f\"Processing status: {status}\"\n\nprint(process_status('success'))","lang":"python","description":"This quickstart demonstrates `typish.instance_of` for type-safe checks with generics, `typish.get_origin` for inspecting generic types, and `typish.is_optional_type` for identifying optional type hints. It also shows `typish.Literal` for broad compatibility of literal types."},"warnings":[{"fix":"Upgrade to `typish` version 1.9.2 or newer to ensure correct behavior with modern Python type hints. Alternatively, avoid using `get_origin` with directly parameterized built-in collections on affected Python versions.","message":"Prior to v1.9.2, `get_origin` did not correctly handle parameterized collections (e.g., `dict[str, str]`) in Python 3.9+.","severity":"gotcha","affected_versions":"<1.9.2"},{"fix":"Upgrade to `typish` version 1.8.0 or newer for robust `instance_of` checks with `Literal`.","message":"In versions older than 1.8.0, `instance_of` could fail when used with `Literal` types containing multiple arguments.","severity":"gotcha","affected_versions":"<1.8.0"},{"fix":"Update to `typish` version 1.8.0 or later to resolve issues with `get_origin` and shadowed class names.","message":"Before v1.8.0, `get_origin` might have failed when dealing with classes that had shadowing names, leading to incorrect type introspection.","severity":"gotcha","affected_versions":"<1.8.0"},{"fix":"Be mindful of your target Python versions when using complex type hints. When backporting or ensuring broad compatibility, use `from typish import Literal` and similar constructs, as `typish` often provides backward-compatible wrappers for newer `typing` features. Refer to Python's `typing` module documentation for version-specific syntax.","message":"Python's native `typing` module has evolved significantly across versions (e.g., `Union[X, Y]` vs `X | Y` syntax). While `typish` aims to provide compatibility, mixing `typing` constructs from different Python versions or relying on `typish` to completely abstract away core `typing` changes might lead to subtle issues if not carefully managed.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}