question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hook removes blank lines not related to imports at all

See original GitHub issue

Fantastic tool, however below is probably a bug.

Hook removes blank lines, that are not related to imports at all.

Example python script (notice blank lines around docstring):

$ cat test.py 
#!/usr/bin/env python3

"""
Here goes synopsis
"""

import os
import random
import time

print("asdf")

Here is my pre-commit config:

$ cat .pre-commit-config.yaml 
repos:
-   repo: https://github.com/asottile/reorder_python_imports
    rev: v1.3.5
    hooks:
    -   id: reorder-python-imports
        language_version: python3

Here is the run of the hook:

$ pre-commit run reorder-python-imports
Reorder python imports...................................................Failed
hookid: reorder-python-imports

Files were modified by this hook. Additional output:

Reordering imports in test.py

And the result:

$ cat test.py 
#!/usr/bin/env python3
"""
Here goes synopsis
"""
import os
import random
import time

print("asdf")

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
peterjccommented, Feb 12, 2020

@asottile would you consider a pull request for this (as stated in Feb 2019), or did you change your mind (as implied in the Aug 2019 comment)?

I would like reorder_python_imports to preserve all lines before the first import, just like it preserves all lines after the imports. I was about to log a duplicate issue until GitHub pointed me here.

Example input (with import sys before import os):

#!/usr/bin/env python
# Copyright 2020 Peter Cock.
# Released under the MIT Licence

"""This is a short example Python script.

This multiline string defined inside triple quotes is considered
to be the module's docstring.
"""

import sys
import os

sys.stderr.write(f"This is {os.uname().sysname}, aka {sys.platform}\n")

Example output from v1.5.1 (import order fixed, but also less blank lines):

#!/usr/bin/env python
# Copyright 2020 Peter Cock.
# Released under the MIT Licence
"""This is a short example Python script.

This multiline string defined inside triple quotes is considered
to be the module's docstring.
"""
import os
import sys

sys.stderr.write(f"This is {os.uname().sysname}, aka {sys.platform}\n")

I’ve seen enough real world Python code from other people to want to leave any blank lines before the first import alone. They can be purely cosmetic as in this example, but I have often seen loads of script documentation done as comment lines where blank lines were deliberate aids to readability.

0reactions
asottilecommented, Feb 12, 2020

I mean, I have my doubts that a simple patch can accomplish that – and I don’t want an option – and in your example the copyright / mit license lines aren’t necessary (that’s what the LICENSE file is for, MIT does not require a header – but sure you’d do that for apache2)

there has to be “one correct way” and currently that’s to remove it all

Read more comments on GitHub >

github_iconTop Results From Across the Web

Organize imports should remove blank lines within imports
I agree that the current behavior of moving blank lines to the end of the imports is not helpful. But removing them in...
Read more >
How to remove blank cells in Excel - Ablebits
The tutorial will teach you how to remove blank spaces in Excel to give your worksheets a clear and professional look. Empty cells...
Read more >
Quick fix "Remove unused imports" removes blank lines - Bugs
Using the Quick fix "Remove unusued imports" removes blank lines around the affected import. This is likely not intended, because when organizing imports, ......
Read more >
Git Hooks | Atlassian Git Tutorial
Git Hooks are scripts that run automatically every time a particular event occurs in a Git repository. Learn what they do and how...
Read more >
How To Manage State with Hooks on React Components
Since you are no longer using the useState Hook, you removed it from the import. Save the file. When you do, the page...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found