Library of Assembled Shared Sources
lass::python::MaybeNone< T > Class Template Reference

Wrapper to type-hint return values in Python that maybe None but not likely. More...

#include <maybe_none.h>

Public Member Functions

T & reference ()
 Return reference to wrapped value.
 
const T & reference () const
 Return reference to wrapped value.
 

Detailed Description

template<typename T>
class lass::python::MaybeNone< T >

Wrapper to type-hint return values in Python that maybe None but not likely.

Use this class to wrap a type T that could be None in Python (for example, a pointer type), but it is not likely to be None and forcing the user to expliclitly check for None can more painful than helpful. Annotating the type as T | MaybeNone instead of T | None will make static type-checkers like mypy ignore the None case, while still allowing the user to check for None if they want to. See The Any Trick for more information.

Note
This class does NOT prevent None values from being passed or returned, and it does NOT provide any runtime or compile-time checks whatsoever. It only alters the type-hint in Python for static type-checkers.

MaybeNone<T> types can only be returned from C++ to Python, it doesn't make sense to have them as function parameters in C++.

Built-in support for MaybeNone is provided for raw pointers T*, util::SharedPtr<T>, std::shared_ptr<T>, and std::optional<T>. To support your own types, you must specialize PyExportTraits, see PyExportTraitsMaybeNone for more information.

See also
PyExportTraitsMaybeNone
PyExportTraits<MaybeNone<T*>>
PyExportTraits<MaybeNone<util::SharedPtr<T,S,C>>>
PyExportTraits<MaybeNone<std::shared_ptr<T>>>
PyExportTraits<MaybeNone<std::optional<T>>>
NoNone

Definition at line 81 of file maybe_none.h.


The documentation for this class was generated from the following file: