@@ -730,7 +730,7 @@ def _(event):
730730 return input_string
731731
732732 @staticmethod
733- def input (
733+ def input [ T ] (
734734 prompt : object = "" ,
735735 start = "" ,
736736 end = "" ,
@@ -742,7 +742,9 @@ def input(
742742 allowed_chars : str = CHARS .ALL , #type: ignore[assignment]
743743 allow_paste : bool = True ,
744744 validator : Optional [Callable [[str ], Optional [str ]]] = None ,
745- ) -> str :
745+ default_val : Optional [T ] = None ,
746+ output_type : type [T ] = str , # type: ignore[assignment]
747+ ) -> T :
746748 """Acts like a standard Python `input()` a bunch of cool extra features.\n
747749 ------------------------------------------------------------------------------------
748750 - `prompt` -⠀the input prompt
@@ -758,12 +760,15 @@ def input(
758760 - `allow_paste` -⠀whether to allow pasting text into the input or not
759761 - `validator` -⠀a function that takes the input string and returns a string error
760762 message if invalid, or nothing if valid
763+ - `default_val` -⠀the default value to return if the input is empty
764+ - `output_type` -⠀the type (class) to convert the input to before returning it\n
761765 ------------------------------------------------------------------------------------
762766 The input prompt can be formatted with special formatting codes. For more detailed
763767 information about formatting codes, see the `format_codes` module documentation."""
764768 result_text = ""
765769 tried_pasting = False
766770 filtered_chars = set ()
771+ has_default = default_val is not None
767772
768773 class InputValidator (Validator ):
769774
@@ -905,4 +910,16 @@ def _(event: KeyPressEvent) -> None:
905910 FormatCodes .print (start , end = "" )
906911 session .prompt ()
907912 FormatCodes .print (end , end = "" )
908- return result_text
913+
914+ if result_text in ("" , None ):
915+ if has_default : return default_val
916+ result_text = ""
917+
918+ if output_type == str :
919+ return result_text # type: ignore[return-value]
920+ else :
921+ try :
922+ return output_type (result_text ) # type: ignore[call-arg]
923+ except (ValueError , TypeError ):
924+ if has_default : return default_val
925+ raise
0 commit comments