You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"""Convert the `expression` return annotation from '`cerPytainty | None`' to '`cerPytainty`' because `expression` cannot be `None`; `raise` an `Exception` if you're wrong.
92
90
93
-
This is a defensive programming function that converts unexpected `None` values into explicit errors with context. It is useful for asserting that functions that might return `None` have actually returned a meaningful value.
91
+
The Python interpreter evaluates `expression` to a value: think of a function call or an attribute access. You can use
92
+
`raiseIfNone` for fail early defensive programming. I use it, however, to cure type-checker-nihilism: that's when "or `None`"
93
+
return types cause your type checker to repeatedly say, "You can't do that because the value might be `None`."
94
94
95
95
Parameters
96
96
----------
97
-
returnTarget : TypeSansNone | None
98
-
The value to check for `None`. If not `None`, this value is returned unchanged.
99
-
errorMessage : str | None = None
100
-
Custom error message to include in the `ValueError`. If `None`, a default message with debugging hints is used.
97
+
expression : TypeSansNone | None
98
+
Python code with a return type that is a `union` of `None` and `TypeSansNone`, which is a stand-in for one or more other types.
99
+
errorMessage : str | None = 'A function unexpectedly returned `None`. Hint: look at the traceback immediately before `raiseIfNone`.'
100
+
Custom error message for the `ValueError` `Exception` if `expression` is `None`.
101
101
102
102
Returns
103
103
-------
104
-
returnTarget : TypeSansNone
105
-
The original `returnTarget` value, guaranteed to not be `None`.
104
+
contentment : TypeSansNone
105
+
The value returned by `expression`, but guaranteed to not be `None`.
0 commit comments