Skip to content
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

UP036 auto-fixer breaks code at runtime after #5766 #6189

Closed
mrcljx opened this issue Jul 31, 2023 · 8 comments · Fixed by #6237
Closed

UP036 auto-fixer breaks code at runtime after #5766 #6189

mrcljx opened this issue Jul 31, 2023 · 8 comments · Fixed by #6237
Labels
bug Something isn't working

Comments

@mrcljx
Copy link

mrcljx commented Jul 31, 2023

After #5766 the auto-fixer for UP036 has broken some code for us:

from typing import Any

import cachetools

class MyClass:
    def __init__(self) -> None:
        self._cache: "cachetools.LRUCache[Any, Any]" = cachetools.LRUCache(maxsize=100)
ruff --isolated --select UP --fix ruff_up037.py

ruff_up037.py:7:22: UP037 [*] Remove quotes from type annotation

The auto-fixer strips the quotes. However this is semantically not valid, as LRUCache is not Generic at runtime, only for the pyi files.

$ ruff --version
ruff 0.0.280
@charliermarsh charliermarsh added the bug Something isn't working label Jul 31, 2023
@clement-escolano
Copy link

clement-escolano commented Aug 1, 2023

I would like to add another example of the rule breaking code at runtime:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from dependency import MyClass

def my_func():
    result: list["MyClass"] = []
    return result

The quotes around MyClass are removed which breaks at run time.

If this should be in a separate issue, please tell me and I will open one

@charliermarsh
Copy link
Member

@clement-escolano -- Hmm, I'm unable to reproduce. This, for example, runs without error for me:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from dependency import MyClass

def my_func():
    result: list[MyClass] = []
    return result

my_func()

Running python foo.py completes without error.

@clement-escolano
Copy link

@charliermarsh
The issue is that dependency is not installed in our production environment (in our project, this is django_stubs_ext which is only used in development for typing). If this is out of scope of this rule, I understand :-)

@charliermarsh
Copy link
Member

I don't think the dependency has to be installed in order for the above to work, I don't have a dependency called dependency locally :) Python doesn't evaluate unquoted annotated assignments within function bodies. But perhaps there's another reason that it's failing for you? Or is it just that it's showing up as an error in your editor?

The same is true of the originating comment @mrcljx. This runs fine for me:

from typing import Any

import cachetools

class MyClass:
    def __init__(self) -> None:
        self._cache: cachetools.LRUCache[Any, Any] = cachetools.LRUCache(maxsize=100)

MyClass()

charliermarsh added a commit that referenced this issue Aug 1, 2023
This is causing some problems, so we'll just revert for now.

Closes #6189.
@mrcljx
Copy link
Author

mrcljx commented Aug 1, 2023

@charliermarsh This indeed seems to have been an IDE bug. Very sorry for bothering you with this report, thanks for Ruff!

@charliermarsh
Copy link
Member

No worries, I went ahead and reverted any way, it seems like it's going to cause some annoyance for folks.

@zanieb
Copy link
Member

zanieb commented Aug 1, 2023

@mrcljx Which IDE? Did you open an issue there?

@mrcljx
Copy link
Author

mrcljx commented Aug 1, 2023

CleanShot 2023-08-01 at 19 53 31@2x

PyCharm. That said, it also had that warning earlier (but it became more visually pronounced in ^):

CleanShot 2023-08-01 at 19 54 10@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants