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

bug: Wrong typehint fix #389

Closed
nchj opened this issue Sep 7, 2023 · 5 comments
Closed

bug: Wrong typehint fix #389

nchj opened this issue Sep 7, 2023 · 5 comments

Comments

@nchj
Copy link

nchj commented Sep 7, 2023

When I define a class named List, and use python buildin list as type hint, fixit will wrongly fix list to List

It seems that fixit is mistaking self defined List for Typing.List

class List:
    def __init__(self, data: list):
        self.data = data

fixit will fix it to

class List:
    def __init__(self, data: List):
        self.data = data
@llllvvuu
Copy link
Contributor

llllvvuu commented Sep 7, 2023

How to reproduce?

(fixit) ; cat > a.py        
class List:
    def __init__(self, data: list):
        self.data = data
(fixit) ; fixit fix a.py         
🧼 1 file clean 🧼
(fixit) ; 

@nchj
Copy link
Author

nchj commented Sep 7, 2023

How to reproduce?

(fixit) ; cat > a.py        
class List:
    def __init__(self, data: list):
        self.data = data
(fixit) ; fixit fix a.py         
🧼 1 file clean 🧼
(fixit) ; 

图片

@llllvvuu
Copy link
Contributor

llllvvuu commented Sep 7, 2023

It must be because of Python version, I am on 3.11.4 so I don't see it:

"""
Enforces the use of types from the ``typing`` module in type annotations in place
of ``builtins.{builtin_type}`` since the type system doesn't recognize the latter
as a valid type before Python ``3.10``.
"""
PYTHON_VERSION = "< 3.10"

But indeed it is a bug that it doesn't auto-import (well, that's a bit tricky to implement since you'd have to import as; maybe an easier fix would be to return no autofix)

@nchj
Copy link
Author

nchj commented Sep 7, 2023 via email

@amyreese
Copy link
Member

amyreese commented Sep 7, 2023

Unfortunately, having rules add imports for newly referenced objects is still something that we need to improve.

For this specific case, it would probably be easiest to disable this specific rule in your fixit config:

disable = ["fixit.rules:UseTypesFromTyping"]

See the config guide for details: https://fixit.readthedocs.io/en/stable/guide.html#configuration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants