Skip to content
Myr

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

1 file+69 added−34 removed

modified:src/click/types.py+69 added-34 removed
1155 unmodified lines
1156 ty(x, param, ctx) for ty, x in zip(self.types, value, strict=False)1157 )115811591160def 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.1160def _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*.11651166 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 ty11721167 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 None11751174 # 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)11781184 guessed_type = True1179 # If the default is empty, return None so convert_type falls1180 # through to STRING.1181 if not default:1182 return None11831186 if isinstance(ty, tuple):1187 return Tuple(ty)1184 item = default[0]11851189 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))11911192 if ty is str or ty is None:1192 return type(item)119311941195@t.overload1196def convert_type(ty: None, default: None = None) -> StringParamType: ...119711981199@t.overload1200def convert_type(1201 ty: type[t.Any] | ParamType[t.Any], default: t.Any | None = None1202) -> ParamType[t.Any]: ...120312041205@t.overload1206def convert_type(1207 ty: t.Any | None, default: t.Any | None = None1208) -> ParamType[t.Any]: ...120912101211def 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 ty12201221 if isinstance(guessed, tuple):1222 return Tuple(guessed)12231224 if isinstance(guessed, ParamType):1225 return guessed12261227 if guessed is str or guessed is None:1228 return STRING12291195 if ty is int:1230 if guessed is int:1231 return INT12321198 if ty is float:1233 if guessed is float:1234 return FLOAT12351201 if ty is bool:1236 if guessed is bool:1237 return BOOL12381204 if guessed_type:1239 if is_guessed:1240 return STRING12411242 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 pass12511217 return FuncParamType(ty)1252 return FuncParamType(guessed)125312541255#: A dummy parameter type that just does nothing. From a user's1256#: perspective this appears to just be the same as `STRING` but
36 unmodified lines