Skip to main content

typing-unknown

PyPI Tests codecov License Black Pyright

Unknown: The missing member of Python's typing module

pip install typing-unknown

Use Unknown as a drop-in (but more strict) replacement for object in type hints.

from typing_unknown import Unknown

def handle_thing(obj: Unknown):
    # Caller may pass any value, but we're limited in how we can use it.

Why Unknown?

Historically, object has served the implicit role of "Unknown" in type hints, preferred over typing.Any when you wish to limit how a value may be used before checking its type.

But, object isn't as strict as you'd think. For example, static type checkers consider the following code valid (no type issues):

def handle_unknown_thing(obj: object | MyType, *args: float):
    if obj:
        obj_class = type(obj)
        instance = obj_class(*args)

But, this code could be invalid. In multiple ways. Even if obj were of a valid, compatible subtype of object, with no hidden or unusual behavior.

To static type checkers, Unknown more accurately reflects what can and cannot be done with an arbitrary, valid object.

Same code, after swapping object for Unknown:

def handle_unknown_thing(obj: Unknown | MyType, *args: float):
    if obj: # PYRIGHT ERROR: Invalid conditional operand
        obj_class = type(obj)
        instance = obj_class(*args) # PYRIGHT ERROR: reportArgumentType

Perfect. This forces us to safely narrow the type of obj (or explicitly suppress the issues):

# Pyright errors are gone. Code is "safe" now.

def handle_unknown_thing(obj: Unknown | MyType, *args: float):
    if obj is not None and isinstance(obj, MyType):
        obj_class = type(obj)
        instance = obj_class(*args)

Metadata

Release files for typing-unknown 0.1.2

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

Source distribution (sdist)

Source distribution for typing-unknown 0.1.2
File Size Uploaded
typing_unknown-0.1.2.tar.gz 21.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for typing-unknown 0.1.2
File Interpreter ABI Platform
typing_unknown-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 26.2 kB

Release files / typing_unknown-0.1.2.tar.gz

Download URL typing_unknown-0.1.2.tar.gz
Size 21.6 kB
Tags Source
SHA-256 checksum
How to use checksums
06fd497120a29adb38b7ddf720d107975d53aac47c037b3abaa5e145ac1df27c
BLAKE2b-256 checksum
How to use checksums
602fc3a10f7094054f3df065c1206f3a97d1128adb29ad99dc1e1409b85c15be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / typing_unknown-0.1.2-py3-none-any.whl

Download URL typing_unknown-0.1.2-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
568256492600f4b3a519043bf3da1874b9408aaf61362e9d59a8be725448a668
BLAKE2b-256 checksum
How to use checksums
43675ed91d05e68aec42297027c6fb339e79443655e1928a0d05420f147a98d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

1 release file

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