29

In this code snippet,

def add(x:int, y:int) -> int:
    return x + y

there are function annotations that are only supported after python 3.0

When I execute flake8 for this python code:

$ flake8 7.3.py -vv
checking 7.3.py
def add(x: int, y: int) -> int:
return x + y
7.3.py:1:11: E901 SyntaxError: invalid syntax

I got the invalid syntax error, but it should be valid syntax. How can I use flake8 to check the syntax that is only supported in Python 3.x?

Dan Oberlam
  • 2,435
  • 9
  • 36
  • 54
Cody
  • 4,353
  • 4
  • 39
  • 42

6 Answers6

20

See: https://bugs.launchpad.net/pyflakes/+bug/989203

NB: Whilst this bug report indicates some level of resolution, testing the latest version of pyflakes 0.8.1 this lack of Python 3 Annotations still exists.

I guess you'd have to file a separate new feature request to pyflakes.

pyflakes Bugs

$ cat - > foo.py
def add(x:int, y:int) -> int:
    return x + y
^D
$ pyflakes --version
0.8.1

$ pyflakes foo.py
foo.py:1:10: invalid syntax
def add(x:int, y:int) -> int:
         ^

UPDATE (20140514):

As it turns out the actual answer to this problem is to run pyflakes or flake8 under Python 3.x instead of Python 2.x. It makes sense :)

So do something like this:

/usr/bin/python3 -m pyflakes foo.py

See: http://codepad.org/9BKxSZaD

Tim
  • 41,901
  • 18
  • 127
  • 145
James Mills
  • 18,669
  • 3
  • 49
  • 62
  • Thanks, I have submitted a bug report: https://bugs.launchpad.net/pyflakes/+bug/1318497 – Cody May 12 '14 at 06:30
  • I got the feedback from pyflakes. We need to execute pyflakes in Python 3 to rid of that error. That is, pyflakes in python 2 can't be used fro python 3 scripts. – Cody May 13 '14 at 01:05
  • That actually makes sense! Not sure why I didn't think of that! I'll update the answer to include this! Thanks! – James Mills May 14 '14 at 06:19
9

I've got the answer on reddit(here):

It uses whatever flake8 is installed in the Python in your path.

so you need to install flake8 by pip3, not pip.

on OSX for me,

pip3 install flake8

this works for me. :)

ZyusAn
  • 369
  • 1
  • 4
  • 13
3

worked for Mac + py2 + py3 + venv:

pip install flake8
flake8 --version
3.5.0 (mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.5.0) CPython 2.7.14 on Darwin

pip3 install flake8
python3 -m flake8 --version
3.5.0 (mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.6.0) CPython 3.6.1 on Darwin
python3 -m flake8 --exclude migrations --max-line-length=121
Legolas Bloom
  • 1,725
  • 1
  • 19
  • 17
  • worked with Distributor ID: Ubuntu Description: Ubuntu 18.04.1 LTS Release: 18.04 Codename: bionic – simkusr Nov 12 '18 at 20:31
1

You need to ensure you are using python3's flake8. On linux you'll want to do:

sudo pip uninstall flake8
sudo pip3 install flake8
0

Y'all probably should be using virtual environments (python3 -m venv venv; source venv/bin/activate)... That way, 'python' and 'pip' use the ones you want, by default... Just saying.

Tom Hundt
  • 1,694
  • 19
  • 18
0

I had a similar issue and solved it by installing flake8 through python.

$ python --version
Python 3.8.3

$ python -m pip install flake8
Azam
  • 1
  • this is not a valid answer to this question. – Vishal Singh Jun 29 '20 at 22:46
  • I have same error as in the original question. I tried an accepted answer and it didn't work. I found installing through python resolved the module version conflict. – Azam Jul 01 '20 at 00:25