[ty] only issue redundant-cast on fully static types#18058
[ty] only issue redundant-cast on fully static types#18058
Conversation
|
I feel like the situation for type-checker directives is a bit of a special case? If I'm a user and I do a big refactor and I accidentally end up with something like def f(x: tuple[Any, ...]):
# several hundred lines of code
y = cast(tuple[Any, ...], x)I'd definitely want a warning from my type checker telling me that I can remove the |
|
The vast majority of the false positives in the primer diff seem related to |
|
Hmm. I don't like introducing behavior distinctions between Also that would mean recursively walking the types twice, or else introducing a new |
nah, we could change the existing ruff/crates/ruff_python_ast/src/helpers.rs Lines 129 to 131 in 41fa082 and then we could change the call site so that it becomes something like ty.any_over_type(|atom| matches!(atom, Type::Dynamic(DynamicType::Todo | DynamicType::Unknown)) |
I do agree that this isn't great. I think the best fix for the underlying issue here would be to figure out why we're inferring |
Of course that's great as far as it goes, but I don't think it's a solution to this issue. There will always be reasons for things to be inferred as I agree that it seems like less of an issue with |
Summary
I initially was just going to silence redundant-cast from Unknown to Unknown (or Any to Any), since we keep seeing this crop up as a false positive, and it trivially violates the gradual guarantee to error on this.
But then I realized that the gradual-guarantee argument, and the entire theoretical basis for this change, applies to any non-fully-static type.
So I think we should just not issue this diagnostic at all, unless the casted type is fully static.
Test Plan
Adjusted/added mdtests.