Why am I getting an error message in Python 'cannot import name NoneType'?

Question:

I’m trying to convert some code from 2 to 3 and the following simple script

import types
from types import NoneType

Results in

ImportError: cannot import name NoneType

How can I convert the above from 2 to 3?

Asked By: deltanovember

||

Answers:

There is no longer a NoneType reference in the types modules. You should just check for identity with None directly, i.e. obj is None. An alternative way, if you really need the NoneType, would be to get it using:

NoneType = type(None)

This is actually the exact same way types.NoneType was previously defined, before it was removed on November 28th, 2007.

As a side note, you do not need to import a module to be able to use the from .. import syntax, so you can drop your import types line if you don’t use the module reference anywhere else.

Answered By: poke

If your use case allows it, you can:

if my_dict.get('key'):  # By Default: returns None if key doesn't exists
    # Do something
else:
    # Do something with None

None is a falsy value so you can get away with if-else statement without importing anything.

Answered By: PrynsTag

Fetching the NoneType is useful when performing validation, to abbreviate the chain of conditions passing the allowed None type in the tuple of acceptable types. So instead of writing something like:

a = {
    # A dict with some fields...
}

# In this example, the optional field is valid if it is a string
is_valid = (
    a.get(optional_field) is None
    or isinstance(a[optional_field], str)
)

One could write:

is_valid = isinstance(a.get(optional_field), (str, NoneType))

It is possible to work around this issue by passing a default string value to .get(...) (because in the absence of the field, you would be returning something that intentionally passes the validation, even if the value is not there), but it’s too hacky, obscure and really affects readability.

As NoneType is not something you can import anymore, you can make your own when needed. These are some options:

NoneType = type(None)
NoneType = None.__class__

The linter may complain that a variable called NoneType doesn’t comply with your naming convention, if you are following PEP8 recommendations. I find it easier to simply use type(None) wherever you need a NoneType. It’s only two chars difference, just as readable and you save a line of code 😉

The final solution for the snippet above would look like:

is_valid = isinstance(a.get(optional_field), (str, type(None)))
Answered By: Victor Schröder
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.