astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
31.66k stars 1.06k forks source link

`ruff check --fix ` failed to fix the E402 error. #13151

Closed hongyi-zhao closed 1 month ago

hongyi-zhao commented 1 month ago

See below:

$ ruff --version
ruff 0.6.2

$ jupyter nbconvert --to python Untitled2.ipynb  
$ ruff check --fix --isolated Untitled2.py
Untitled2.py:75:1: E402 Module level import not at top of file
   |
73 | # In[1]:
74 | 
75 | from mp_api.client import MPRester
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E402
76 | 
77 | with MPRester() as mpr:
   |

Untitled2.py:87:1: E402 Module level import not at top of file
   |
85 | # In[3]:
86 | 
87 | from mp_api.client import MPRester
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E402
88 | from pymatgen.io.cif import CifWriter
89 | from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
   |

Untitled2.py:88:1: E402 Module level import not at top of file
   |
87 | from mp_api.client import MPRester
88 | from pymatgen.io.cif import CifWriter
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E402
89 | from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
   |

Untitled2.py:89:1: E402 Module level import not at top of file
   |
87 | from mp_api.client import MPRester
88 | from pymatgen.io.cif import CifWriter
89 | from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E402
90 | 
91 | # 使用你的API密钥初始化MPRester
   |

Found 4 errors.

On the other hand, the following command will fix these problems:

$ autopep8 --in-place --aggressive --aggressive Untitled2.py

Below are all the mentioned files here.

Untitled2.zip

Regards, Zhao

nathanjmcdougall commented 1 month ago

In general, import statements can have side effects, so a safe autofix is not possible.

Support for an unsafe fix is tracked here: https://github.com/astral-sh/ruff/issues/6514

hongyi-zhao commented 1 month ago

But even I use the --unsafe-fixes, it still doesn't work:

(datasci) werner@x13dai-t:~/Desktop/mp_workflow$ ruff check --fix --unsafe-fixes Untitled2.py 
Untitled2.py:75:1: E402 Module level import not at top of file
   |
73 | # In[1]:
74 | 
75 | from mp_api.client import MPRester
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E402
76 | 
77 | with MPRester() as mpr:
   |

Untitled2.py:87:1: E402 Module level import not at top of file
   |
85 | # In[3]:
86 | 
87 | from mp_api.client import MPRester
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E402
88 | from pymatgen.io.cif import CifWriter
89 | from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
   |

Untitled2.py:88:1: E402 Module level import not at top of file
   |
87 | from mp_api.client import MPRester
88 | from pymatgen.io.cif import CifWriter
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E402
89 | from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
   |

Untitled2.py:89:1: E402 Module level import not at top of file
   |
87 | from mp_api.client import MPRester
88 | from pymatgen.io.cif import CifWriter
89 | from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E402
90 | 
91 | # 使用你的API密钥初始化MPRester
   |

Found 4 errors.
nathanjmcdougall commented 1 month ago

ruff doesn't currently have an unsafe fix implemented for this rule.

Discussion about adding one is here: https://github.com/astral-sh/ruff/issues/6514

hongyi-zhao commented 1 month ago

Thank you for your comments.