-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with new PYI034
autofix when first param is annotated
#14421
Comments
Admittedly, the fix is unsafe here (and, indeed, it was marked as such), but I'm not sure if this falls within the scope of |
I don't think this falls under PYI019, there are no typevar used here. Ruff probably doesn't need to try and fix code like The autofix is indeed unsafe, as there are cases where you wouldn't want to use However (and if) this case ends up being handled, both known reasons for this fix to be unsafe could be documented.
|
Maybe Ruff should have a rule to disallow (and remove) non-typevar annotations on |
@InSyncWithFoo If I recall, then #14238 handles type annotations in |
I think it would only be safe to remove the annotation (not just replace) when:
Consider this contrived example: # Before
class Container[T]:
def __init__(self, v: T, /) -> None: ...
def map(self: Container, mapper: Callable, /) -> Container: ...
# ^^^^^^^^^ `Container[Any]` ^^^^^^^^^
# After
class Container[T]:
def __init__(self, v: T, /) -> None: ...
def map(self, mapper: Callable, /) -> Self: ...
# `Container[T]` ^^^^ c = Container(0) # Container[int]
# Before
reveal_type(c.map(lambda _: '')) # Container[Any]
# After
reveal_type(c.map(lambda _: '')) # Container[int] |
Thanks @InSyncWithFoo for adding an autofix in #14217 .
I did notice an issue that I think should be possible to handle. This case:
gets autofixed to
Which causes type error:
I think it should be:
ruff /path/to/file.py --fix
), ideally including the--isolated
flag.ruff check --select=PYI034 --fix --preview --unsafe-fixes --isolated
pyproject.toml
).None
ruff --version
).ruff 0.7.4
The text was updated successfully, but these errors were encountered: