Skip to content

Should pytest.warns check for issubclass rather than class equality? #2151

Closed
@lesteve

Description

@lesteve

pytest.warns check for class equality and not subclass relationship. This snippet for example fails:

import warnings

import pytest


with pytest.warns(Warning):
    warnings.warn('user', UserWarning)

This was slightly surprising to me since my expectation was that it would be similar to pytest.raises where you can check for a base class:

import pytest


with pytest.raises(Exception):
    raise ValueError()

What do you guys think? After all you can always use with pytest.warns(None) as warninfo and check issubclass(warninfo[0].message, a_warning_class) if you want to check a subclass relationship. At the same time, I don't really how it would hurt to check subclass relationship.

I guess that is just a matter of changing this line by:

if not any(issubclass(r.category, each) for each in self.expected_warning for r in self):

I am willing to open a PR about this if you think it is worth it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions