You can use type(None)
to get the type object, but you want to use isinstance()
here, not type() in {...}
:
assert isinstance(value, (str, type(None)))
The NoneType
object is not otherwise exposed anywhere in Python versions older than 3.10*.
I'd not use type checking for that at all really, I'd use:
assert value is None or isinstance(value, str)
as None
is a singleton (very much on purpose) and NoneType
explicitly forbids subclassing anyway:
>>> type(None)() is None
True
>>> class NoneSubclass(type(None)):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: type 'NoneType' is not an acceptable base type
* As of Python 3.10, you could use types.NoneType
, to be consistent with the other singleton types being added to the types
module.