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

Use BaseException in raise_() #515

Merged
merged 1 commit into from
Oct 24, 2019

Conversation

jparise
Copy link
Contributor

@jparise jparise commented Oct 18, 2019

In both Python 2 and 3, the Exception type is derived from
BaseException:

The base class for all built-in exceptions. It is not meant to be
directly inherited by user-defined classes (for that, use Exception).

In practice, some libraries provide exception types that do derive
directly from BaseException (such as gevent.Timeout), and this code
should recognize them as valid exception types.

As further evidence, Python 2 explicitly states that raised types must
be derived from BaseException:

exceptions must be old-style classes or derived from BaseException

Python 3 is more flexible here, which is why we provide a TypeError case
for non-BaseException-derived types. While I'm here, I made that message
a little more helpful by including the name of the incompatible type.

See also #512

In both Python 2 and 3, the Exception type is derived from
BaseException:

  The base class for all built-in exceptions. It is not meant to be
  directly inherited by user-defined classes (for that, use Exception).

In practice, some libraries provide exception types that do derive
directly from BaseException (such as `gevent.Timeout`), and this code
should recognize them as valid exception types.

As further evidence, Python 2 explicitly states that raised types must
be derived from BaseException:

  exceptions must be old-style classes or derived from BaseException

Python 3 is more flexible here, which is why we provide a TypeError case
for non-BaseException-derived types. While I'm here, I made that message
a little more helpful by including the name of the incompatible type.
@jmadler jmadler merged commit e783561 into PythonCharmers:master Oct 24, 2019
@jparise jparise deleted the raise-base-exception branch October 25, 2019 21:37
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

Successfully merging this pull request may close these issues.

2 participants