Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

B014 recommends except (): #110

Closed
robinst opened this issue Jan 6, 2020 · 4 comments · Fixed by #129
Closed

B014 recommends except (): #110

robinst opened this issue Jan 6, 2020 · 4 comments · Fixed by #129

Comments

@robinst
Copy link

robinst commented Jan 6, 2020

In the following code:

    try:
        with open(fn) as f:
            return f.read().split('\n')
    except (OSError, IOError):
        pass

The new B014 warning shows this:

B014 Redundant exception types in except (OSError, IOError):. Write except ():, which catches exactly the same exceptions.

Doesn't seem to make sense to me. Is this a bug 🐻?

@cooperlees
Copy link
Collaborator

Thanks for the report, seems buggy to me. If someone does not beat me, I'll try add a test in the next few days and see if I can work out the bug.

@AlexandreYang
Copy link

I guess you are using Python3.

We encountered the same case: DataDog/integrations-core#5389

That's because in Python 3 OSError and IOError are equal.

$ python2 -c "print(OSError == IOError)"
False
$ python3 -c "print(OSError == IOError)"
True

In our case, we still need to support both Python 2 and Python 3 for now, hence we chose to # noqa: B014 that line.

Details

For Python 3:

Changed in version 3.3: EnvironmentError, IOError, WindowsError, socket.error, select.error and mmap.error have been merged into OSError, and the constructor may return a subclass.

But in Python 2 IOError and OSError are still conceptually two different error classes.

BaseException
 +-- Exception
      +-- StandardError
      |    +-- EnvironmentError
      |    |    +-- IOError
      |    |    +-- OSError
      |    |         +-- WindowsError (Windows)
      |    |         +-- VMSError (VMS)

source: https://docs.python.org/2/library/exceptions.html

@cooperlees
Copy link
Collaborator

Thanks for the insightful explanation.

@ghost ghost mentioned this issue Jan 21, 2020
@deveshks
Copy link

deveshks commented Jun 5, 2020

Is there any other fix for this issue if the code in question supports both Python 2 and Python 3 apart from adding # noqa: B014 ?

I am trying to add flake8-bugbear support for pip and this line complains with

B014 Redundant exception types in `except (OSError, IOError):`.  
Write `except ():`, which catches exactly the same exceptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants