Skip to main content

automatic typeguards

Project description

arketip

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. arketip is an effort to generate these TypeGuards automatically based solely on the type annotations.

Example 1: TypeGuards

Here's an example of how you can use arketip 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. arketip'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 arketip.

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 arketip, this is a breeze:

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

arketip-0.5-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file arketip-0.5-py3-none-any.whl.

File metadata

  • Download URL: arketip-0.5-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for arketip-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e763e81d0f64466a210cfae7516a4efc5dd3f719eb882b788259b22f14295bbd
MD5 62965cb7adcc492413d3d88e8708262f
BLAKE2b-256 bef974c2b7a1a6a7c36211da22299a099a05223f3964106e195e80e6f9b30c04

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page