Instagram / Fixit

Advanced Python linting framework with auto-fixes and hierarchical configuration that makes it easy to write custom in-repo lint rules.
https://fixit.rtfd.io/en/latest/
Other
666 stars 62 forks source link

bug: Wrong typehint fix #389

Closed nchj closed 10 months ago

nchj commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

https://github.com/Instagram/Fixit/blob/84378a845746e2633882010061b808f67f836b3d/src/fixit/rules/use_types_from_typing.py#L25-L31

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 commented 1 year ago

cool, thanks, I am using python3.9

---Original--- From: @.> Date: Thu, Sep 7, 2023 12:28 PM To: @.>; Cc: "Jia @.**@.>; Subject: Re: [Instagram/Fixit] bug: Wrong typehint fix (Issue #389)

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

https://github.com/Instagram/Fixit/blob/84378a845746e2633882010061b808f67f836b3d/src/fixit/rules/use_types_from_typing.py#L25-L31

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)

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

amyreese commented 1 year ago

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