maybe
maybe(callback)
¶
Evaluate the callback to return a value.
Unlike Typescript, Python does not have optional chaining::
https://en.wikipedia.org/wiki/Safe_navigation_operator#Python
To simulate this we can use this function as follows::
ans = maybe(lambda: path.to.prop) # type: ignore[union-attr]
It is ok to disable the union-attr
mypy check as long as mypy is checking
for no-any-return
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable[[], T]
|
A function return a value. |
required |
Returns:
Type | Description |
---|---|
T | None
|
The value returned by the function or |
Source code in m/core/maybe.py
non_null(inst)
¶
Assert that inst
is not None
.
Implementation taken from::
https://github.com/python/typing/issues/645#issuecomment-501057220
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inst |
T | None
|
A possibly null instance. |
required |
Returns:
Type | Description |
---|---|
T
|
The same argument with the |