Skip to content

pytest with importlib & dataclass & __future__ annotations crash #7856

Closed
@yoav-orca

Description

@yoav-orca

I'm transitioning from pytest regular imports to --import-mode=importlib imports due to namespace collisions in tests. And I'm getting the following crash, no plugins installed, pytest 6.1.1 CPython 3.8.6, the issue reproduces on Mac & Linux.

File names test_xx.py:

from __future__ import annotations

from dataclasses import dataclass

@dataclass
class TestHelper:
    value: str

running pytest --import-mode=importlib test_xx.py and I get the following crash:

==================================================== test session starts ====================================================
platform darwin -- Python 3.8.5, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: /private/tmp/qwer/dir
collected 0 items / 1 error

========================================================== ERRORS ===========================================================
________________________________________________ ERROR collecting test_xx.py ________________________________________________
test_xx.py:6: in <module>
    class TestHelper:
/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/dataclasses.py:1019: in dataclass
    return wrap(cls)
/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/dataclasses.py:1011: in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/dataclasses.py:861: in _process_class
    cls_fields = [_get_field(cls, name, type)
/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/dataclasses.py:861: in <listcomp>
    cls_fields = [_get_field(cls, name, type)
/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/dataclasses.py:712: in _get_field
    and _is_type(f.type, cls, typing, typing.ClassVar,
/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/dataclasses.py:658: in _is_type
    ns = sys.modules.get(cls.__module__).__dict__
E   AttributeError: 'NoneType' object has no attribute '__dict__'
================================================== short test summary info ==================================================
ERROR test_xx.py - AttributeError: 'NoneType' object has no attribute '__dict__'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
===================================================== 1 error in 0.28s ======================================================

pip list:

Package    Version
---------- -------
attrs      20.2.0
iniconfig  1.0.1
packaging  20.4
pip        20.1.1
pluggy     0.13.1
py         1.9.0
pyparsing  2.4.7
pytest     6.1.1
setuptools 50.3.0
six        1.15.0
toml       0.10.1
wheel      0.35.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions