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

Exception clause doesn't allow tuples as variables for tuple of exception classes #1756

Closed
frenchtoast747 opened this issue Dec 1, 2017 · 3 comments

Comments

@frenchtoast747
Copy link

Steps to reproduce

  1. In a virtual environment, install Openstack's keystoneauth1 package: pip install keystoneauth1==3.2.0
  2. Create a small Python file:
    """
    asdf
    """
    from keystoneauth1 import exceptions
    
    # create a predefined tuple constant of exceptions to catch
    CONNECTION_FAILURE_EXCEPTIONS = (
        exceptions.connection.ConnectFailure,
        exceptions.connection.UnknownConnectionError,
    )
    
    try:
        raise exceptions.connection.ConnectFailure('Oops :(')
    # try to catch the tuple of exceptions, which is valid Python.
    except CONNECTION_FAILURE_EXCEPTIONS as exc:
        print 'Something bad happened! {}'.format(exc)
  3. Run pylint test.py

Current behavior

When running pylint on the above test file, it reports a catching-non-exception error for the tuple given:

E: 15, 7: Catching an exception which doesn't inherit from Exception: CONNECTION_FAILURE_EXCEPTIONS (catching-non-exception)

Expected behavior

I would expect that error not to be present. Consider the following similar example:

"""
asdf
"""

EXCEPTIONS = (
    ValueError,
    KeyError,
    TypeError,
)

try:
    raise TypeError('Oops :(')
except EXCEPTIONS as exc:
    print 'Something bad happened! {}'.format(exc)

When pylint is run on the above code, it doesn't report foul play on the EXCEPTIONS tuple.

pylint --version output

pylint 1.7.4,
astroid 1.5.3
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]


I'm not sure why this is failing under the circumstances. I have ensured that both of the Openstack Keystone Exception classes are indeed inheriting from Exception. Passing in a literal tuple of the keystone exceptions works as expected, but having a variable of the same tuple does not. I would imagine that the tuple traversal is failing or not going deep enough to determine if the classes inherit from Exception or BaseException.

@PCManticore
Copy link
Contributor

Thank you @frenchtoast747 ! The referenced commit should fix this problem, we're going to release it as part of 1.8 soon.

@frenchtoast747
Copy link
Author

Awesome! Thank you very much @PCManticore !

@dmtucker
Copy link

I believe #2431 (comment) suggests this is still open.

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

No branches or pull requests

3 participants