Search code examples
bashgitflake8

flake8 --diff not working from directory except repository root


Repository tree is shown as above

.
├── __init__.py
└── source
    ├── __init__.py
    └── main.py

main.py file

def add(a, b):
    return a + b

When I change file main.py to

def add(a, b):
    return a +b

and make git diff -U0 | flake8 --diff from repository root it shows me

source/main.py:2:15: E225 missing whitespace around operator

but when I make the same command from source folder it doesn't show anything

At the same moment git diff -U0 shows an identical result inside root repository and source folder

diff --git a/source/main.py b/source/main.py
index 4693ad3..fd47298 100644
--- a/source/main.py
+++ b/source/main.py
@@ -2 +2 @@ def add(a, b):
-    return a + b
+    return a +b

Solution

  • Apparently flake8 would like the paths in the output to be relative to the current working directory. That means you want:

    git diff -U0 --relative | flake8 --diff
    

    since by default, git diff produces:

    --- a/source/main.py
    +++ b/source/main.py
    

    as in your example. Adding --relative (which defaults to the current directory) should produce:

    --- a/main.py
    +++ b/main.py
    

    instead.