bcb
bcb

Reputation: 2078

Why does Python use the None type hint instead of NoneType?

For Python's None object, the type hint None is used instead of its actual type, NoneType. From the docs:

Note that None as a type hint is a special case and is replaced by type(None).

I find this confusing and no further explanation is given.

Does anyone know the reason behind this?

Upvotes: 5

Views: 610

Answers (1)

Marco Bonelli
Marco Bonelli

Reputation: 69276

It's just a design choice. There's no particular reason behind it.

Writing def fn(x: None) -> None is more concise than def fn(x: NoneType) -> NoneType (and IMHO also clearer).

It's also consistent with other type hints: List, Dict, etc, which are all a single word (the same as the associated built-in function) with an uppercase first letter.

Upvotes: 3

Related Questions