Extract `_guess_type` from `convert_type` and add overloads (#3372)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kevin Deldycke · committed by GitHub · parent 25edc1e · Browse files · 1 file·+69 added−34 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Andreas Backx <AndreasBackx@users.noreply.github.com>
Diff stats
1 file+69 added−34 removed
- other:+69 added−34 removed
Provenance
- Commit
d2e4a42- Tree
2df0b94- Author
Kevin Deldycke <kdeldycke@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
25edc1e
1 file changed+69 added−34 removed
modified:src/click/types.py+69 added-34 removed
1155 unmodified lines1156 ty(x, param, ctx) for ty, x in zip(self.types, value, strict=False)1157 )1158 1159 1160-def convert_type(ty: t.Any | None, default: t.Any | None = None) -> ParamType[t.Any]:1161- """Find the most appropriate :class:`ParamType` for the given Python1162- type. If the type isn't provided, it can be inferred from a default1163- value.1160+def _guess_type(1161+ ty: type[t.Any] | ParamType[t.Any] | None,1162+ default: t.Any | None,1163+) -> type[t.Any] | tuple[type[t.Any], ...] | ParamType[t.Any] | None:1164+ """Infer a type from *ty* or *default*.1165+1166+ Returns *ty* unchanged when it is not ``None``. Otherwise inspects1167+ *default* to produce a ``type``, a ``tuple`` of types (for tuple1168+ defaults), or ``None``.1169 """1165- guessed_type = False1170+ if ty is not None:1171+ return ty1172 1167- if ty is None and default is not None:1168- if isinstance(default, (tuple, list)):1169- # If the default is empty, ty will remain None and will1170- # return STRING.1171- if default:1172- item = default[0]1173+ if default is None:1174+ return None1175 1174- # A tuple of tuples needs to detect the inner types.1175- # Can't call convert recursively because that would1176- # incorrectly unwind the tuple to a single type.1177- if isinstance(item, (tuple, list)):1178- ty = tuple(map(type, item))1179- else:1180- ty = type(item)1181- else:1182- ty = type(default)1176+ if not isinstance(default, (tuple, list)):1177+ return type(default)1178 1184- guessed_type = True1179+ # If the default is empty, return None so convert_type falls1180+ # through to STRING.1181+ if not default:1182+ return None1183 1186- if isinstance(ty, tuple):1187- return Tuple(ty)1184+ item = default[0]1185 1189- if isinstance(ty, ParamType):1190- return ty1186+ # A sequence of iterables needs to detect the inner types.1187+ # Can't call convert_type recursively because that would1188+ # incorrectly unwind the tuple to a single type.1189+ if isinstance(item, (tuple, list)):1190+ return tuple(map(type, item))1191 1192- if ty is str or ty is None:1192+ return type(item)1193+1194+1195+@t.overload1196+def convert_type(ty: None, default: None = None) -> StringParamType: ...1197+1198+1199+@t.overload1200+def convert_type(1201+ ty: type[t.Any] | ParamType[t.Any], default: t.Any | None = None1202+) -> ParamType[t.Any]: ...1203+1204+1205+@t.overload1206+def convert_type(1207+ ty: t.Any | None, default: t.Any | None = None1208+) -> ParamType[t.Any]: ...1209+1210+1211+def convert_type(1212+ ty: t.Any | None = None, default: t.Any | None = None1213+) -> ParamType[t.Any]:1214+ """Find the most appropriate :class:`ParamType` for the given Python1215+ type. If the type isn't provided, it can be inferred from a default1216+ value.1217+ """1218+ guessed = _guess_type(ty, default)1219+ is_guessed = guessed is not ty1220+1221+ if isinstance(guessed, tuple):1222+ return Tuple(guessed)1223+1224+ if isinstance(guessed, ParamType):1225+ return guessed1226+1227+ if guessed is str or guessed is None:1228 return STRING1229 1195- if ty is int:1230+ if guessed is int:1231 return INT1232 1198- if ty is float:1233+ if guessed is float:1234 return FLOAT1235 1201- if ty is bool:1236+ if guessed is bool:1237 return BOOL1238 1204- if guessed_type:1239+ if is_guessed:1240 return STRING1241 1242 if __debug__:1243 try:1209- if issubclass(ty, ParamType):1244+ if issubclass(guessed, ParamType):1245 raise AssertionError(1211- f"Attempted to use an uninstantiated parameter type ({ty})."1246+ f"Attempted to use an uninstantiated parameter type ({guessed})."1247 )1248 except TypeError:1214- # ty is an instance (correct), so issubclass fails.1249+ # guessed is an instance (correct), so issubclass fails.1250 pass1251 1217- return FuncParamType(ty)1252+ return FuncParamType(guessed)1253 1254 1255 #: A dummy parameter type that just does nothing. From a user's1256 #: perspective this appears to just be the same as `STRING` but36 unmodified lines