Skip to content

[FEATURE] Add scope analiythis #167

@Bobronium

Description

@Bobronium

Thesis

pycln should perform scope analythis and remove unused imports accordingly.

Reasoning

This would be consistent with flake8, pylance and PyCharms behaviour in identifying unused imports.

autoflake and unimport are able to remove them as well

image

cat test.py

import sys

def func():
    import sys

    def inner():
        import sys

        sys.exit(0)

pycln

$ pycln test.py                 
Looks good! ✨ 🍰 ✨
1 file left unchanged.

autoflake

$ autoflake test.py                                                  
--- original/test.py
+++ fixed/test.py
@@ -1,8 +1,7 @@
-import sys
 
 
 def func():
-    import sys
+    pass
 
     def inner():
         import sys

unimport

$ unimport -d test.py           
--- test.py

+++ 

@@ -1,8 +1,6 @@

-import sys
 
 
 def func():
-    import sys
 
     def inner():
         import sys

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions