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

"Consistent AST position" crashes pytest: "ValueError: line 22-0 is not a valid range" #93387

Closed
nedbat opened this issue May 31, 2022 · 6 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@nedbat
Copy link
Member

nedbat commented May 31, 2022

Bug report

The coverage.py test suite fails to even start on 3.11.0b2, because of a crash inside pytest related to code positions. This is the error from a nightly build:

py311 run-test: commands[3] | python igor.py test_with_tracer c -rfsEX
  === CPython 3.11.0b1+ with C tracer (.tox/py311/bin/python) ===
  Traceback (most recent call last):
    File "/home/runner/work/coveragepy/coveragepy/igor.py", line 474, in <module>
      sys.exit(main(sys.argv[1:]))
               ^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/igor.py", line 466, in main
      ret = handler(*handler_args)
            ^^^^^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/igor.py", line 239, in do_test_with_tracer
      return run_tests(tracer, *runner_args)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/igor.py", line 152, in run_tests
      return pytest.main(list(runner_args))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/_pytest/config/__init__.py", line 145, in main
      config = _prepareconfig(args, plugins)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/_pytest/config/__init__.py", line 324, in _prepareconfig
      config = pluginmanager.hook.pytest_cmdline_parse(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/pluggy/_hooks.py", line 265, in __call__
      return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/pluggy/_manager.py", line 80, in _hookexec
      return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/pluggy/_callers.py", line 55, in _multicall
      gen.send(outcome)
      ^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/_pytest/helpconfig.py", line 102, in pytest_cmdline_parse
      config: Config = outcome.get_result()
                       ^^^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/pluggy/_result.py", line 60, in get_result
      raise ex[1].with_traceback(ex[2])
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/pluggy/_callers.py", line 39, in _multicall
      res = hook_impl.function(*args)
            ^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/_pytest/config/__init__.py", line 1016, in pytest_cmdline_parse
      self.parse(args)
      ^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/_pytest/config/__init__.py", line 1304, in parse
      self._preparse(args, addopts=addopts)
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/_pytest/config/__init__.py", line 1187, in _preparse
      self.pluginmanager.load_setuptools_entrypoints("pytest11")
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/pluggy/_manager.py", line 287, in load_setuptools_entrypoints
      plugin = ep.load()
               ^^^^^^^^^
    File "/usr/lib/python3.11/importlib/metadata/__init__.py", line 198, in load
      module = import_module(match.group('module'))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
    File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
    File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/_pytest/assertion/rewrite.py", line 159, in exec_module
      source_stat, co = _rewrite_test(fn, self.config)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/runner/work/coveragepy/coveragepy/.tox/py311/lib/python3.11/site-packages/_pytest/assertion/rewrite.py", line 361, in _rewrite_test
      co = compile(tree, strfn, "exec", dont_inherit=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ValueError: line 22-0 is not a valid range

When I run the test suite locally with 3.11.0b2, I get the same traceback, but the message is "line 1-0 is not a valid range".

Running pytest directly, rather than through my helper doesn't fail.

I git bisected: the first bad commit was:

7f6e6abdc43d9b61d81eb26b9fb2073dccf6a3cb is the first bad commit
commit 7f6e6abdc43d9b61d81eb26b9fb2073dccf6a3cb
Author: Miss Islington (bot) <[email protected]>
Date:   Mon May 30 11:52:36 2022 -0700

    gh-93351: Ensure the position information in AST nodes created by the parser is always consistent (GH-93352)

    (cherry picked from commit 5893b5db98b38b17750c0572c7209774a5034898)

    Co-authored-by: Pablo Galindo Salgado <[email protected]>

 Lib/test/test_ast.py | 27 +++++++++++++++++++++++++++
 Python/ast.c         | 26 ++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

Your environment

  • CPython versions tested on: 3.11.0b1 works, 3.11.0b2 fails.
  • Operating system and architecture: Mac OS 10.15.7

To reproduce

$ mktmpenv -p python3.10    # or some other way to create an environment
$ git clone https://github.com/nedbat/coveragepy
$ cd coveragepy
$ pip install tox
$ tox -e py311
@nedbat nedbat added the type-bug An unexpected behavior, bug, or error label May 31, 2022
@nedbat
Copy link
Member Author

nedbat commented May 31, 2022

@pablogsal ^^

@JelleZijlstra
Copy link
Member

Cross-reference #92597

@nedbat
Copy link
Member Author

nedbat commented May 31, 2022

BTW, latest tip of CPython (07df8d5) also fails in this way.

@pablogsal
Copy link
Member

This is expected, and it means that something is creating and compiling AST nodes with invalid position information. In this case the end line number is lower than the start line number. This can crash the compiler so we reject it now.

@pablogsal
Copy link
Member

pablogsal commented May 31, 2022

Unfortunately seems that #93359 didn't make it to beta2 so you likely need that fix so AST nodes created with line number but no end line number still work as before.

@pablogsal
Copy link
Member

I'm closing this as this is a duplicate of #92597

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants