密谋崩溃全局对象内容

plotly crashing over the globals object contents

当我介绍一个包含以下内容的 pytest.ini 文件时 content:

[pytest]
python_files = *.py

然后我遇到以下崩溃:

$ python -m pytest a.py 
========================================== test session starts ==========================================
platform linux -- Python 3.9.5, pytest-7.1.0, pluggy-1.0.0 , configfile: pytest.ini
collected 0 items / 1 error                                                                             

================================================ ERRORS =================================================
_________________________________________ ERROR collecting a.py _________________________________________
a.py:1: in <module>
    import plotly.express as px
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:986: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:680: in _load_unlocked
    ???
venv/lib/python3.9/site-packages/_pytest/assertion/rewrite.py:168: in exec_module
    exec(co, module.__dict__)
venv/lib/python3.9/site-packages/plotly/express/__init__.py:15: in <module>
    from ._imshow import imshow
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:986: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:680: in _load_unlocked
    ???
venv/lib/python3.9/site-packages/_pytest/assertion/rewrite.py:168: in exec_module
    exec(co, module.__dict__)
venv/lib/python3.9/site-packages/plotly/express/_imshow.py:3: in <module>
    from ._core import apply_default_cascade, init_figure, configure_animation_controls
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:986: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:680: in _load_unlocked
    ???
venv/lib/python3.9/site-packages/_pytest/assertion/rewrite.py:168: in exec_module
    exec(co, module.__dict__)
venv/lib/python3.9/site-packages/plotly/express/_core.py:8: in <module>
    from plotly.colors import qualitative, sequential
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:986: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:680: in _load_unlocked
    ???
venv/lib/python3.9/site-packages/_pytest/assertion/rewrite.py:168: in exec_module
    exec(co, module.__dict__)
venv/lib/python3.9/site-packages/plotly/colors/__init__.py:16: in <module>
    from _plotly_utils.colors import *  # noqa: F401
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:986: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:680: in _load_unlocked
    ???
venv/lib/python3.9/site-packages/_pytest/assertion/rewrite.py:168: in exec_module
    exec(co, module.__dict__)
venv/lib/python3.9/site-packages/_plotly_utils/colors/__init__.py:89: in <module>
    from . import (  # noqa: F401
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:986: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:680: in _load_unlocked
    ???
venv/lib/python3.9/site-packages/_pytest/assertion/rewrite.py:168: in exec_module
    exec(co, module.__dict__)
venv/lib/python3.9/site-packages/_plotly_utils/colors/qualitative.py:146: in <module>
    from .colorbrewer import Set1, Pastel1, Dark2, Set2, Pastel2, Set3  # noqa: F401
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:986: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:680: in _load_unlocked
    ???
venv/lib/python3.9/site-packages/_pytest/assertion/rewrite.py:168: in exec_module
    exec(co, module.__dict__)
venv/lib/python3.9/site-packages/_plotly_utils/colors/colorbrewer.py:465: in <module>
    globals()[_k + "_r"] = _cols[::-1]
E   TypeError: 'module' object is not subscriptable
======================================== short test summary info ========================================
ERROR a.py - TypeError: 'module' object is not subscriptable
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================== 1 error in 0.55s ============================================

以下简化目标 python 文件在将其作为 pytest 目标提供时重现了我的设置错误:

import plotly.express as px    
def test_a():
    pass

而没有配置文件,pytest 正常执行测试。

我可能会从错误中猜测某些库或路径,或者通过我上面的 pytest 配置引入的动态定义——包含或者是一个名为 globals 的模块,然后当测试被 pytest 运行 覆盖时,覆盖 python 的内置 globals 对象。然而,我通过多种搜索方法未能证明确实如此。

我设置 pytest 配置的目标是让它从项目的所有 python 文件中收集测试函数(因为我们在 class 文件的底部交错测试代码,而不仅仅是在标记为测试文件的文件)。

据我所知,问题不在于 globals() 不可订阅,而在于 _cols 不可订阅,因为 the source 显示 _cols获取模块中每个全局变量的值,并跳过一组硬编码名称的下标操作。

在 Plotly 方面,他们可以通过检查 _cols 的类型而不仅仅是 var 名称来使它对 pytest 所做的任何事情都更加健壮。

在你这边,我不确定 pytest 到底在做什么,但我注意到调用堆栈提到了断言重写,from descriptions online, could plausibly introduce new variables. Based on the last section 在那里,你可能会有些运气 运行 此代码使用 --assert=plain pytest 选项。因为我没有设置类似的环境,所以我很难比这更进一步。