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

should_never_reach_here for particular test case when parametrized test has unmatched brackets or parenthesis #17676

Closed
Tracked by #17242
ericmclachlan opened this issue Oct 9, 2021 · 10 comments
Assignees
Labels
area-testing bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster

Comments

@ericmclachlan
Copy link

Environment data

  • VS Code version: 1.62.0-insider
  • Extension version (available under the Extensions sidebar): Python v2021.10.1317843341
  • OS and version: Windows_NT x64 10.0.19043
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8.0
  • Type of virtual environment used: N/A
  • Value of the python.languageServer setting: Pylance

Expected behaviour

Non-fatal test discovery.

Actual behaviour

Runtime error causes test discovery to fail.

Steps to reproduce:

@pytest.mark.parametrize("test_input,expected",
    [
		# Some tests,
        ("spaces](www", "spaces](www"),
		# Some other tests,
    ]
)
def test_remove_markdown_links(test_input, expected):
    # My implementation

Commenting out the above test case removes the symptoms of the bug.

Logs

The Python extension has run into an unexpected situation
while processing a pytest node during test discovery.  Please
Please open an issue at:
  https://github.com/microsoft/vscode-python/issues
and paste the following output there.

nodeid: ./deployment/tests/test_text_helper.py::test_remove_markdown_links[spaces](www-spaces](www]

traceback:
  File "/home/eric/.vscode-server/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/run_adapter.py", line 22, in <module>
    main(tool, cmd, subargs, toolargs)
  File "/home/eric/.vscode-server/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/__main__.py", line 100, in main
    parents, result = run(toolargs, **subargs)
  File "/home/eric/.vscode-server/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 30, in discover
    ec = _pytest_main(pytestargs, [_plugin])
  File "/home/eric/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 162, in main
    ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
  File "/home/eric/.local/lib/python3.8/site-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/home/eric/.local/lib/python3.8/site-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/home/eric/.local/lib/python3.8/site-packages/pluggy/manager.py", line 84, in <lambda>
    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
  File "/home/eric/.local/lib/python3.8/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/home/eric/.local/lib/python3.8/site-packages/_pytest/main.py", line 316, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "/home/eric/.local/lib/python3.8/site-packages/_pytest/main.py", line 269, in wrap_session
    session.exitstatus = doit(config, session) or 0
  File "/home/eric/.local/lib/python3.8/site-packages/_pytest/main.py", line 322, in _main
    config.hook.pytest_collection(session=session)
  File "/home/eric/.local/lib/python3.8/site-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/home/eric/.local/lib/python3.8/site-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/home/eric/.local/lib/python3.8/site-packages/pluggy/manager.py", line 84, in <lambda>
    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
  File "/home/eric/.local/lib/python3.8/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/home/eric/.local/lib/python3.8/site-packages/_pytest/main.py", line 333, in pytest_collection
    session.perform_collect()
  File "/home/eric/.local/lib/python3.8/site-packages/_pytest/main.py", line 641, in perform_collect
    hook.pytest_collection_finish(session=self)
  File "/home/eric/.local/lib/python3.8/site-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/home/eric/.local/lib/python3.8/site-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/home/eric/.local/lib/python3.8/site-packages/pluggy/manager.py", line 84, in <lambda>
    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
  File "/home/eric/.local/lib/python3.8/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/home/eric/.vscode-server/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 107, in pytest_collection_finish
    test, parents = self.parse_item(item)
  File "/home/eric/.vscode-server/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 77, in parse_item
    return parse_item(item)
  File "/home/eric/.vscode-server/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 161, in parse_item
    (nodeid, parents, fileid, testfunc, parameterized) = _parse_node_id(
  File "/home/eric/.vscode-server/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 144, in <lambda>
    _parse_node_id=(lambda *a: _parse_node_id(*a)),
  File "/home/eric/.vscode-server/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 382, in _parse_node_id
    testid, name, kind = next(nodes)
  File "/home/eric/.vscode-server/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 479, in _iter_nodes
    raise should_never_reach_here(
  File "/home/eric/.vscode-server/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 132, in should_never_reach_here
    traceback.print_stack()

@ericmclachlan ericmclachlan added bug Issue identified by VS Code Team member as probable bug triage-needed Needs assignment to the proper sub-team labels Oct 9, 2021
@SteggyLeggy
Copy link

SteggyLeggy commented Oct 11, 2021

Same issue here, just started happening for me with an existing project using this parametrize decorator.

@pytest.mark.parametrize("invalid_char", [punct for punct in string.punctuation if punct not in "-_"])

Have recreated in a new folder, using a much simpler test case

import pytest

@pytest.mark.parametrize("invalid_char", ["]"])
def test_pytest_fixture(invalid_char):
    assert invalid_char == "]"

Like I said test discovery was working last week, and I haven't updated pytest since then. Looking at the code in _find_left_bracket in _pytest_item.py it looks like it would of never been able to handle a parameter that contains a single ] so I've no idea why it suddenly stopped work (or how it was working ok before).

The Python extension has run into an unexpected situation
while processing a pytest node during test discovery.  Please
Please open an issue at:
  https://github.com/microsoft/vscode-python/issues
and paste the following output there.

nodeid: ./test_py.py::test_pytest_fixture[]]

traceback:
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/run_adapter.py", line 22, in <module>
    main(tool, cmd, subargs, toolargs)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/__main__.py", line 100, in main
    parents, result = run(toolargs, **subargs)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 30, in discover
    ec = _pytest_main(pytestargs, [_plugin])
  File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 162, in main
    ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 316, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 269, in wrap_session
    session.exitstatus = doit(config, session) or 0
  File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 322, in _main
    config.hook.pytest_collection(session=session)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 333, in pytest_collection
    session.perform_collect()
  File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 637, in perform_collect
    hook.pytest_collection_modifyitems(
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 92, in pytest_collection_modifyitems
    test, parents = self.parse_item(item)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 77, in parse_item
    return parse_item(item)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 161, in parse_item
    (nodeid, parents, fileid, testfunc, parameterized) = _parse_node_id(
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 144, in <lambda>
    _parse_node_id=(lambda *a: _parse_node_id(*a)),
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 382, in _parse_node_id
    testid, name, kind = next(nodes)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 479, in _iter_nodes
    raise should_never_reach_here(
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 132, in should_never_reach_here
    traceback.print_stack()
collected 1 item

<Module test_py.py>
  <Function test_pytest_fixture[]]>
The Python extension has run into an unexpected situation
while processing a pytest node during test discovery.  Please
Please open an issue at:
  https://github.com/microsoft/vscode-python/issues
and paste the following output there.

nodeid: ./test_py.py::test_pytest_fixture[]]

traceback:
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/run_adapter.py", line 22, in <module>
    main(tool, cmd, subargs, toolargs)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/__main__.py", line 100, in main
    parents, result = run(toolargs, **subargs)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 30, in discover
    ec = _pytest_main(pytestargs, [_plugin])
  File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 162, in main
    ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 316, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 269, in wrap_session
    session.exitstatus = doit(config, session) or 0
  File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 322, in _main
    config.hook.pytest_collection(session=session)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 333, in pytest_collection
    session.perform_collect()
  File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 641, in perform_collect
    hook.pytest_collection_finish(session=self)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 107, in pytest_collection_finish
    test, parents = self.parse_item(item)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 77, in parse_item
    return parse_item(item)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 161, in parse_item
    (nodeid, parents, fileid, testfunc, parameterized) = _parse_node_id(
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 144, in <lambda>
    _parse_node_id=(lambda *a: _parse_node_id(*a)),
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 382, in _parse_node_id
    testid, name, kind = next(nodes)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 479, in _iter_nodes
    raise should_never_reach_here(
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 132, in should_never_reach_here
    traceback.print_stack()
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 637, in perform_collect
INTERNALERROR>     hook.pytest_collection_modifyitems(
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 92, in pytest_collection_modifyitems
INTERNALERROR>     test, parents = self.parse_item(item)
INTERNALERROR>   File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 77, in parse_item
INTERNALERROR>     return parse_item(item)
INTERNALERROR>   File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 161, in parse_item
INTERNALERROR>     (nodeid, parents, fileid, testfunc, parameterized) = _parse_node_id(
INTERNALERROR>   File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 144, in <lambda>
INTERNALERROR>     _parse_node_id=(lambda *a: _parse_node_id(*a)),
INTERNALERROR>   File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 382, in _parse_node_id
INTERNALERROR>     testid, name, kind = next(nodes)
INTERNALERROR>   File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 479, in _iter_nodes
INTERNALERROR>     raise should_never_reach_here(
INTERNALERROR> NotImplementedError: Unexpected pytest node (see printed output).
INTERNALERROR> 
INTERNALERROR> During handling of the above exception, another exception occurred:
INTERNALERROR> 
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 269, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 322, in _main
INTERNALERROR>     config.hook.pytest_collection(session=session)
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 333, in pytest_collection
INTERNALERROR>     session.perform_collect()
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/_pytest/main.py", line 641, in perform_collect
INTERNALERROR>     hook.pytest_collection_finish(session=self)
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/home/steve2/.local/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 107, in pytest_collection_finish
INTERNALERROR>     test, parents = self.parse_item(item)
INTERNALERROR>   File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 77, in parse_item
INTERNALERROR>     return parse_item(item)
INTERNALERROR>   File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 161, in parse_item
INTERNALERROR>     (nodeid, parents, fileid, testfunc, parameterized) = _parse_node_id(
INTERNALERROR>   File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 144, in <lambda>
INTERNALERROR>     _parse_node_id=(lambda *a: _parse_node_id(*a)),
INTERNALERROR>   File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 382, in _parse_node_id
INTERNALERROR>     testid, name, kind = next(nodes)
INTERNALERROR>   File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py", line 479, in _iter_nodes
INTERNALERROR>     raise should_never_reach_here(
INTERNALERROR> NotImplementedError: Unexpected pytest node (see printed output).

========================== 1 test collected in 0.00s ===========================

Traceback (most recent call last):
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/run_adapter.py", line 22, in <module>
    main(tool, cmd, subargs, toolargs)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/__main__.py", line 100, in main
    parents, result = run(toolargs, **subargs)
  File "/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 44, in discover
    raise Exception("pytest discovery failed (exit code {})".format(ec))
Exception: pytest discovery failed (exit code 3)

	at ChildProcess.<anonymous> (/home/steve2/.vscode/extensions/ms-python.python-2021.10.1317843341/out/client/extension.js:9:358475)
	at Object.onceWrapper (events.js:422:26)
	at ChildProcess.emit (events.js:315:20)
	at maybeClose (internal/child_process.js:1048:16)
	at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)]

EDIT:
Just downgraded to version 2021.9.1246542782 of ms-python.python (I had to download the vsix file and install via the command line) but that has fixed the issue so far.

@karthiknadig karthiknadig self-assigned this Oct 11, 2021
@karthiknadig karthiknadig added triage area-testing and removed triage-needed Needs assignment to the proper sub-team labels Oct 11, 2021
@mjpieters
Copy link

The relevant commit is a68f98f, which is PR #17357.

@mjpieters
Copy link

mjpieters commented Oct 13, 2021

This unfortunately caused a lot of breakage in projects that have an 'unbalanced' ] character in a parameter. The assumption that brackets are always balanced is not correct here, I fear!

Instead, shouldn't the code not just use the provided Node and Function interfaces to 'parse' the name? I know that currently _parse_node_id() is not passed the original item, only item.nodeid, but item has enough information to avoid this whole issue.

E.g., given:

$ pwd
/tmp/mvce
$ cat 'tests/folder-[2]/test_foo.py'
import pytest

@pytest.mark.parametrize("bar", [("]",)])
def test_foo(bar):
    pass
$ python -m pdb ~/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/run_adapter.py discover pytest --no-hide-stdio
# ... PDB session started ...

then when I inspect item inside testing_tools/adapter/pytest/_pytest_item.py:parse_item() you can see it has all the information that _parse_node_id() tries to extract as rich Python object data:

(Pdb) item.nodeid
'tests/folder-[2]/test_foo.py::test_foo[]]'
(Pdb) item.name  # Node API
'test_foo[]]'
(Pdb) item.originalname  # Function API
'test_foo'
(Pdb) item.fspath
local('/private/tmp/mcve/tests/folder-[2]/test_foo.py')
(Pdb) item.fspath.parts()
[local('/'), local('/private'), local('/private/tmp'), local('/private/tmp/mcve'), local('/private/tmp/mcve/tests'), local('/private/tmp/mcve/tests/folder-[2]'), local('/private/tmp/mcve/tests/folder-[2]/test_foo.py')]

I also note that there is also a helpful _pytest.nodes.iterparentnodeids() function:

(Pdb) from _pytest.nodes import iterparentnodeids
(Pdb) print(iterparentnodeids.__doc__)
Return the parent node IDs of a given node ID, inclusive.

    For the node ID

        "testing/code/test_excinfo.py::TestFormattedExcinfo::test_repr_source"

    the result would be

        ""
        "testing"
        "testing/code"
        "testing/code/test_excinfo.py"
        "testing/code/test_excinfo.py::TestFormattedExcinfo"
        "testing/code/test_excinfo.py::TestFormattedExcinfo::test_repr_source"

    Note that :: parts are only considered at the last / component.

(Pdb) pp list(iterparentnodeids(item.nodeid))
['',
 'tests',
 'tests/folder-[2]',
 'tests/folder-[2]/test_foo.py',
 'tests/folder-[2]/test_foo.py::test_foo[]]']

If the expected output for _iter_nodes is:

[('./tests/folder-[2]/test_foo.py::test_foo[]]', '[]]', 'subtest'),
 ('./tests/folder-[2]/test_foo.py::test_foo', 'test_foo', 'function'),
 ('./tests/folder-[2]/test_foo.py', 'test_foo.py', 'file'),
 ('./tests/folder-[2]', 'folder-[2]', 'folder'),
 ('./tests', 'tests', 'folder'),
 ('.', None, 'folder')]

then using .nodeid alone is going to make this way, way too hard. Instead, the first 2 values can already nicely approximated with:

(Pdb) f'{item.parent.nodeid}::{item.originalname}', item.name[len(item.originalname):], 'subtest'
('tests/folder-[2]/test_foo.py::test_foo', '[]]', 'subtest')
(Pdb) item.parent.nodeid, item.originalname, 'function'
('tests/folder-[2]/test_foo.py', 'test_foo', 'function')

Then, perhaps use item.iterchain()[0].fspath as the basis for constructing the remaining elements from the item.fspath.parts() list (see the py.path documentation for the LocalPath() class API).

@mjpieters
Copy link

mjpieters commented Oct 13, 2021

I fixed this locally by replacing lines 161-163 in testing_tools/adapter/pytest/_pytest_item.py, with:

    if kind == "function" and item.nodeid[-1:] == "]":
        # work around microsoft/vscode-python#17676
        parameterized = item.name[len(item.originalname) :]
        (parentid, parents, fileid, testfunc, _) = _parse_node_id(
            item.nodeid[: -len(parameterized)], kind
        )
        nodeid = f"{parentid}{parameterized}"
        parents = [(parentid, item.originalname, kind), *parents]
    else:
        (nodeid, parents, fileid, testfunc, parameterized) = _parse_node_id(
            item.nodeid, kind
        )

On your local system, that's ~/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py

So, if the current item is a function and has been parametrized (sic, pytest's misspelling), then it parses the item without the parameters, to then add back that information based on the parsed result. This avoids having to fully rework the way the rest of the nodeid elements are parsed and normalized.

@karthiknadig
Copy link
Member

@mjpieters Can you make a PR with this change?

@mjpieters
Copy link

@mjpieters Can you make a PR with this change?

I was thinking of that change as an ugly-ish work-around, not the full solution, to be honest.

I'd be happy to turn it into a PR of course, it's your tech debt to pay later ;-)

@karthiknadig
Copy link
Member

@mjpieters We have plans replacing this test adapter entirely, there are several issues with the current test adapter. This will be a temporary solution for this issue.

@karthiknadig karthiknadig removed their assignment Oct 27, 2021
@karthiknadig karthiknadig changed the title should_never_reach_here for particular test case should_never_reach_here for particular test case when parametrized test has unmatched brackets or parenthesis Oct 28, 2021
mjpieters added a commit to mjpieters/vscode-python that referenced this issue Oct 30, 2021
Rather than try and parse out the parametrized portion of the nodeid (delimited by square brackets but possibly containing square brackets), use native [pytest item attributes](https://docs.pytest.org/en/6.2.x/reference.html#function) to separate out the decoration.

Better solution for microsoft#17357, fixing microsoft#17676.
mjpieters added a commit to mjpieters/vscode-python that referenced this issue Oct 31, 2021
Rather than try and parse out the parametrized portion of the nodeid (delimited by square brackets but possibly containing square brackets), use native [pytest item attributes](https://docs.pytest.org/en/6.2.x/reference.html#function) to separate out the decoration.

Better solution for microsoft#17357, fixing microsoft#17676.
mjpieters added a commit to mjpieters/vscode-python that referenced this issue Oct 31, 2021
Rather than try and parse out the parametrized portion of the nodeid (delimited by square brackets but possibly containing square brackets), use native [pytest item attributes](https://docs.pytest.org/en/6.2.x/reference.html#function) to separate out the decoration.

Better solution for microsoft#17357, fixing microsoft#17676.
@The-Compiler
Copy link

The-Compiler commented Nov 4, 2021

As a workaround, you can use pytest.param to override the node ID for the problematic test (reference):

import pytest

@pytest.mark.parametrize("invalid_char", [
    pytest.param("]", id='closing-bracket'),
])
def test_pytest_fixture(invalid_char):
    assert invalid_char == "]"

also, FWIW:

parametrized (sic, pytest's misspelling)

Not a misspelling - English just happens to have four valid spellings of that word. pytest-parawtf to the rescue! (no, please don't use it in production...)

karthiknadig added a commit that referenced this issue Nov 8, 2021
* Use pytest data to extract the parametrised decoration

Rather than try and parse out the parametrized portion of the nodeid (delimited by square brackets but possibly containing square brackets), use native [pytest item attributes](https://docs.pytest.org/en/6.2.x/reference.html#function) to separate out the decoration.

Better solution for #17357, fixing #17676.

* Add news entry

* Update news/2 Fixes/17676.md

Co-authored-by: Karthik Nadig <kanadig@microsoft.com>

Co-authored-by: Karthik Nadig <kanadig@microsoft.com>
mkonig pushed a commit to mkonig/qutebrowser that referenced this issue Dec 7, 2021
This works around a VS Code bug with weird node IDs:
microsoft/vscode-python#17676
twigleingrid pushed a commit to twigleingrid/qutebrowser that referenced this issue May 13, 2022
This works around a VS Code bug with weird node IDs:
microsoft/vscode-python#17676
@github-actions github-actions bot removed the needs PR label Aug 9, 2022
@karrtikr karrtikr added the needs PR Ready to be worked on label Aug 9, 2022
@eleanorjboyd
Copy link
Member

Hello! We have just finished our testing rewrite and are beginning the roll out to users. If you are able, it would be very helpful to know if your issue still exists on the rewrite! To try it yourself, add this setting to your users settings.json "python.experiments.optInto": ["pythonTestAdapter"].

You can confirm you have the rewrite enabled by setting "python.analysis.logLevel": "Trace", in your user settings then check for Experiment 'pythonTestAdapter' is active in your python logs.

Let me know if the rewrite fixes your issue. Thanks!

@eleanorjboyd eleanorjboyd self-assigned this Sep 19, 2023
@eleanorjboyd eleanorjboyd added info-needed Issue requires more information from poster ghc-osd and removed needs PR Ready to be worked on labels Sep 19, 2023
@github-actions
Copy link

Because we have not heard back with the information we requested, we are closing this issue for now. If you are able to provide the info later on, then we will be happy to re-open this issue to pick up where we left off.

Happy Coding!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 20, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-testing bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster
Projects
None yet
Development

No branches or pull requests

7 participants