Back to all reviewers

Explicit null checks

apache/mxnet
Based on 4 comments
Python

Always use explicit null checks (`value is None` or `value is not None`) rather than implicit truthiness evaluations when testing for null/None values. Implicit boolean checks can invoke `__bool__` methods leading to unexpected behavior.

Null Handling Python

Reviewer Prompt

Always use explicit null checks (value is None or value is not None) rather than implicit truthiness evaluations when testing for null/None values. Implicit boolean checks can invoke __bool__ methods leading to unexpected behavior.

Do this:

# Explicitly check if out is not None
if out is not None:
    # Use out
    process(out)

Not this:

# Don't rely on truthiness which may call __bool__
if out:
    # May not behave as expected if __bool__ is implemented
    process(out)

For complex types that can represent optional values, use proper optional type wrappers:

In C++:

// Use optional wrappers for nullable types
'float or None': 'dmlc::optional<float>'  // Instead of just 'mx_float'

When handling values that might have multiple return types or be null:

# Check the specific type before processing
if isinstance(out, NDArrayBase):
    return out
return list(out)  # Convert if needed
4
Comments Analyzed
Python
Primary Language
Null Handling
Category

Source Discussions