File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ This document defines the API of the `cog` Python module, which is used to defin
28
28
- [ ` File() ` ] ( #file )
29
29
- [ ` Path() ` ] ( #path )
30
30
- [ ` Secret ` ] ( #secret )
31
+ - [ ` Optional ` ] ( #optional )
31
32
- [ ` List ` ] ( #list )
32
33
33
34
## ` BasePredictor `
@@ -390,6 +391,31 @@ any value passed to a `Secret` input is redacted after being sent to the model.
390
391
> Passing secret values to untrusted models can result in
391
392
> unintended disclosure, exfiltration, or misuse of sensitive data.
392
393
394
+ ## ` Optional `
395
+
396
+ Optional inputs should be explicitly defined as ` Optional[T] ` so that type checker can warn us about error-prone ` None ` values.
397
+
398
+ For example, the following code might fail if ` prompt ` is not specified in the inputs:
399
+
400
+ ``` python
401
+ class Predictor (BasePredictor ):
402
+ def predict (self , prompt : str = Input(description = " prompt" , default = None )) -> str :
403
+ return " hello" + prompt # TypeError: can only concatenate str (not "NoneType") to str
404
+ ```
405
+
406
+ We can improve it by making ` prompt ` an ` Optional[str] ` . Note that ` default=None ` is now redundant as ` Optional ` implies it.
407
+
408
+ ``` python
409
+ class Predictor (BasePredictor ):
410
+ def predict (self , prompt : Optional[str ]= Input(description = " prompt" )) -> str :
411
+ if prompt is None : # type check can warn us if we forget this
412
+ return " hello"
413
+ else :
414
+ return " hello" + prompt
415
+ ```
416
+
417
+ Note that the error prone usage of ` prompt: str=Input(default=None) ` might throw an error in a future release of Cog.
418
+
393
419
## ` List `
394
420
395
421
The List type is also supported in inputs. It can hold any supported type.
You can’t perform that action at this time.
0 commit comments