mfussenegger / nvim-dap-python

An extension for nvim-dap, providing default configurations for python and methods to debug individual test methods or classes.
GNU General Public License v3.0
492 stars 46 forks source link

Running async test method raises an error #145

Closed gertjanstulp closed 2 weeks ago

gertjanstulp commented 1 month ago

Hi, running the test_method method on an async python test raises an error in neovim. The method looks something like this:

async def test_something():
    await some_async_method()
    <checks, not relevant for this issue>

If I try to run this test I get the following error:

E5108: Error executing lua ...local/share/nvim/lazy/nvim-dap-python/lua/dap-python.lua:336: assertion failed!
stack traceback:
        [C]: in function 'assert'
        ...local/share/nvim/lazy/nvim-dap-python/lua/dap-python.lua:336: in function '_get_nodes'
        ...local/share/nvim/lazy/nvim-dap-python/lua/dap-python.lua:423: in function 'test_method'
        [string ":lua"]:1: in main chunk

I took a quick look at the code around line 336 and I think that the failing assertion doesn't take async methods into account. It verifies if the second child element of the parent node is an identifier:

local ident = parent:child(1)
assert(ident:type() == "identifier")

With an async method this breaks as the second child element is actually the 'def' element. In that case I think it should verify the third child element. I tried it out by locally modifying the code on line 335 to

local ident = parent:child(2)
assert(ident:type() == "identifier")

This resolved the issue and I could successfully run the test.

I'm using pytest as test_runner in my setup.

Avishayy commented 3 weeks ago

@gertjanstulp thanks for reporting this, this annoyed me too, I created a PR for a fix (to support both async and sync tests) in #148