Flake8 Not Recognizing built-in exception
Asked Answered
C

3

23

I have a strict pre-commit hook set up with flake8, so I can't commit until its happy. This is normally a good thing, but it is causing some problems with what seems like no problem at all.

I can't get Flake8 to recognize a FileNotFoundError.

example.py

try:
    pass
except FileNotFoundError:
    pass

This is enough code to get Flake8 to generate the error

$ flake8 example.py
example.py:3:8: F821 undefined name 'FileNotFoundError'
$ python example.py # no error
$ python3 example.py # no error

I checked the python docs, and FileNotFoundError is a 'built-in' exception, so I don't think I should have to import it from anywhere, and my python interpreters have not complained about it, just seems like an issue with flake8.

Crumple answered 14/8, 2014 at 13:15 Comment(0)
B
10

Running python 2.7.8 I get the follwoing:

 except FileNotFoundError:
 NameError: name 'FileNotFoundError' is not defined

If I change to OSError it works correctly on python 3

except OSError:

FileNotFoundError is a subclass of OSError in python3

Using OSError will catch more than just the FileNotFound error in python 3 so you may want to add additional checks.

For python 2 it is an IOError:

You can catch the specific error:

import __builtin__

exc = getattr(__builtin__,"IOError","FileNotFoundError")

try:
    (open("bad_file"))
except exc:
    pass

Sure there are betters ways but for python 3:

try:
    f = (open("bad_file"))
except OSError as e:
    if isinstance(e, FileNotFoundError):
        pass
    else:
        raise e
Belen answered 14/8, 2014 at 13:25 Comment(4)
I don't get that error in 2.7.6, but I suppose I can change to OSError.Crumple
@NateMara, it is IOError in python 2 but OSError for python 3Belen
This is a pyflakes question, not a question about implementation details. I think that the root cause of the OP's problem is mostly likely that pyflakes was running under python 2. See the comment from @Bryce below for what is probably a more fitting solution.Amylopectin
Rewriting a code in such cases - not a good idea. You should provide a proper configuration for flake8 . @Bryce answer is a better choice for this caseHoke
D
23

I found a couple of mentions of this issue on the Python Code Quality tools repo. Specifically Issue #75.

Two workarounds were listed. You can use the --builtins flag to specify a comma separated list of known builtins that flake8 is flagging.

$ flake8 example.py
example.py:3:8: F821 undefined name 'FileNotFoundError'
$ flake8 --builtins=FileNotFoundError,... example.py
$ 

The other workaround is running flake8 under python3 instead of python2.

$ /usr/bin/python3.5 -m pyflakes example.py 
$ 

Hopefully one of these two solutions will work out for you, as twisting your code up to work around a syntax check tool is counterproductive.

Defibrillator answered 21/12, 2015 at 2:39 Comment(0)
B
10

Running python 2.7.8 I get the follwoing:

 except FileNotFoundError:
 NameError: name 'FileNotFoundError' is not defined

If I change to OSError it works correctly on python 3

except OSError:

FileNotFoundError is a subclass of OSError in python3

Using OSError will catch more than just the FileNotFound error in python 3 so you may want to add additional checks.

For python 2 it is an IOError:

You can catch the specific error:

import __builtin__

exc = getattr(__builtin__,"IOError","FileNotFoundError")

try:
    (open("bad_file"))
except exc:
    pass

Sure there are betters ways but for python 3:

try:
    f = (open("bad_file"))
except OSError as e:
    if isinstance(e, FileNotFoundError):
        pass
    else:
        raise e
Belen answered 14/8, 2014 at 13:25 Comment(4)
I don't get that error in 2.7.6, but I suppose I can change to OSError.Crumple
@NateMara, it is IOError in python 2 but OSError for python 3Belen
This is a pyflakes question, not a question about implementation details. I think that the root cause of the OP's problem is mostly likely that pyflakes was running under python 2. See the comment from @Bryce below for what is probably a more fitting solution.Amylopectin
Rewriting a code in such cases - not a good idea. You should provide a proper configuration for flake8 . @Bryce answer is a better choice for this caseHoke
I
1

In addition to Bryce's answer, another option which avoids contorting the code to fit the syntax check tool, is to add a noqa flag to the relevant line:

try:
    open("bad_file")
except FileNotFoundError: # noqa: F821
    print("file not found")

This just tells flake8 to ignore this particular error on this particular line.

Imray answered 6/9, 2019 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.