Skip to main content

tipsy

python3.10 introduced user-defined TypeGuards. Finally giving users the ability to actually code type narrowing functions themselves and avoid shortfalls of mypy. However, for most builtin basic types automatic TypeGuards are still missing. Writing these TypeGuards yourself for each of your types from scratch can feel redundant and frustrating. tipsy generates these TypeGuards automatically for you based solely on the type annotations.

Example 1: TypeGuards

Here's an example of how you can use tipsy to generate TypeGuards for a TypedDict. Let's suppose you have a TypedDict like this:

class Person(TypedDict):
    uid: int
    name: str

You might already have observed that you can not use isinstance to check whether a variable is of that type.

isinstance({"uid": 5, "name": "jeeves"}, Person)
# TypeError: TypedDict does not support instance and class checks

So how can you do type narrowing in that case? Well, you need to write a TypeGuard yourself.

def is_person(data: Any) -> TypeGuard[Person]:
    return (isinstance(data, dict)
        and set(data.keys()) == {"uid", "name"}
        and isinstance(data["uid"], int)
        and isinstance(data["name"], str)
    )

is_person({"uid": 5, "name": "jeeves"}) # True
is_person({"name": "jeeves"}) # False
is_person({"uid": 5.0, "name": "jeeves"}) # False
is_person({"uid": 5, "name": b"jeeves"}) # False

As you can see this works perfectly, but these TypeGuards really feel like you are just duplicating the information that is already contained in the type, just to make mypy happy. Well, it's because it is actually duplicated. And even worse, for more complex nested types this gets out of hand quickly. tipsy's is_type uses just the information from the type to automatically generate such TypeGuards for you.

is_type({"uid": 5, "name": "jeeves"}, Person) # True
is_type({"name": "jeeves"}, Person) # False
is_type({"uid": 5.0, "name": "jeeves"}, Person) # False
is_type({"uid": 5, "name": b"jeeves"}, Person) # False

That's it, it's literally the same function, but automatically generated for you by tipsy.

Example 2: casting

Another use-case is casting, maybe you know that only one type is valid in a certain scenario. Let's say you again have the same TypedDict:

class Person(TypedDict):
    uid: int
    name: str

with tipsy, this is a breeze:

jeeves = cast_to_type({"uid": 5, "name": "jeeves"}, Person)

Metadata

Release files for tipsy 0.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for tipsy 0.7.1
File Interpreter ABI Platform Actions
tipsy-0.7.1-py3-none-any.whl Python 3 none any Details for tipsy-0.7.1-py3-none-any.whl

Release files / tipsy-0.7.1-py3-none-any.whl

Download URL tipsy-0.7.1-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
721774e6aee67558ef40854b1d290864508bd9659f225beb6ecfee8389f818a1
BLAKE2b-256 checksum
How to use checksums
5c198bcf6a5326e526d5eb6ce2dc2513623985ac73a257284a3dfe28507ba5ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Security for tipsy 0.7.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page