← Back to index

specialtypes_none.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 3
FP: 0
FN: 0
Optional: 0 / 0
1"""
2Tests the handling of builtins.None in a type annotation.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/special-types.html#none
6
7from types import NoneType
8from typing import Hashable, Iterable, assert_type
9
11# > When used in a type hint, the expression None is considered equivalent to type(None).
14def func1(val1: None) -> None:
15 assert_type(val1, None)
16 t1: None = None
17 return None # OK
20func1(None) # OK
21func1(type(None)) # E
[invalid-argument-type] Argument to function `func1` is incorrect: Expected `None`, found `<class 'NoneType'>`
23# None is hashable
24none1: Hashable = None # OK
26# None is not iterable
27none2: Iterable = None # E: not iterable
[invalid-assignment] Object of type `None` is not assignable to `Iterable[Unknown]`
30None.__class__ # OK
31None.__doc__ # OK
32None.__eq__(0) # OK
35def func2(val1: type[None]):
36 assert_type(val1, type[None])
39func2(None.__class__) # OK
40func2(type(None)) # OK
41func2(None) # E: not compatible
[invalid-argument-type] Argument to function `func2` is incorrect: Expected `<class 'NoneType'>`, found `None`