rules/python/same-type-conversion.md

float(), int(), str() or bool() around a value that already has that type does nothing. Drop the call.

diff
1 def summary(prices: pd.Series, day: date) -> tuple[str, int, float, bool]:
2- return (
3- str(day.isoformat()),
4- int(prices.nunique()),
5- float(prices.std()),
6- bool(prices.empty),
7- )
8+ return day.isoformat(), prices.nunique(), prices.std(), prices.empty

What the rule reports

The rule reports float(), int(), str() and bool() around a value that has that type already. The table lists what it reports and what it leaves alone.

CallReported aroundLeft alone
float()mean, median, std, var, corr and autocorr without an axis, in a file that imports numpy or pandas. np.sqrt, np.exp, np.log, np.hypot, np.mean, np.median, np.percentile, np.quantile, np.average, np.interp, np.trapezoid, np.linalg.norm and the nan forms without an axis. A division with /A call with axis=, sem/skew/kurt (which pandas-stubs types as Scalar), return inside a -> float function, an assignment annotated with float, and a file that imports torch, jax, tensorflow, xarray, cupy, dask, mlx, polars or sympy
int()nunique(), .size, .ndim and .shape[0]sum, count, argmax and np.count_nonzero, which return np.int64. len(x) and round(x), which ruff's RUF046 reports
str()An f-string, and isoformat, strftime, format, join, strip, lower, upper, title, removeprefix, removesuffix, read_text, decode, hexdigest and as_posixA plain string, which ruff's UP018 reports
bool()not x, x in y, x is y, isinstance, any, all, startswith, endswith, exists, is_file, is_dir, .empty and .is_uniqueA comparison such as a < b, which returns np.bool on NumPy values

How to fix it

xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.

The safe fix writes the value without int(), str() or bool(). With the unsafe fix, the value without float(), because the mean of a float32 array is an np.float32, which is not a float. float() around a division has no fix.

xpd applies the replacement with --fix, and the one for float() with --fix --unsafe-fixes.

Each call converts a value to the type it has. The table shows what Python returned on NumPy 2.5.5 and pandas 3.1.1, and ty 0.0.113 accepted return x.mean() in a function that returns float for NDArray[np.float64] and pd.Series[float]. Operations whose stubs return np.floating[Any] (such as np.vdot) or Scalar (such as Series.sem(), Series.skew(), and Series.kurt() in pandas-stubs) are not subtypes of float for ty; convert those with .item() or narrow them with float() in a -> float return or : float assignment.

ExpressionTypeisinstance of the type in the name
a.mean(), s.median(), s.quantile(0.5), s.corr(s)np.float64float: True
np.sqrt(a[0]), np.median(a), np.percentile(a, 50), np.interp(1.5, a, a), np.linalg.norm(a)np.float64float: True
a.astype("float32").mean()np.float32float: False
s.nunique(), s.size, a.ndim, a.shape[0]intint: True
(s > 1).sum(), s.count(), a.argmax(), np.count_nonzero(a)np.int64int: False
s.empty, s.is_uniqueboolbool: True
(s > 1).any(), a[0] < 1np.boolbool: False

int() around sum, count and argmax does change the type, and ty rejects return x.argmax() in a function that returns int, so the rule leaves those calls alone. An np.int64 works as an index and in arithmetic without a conversion. Convert it once, where the value is written to JSON or returned as an int, and not at every step on the way.