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

v8.2.0 raises Can't instantiate abstract class when a test class implements an abstract class #12275

Closed
MartinBraquet opened this issue Apr 30, 2024 · 4 comments · Fixed by #12318
Labels
plugin: unittest related to the unittest integration builtin plugin type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously

Comments

@MartinBraquet
Copy link

MartinBraquet commented Apr 30, 2024

Before, we could have a test class inheriting from an ABC (and unittest.TestCase), now it errors because pytest is trying to instantiate the ABC itself. Running pytest should only instantiate the child test class (which indeed implements all the abstract methods of its ABC parent).
This issue does not appear in v8.1.2 and was likely introduced in #12089.

Minimal example:

from abc import ABC, abstractmethod
from unittest import TestCase


class Base(TestCase, ABC):
    @abstractmethod
    def foo(self): ...


class Tests(Base):

    def foo(self): ...

    def test(self): ...

Output:

(test) tests 🔥pytest test.py    
============================================================================= test session starts ==============================================================================
platform darwin -- Python 3.9.19, pytest-8.2.0, pluggy-1.5.0
rootdir: ...
configfile: pytest.ini
plugins: env-1.1.3
collected 1 item / 1 error                                                                                                                                                     

==================================================================================== ERRORS ====================================================================================
________________________________________________________________ ERROR collecting test.py ________________________________________________________________
/Users/martin/miniconda3/envs/test/lib/python3.9/site-packages/_pytest/runner.py:341: in from_call
    result: Optional[TResult] = func()
/Users/martin/miniconda3/envs/test/lib/python3.9/site-packages/_pytest/runner.py:389: in collect
    return list(collector.collect())
/Users/martin/miniconda3/envs/test/lib/python3.9/site-packages/_pytest/unittest.py:90: in collect
    self.session._fixturemanager.parsefactories(self.newinstance(), self.nodeid)
/Users/martin/miniconda3/envs/test/lib/python3.9/site-packages/_pytest/unittest.py:75: in newinstance
    return self.obj("runTest")
E   TypeError: Can't instantiate abstract class Base with abstract method foo
=========================================================================== short test summary info ============================================================================
ERROR test.py::Base - TypeError: Can't instantiate abstract class Base with abstract method foo
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=============================================================================== 1 error in 0.04s ===============================================================================

pip list:

Package        Version
-------------- -------
exceptiongroup 1.2.1
iniconfig      2.0.0
packaging      24.0
pip            23.3.1
pluggy         1.5.0
pytest         8.2.0
pytest-env     1.1.3
setuptools     68.2.2
tomli          2.0.1
wheel          0.41.2

Thanks for your work in general, it's a great package.

@MartinBraquet MartinBraquet changed the title v8.2.0 raises Can't instantiate abstract class when test classes implements an abstract class v8.2.0 raises Can't instantiate abstract class when a test class implements an abstract class Apr 30, 2024
@The-Compiler
Copy link
Member

Your suspicion is correct, a bisect indeed points to 1a5e0eb.

@bluetech
Copy link
Member

bluetech commented May 6, 2024

Thanks for the report. I agree that what you're doing is very reasonable and should work.

Until we have some fix for this, if you want to remove the pin, a workaround would be to drop this snippet to a conftest.py file in the directory where the abc is defined:

import inspect

def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isabstract(obj):
        return []

@bluetech bluetech added type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously plugin: unittest related to the unittest integration builtin plugin labels May 6, 2024
@bluetech
Copy link
Member

Since I don't currently have time to attempt the change hinted at the final sentence in #12089, I suggest we just don't collect abstract classes for now. It probably makes some sense anyway -- the problem also exists for non-unittest test classes, so might as well fix it there as well.

@bluetech
Copy link
Member

This will be fixed in pytest 8.3.0 (this fix is a bit too scary to backport to 8.2.x).

enolfc added a commit to enolfc/cloud-info-provider that referenced this issue May 20, 2024
enolfc added a commit to enolfc/cloud-info-provider that referenced this issue May 20, 2024
enolfc added a commit to enolfc/cloud-info-provider that referenced this issue May 20, 2024
enolfc added a commit to EGI-Federation/cloud-info-provider that referenced this issue May 21, 2024
* New json template

* New JSON formatter

* New JSON formatter

* Use dict instead of list for other_info

* Fix syntax

* linter fixes

* Fix tests

* Fix variables

* Try to force older pytest versions

See pytest-dev/pytest#12275

* CentOS 7 is long gone

* Fix the formatter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: unittest related to the unittest integration builtin plugin type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants