Defines Unknown type for Python
Project description
typing-unknown
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.
[!TIP] If something is
object, type checkers assume:
- Its constructor accepts any
*args, or any**kwargs, both, or no arguments at all.- Its provided-by-default-but-overridable "magic" signatures - like
__bool__- are unchanged.
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typing_unknown-0.1.2.tar.gz.
File metadata
- Download URL: typing_unknown-0.1.2.tar.gz
- Upload date:
- Size: 21.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06fd497120a29adb38b7ddf720d107975d53aac47c037b3abaa5e145ac1df27c
|
|
| MD5 |
7d0f848d6333f0a65a2905900db15f39
|
|
| BLAKE2b-256 |
602fc3a10f7094054f3df065c1206f3a97d1128adb29ad99dc1e1409b85c15be
|
File details
Details for the file typing_unknown-0.1.2-py3-none-any.whl.
File metadata
- Download URL: typing_unknown-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
568256492600f4b3a519043bf3da1874b9408aaf61362e9d59a8be725448a668
|
|
| MD5 |
105a68fe6a8bf2794730526a7ba4ffc4
|
|
| BLAKE2b-256 |
43675ed91d05e68aec42297027c6fb339e79443655e1928a0d05420f147a98d7
|