Search code examples
python-3.xpython-poetry

Python Poetry's default test not working for me


I want to try out poetry, but the initial setup/test isn't working for me, so I'm a little confused. I believe it should 'just work', so I guess I'm missing some simple step?

I'm on macOS: 12.3 (21E230) I got the latest version installed:

$ poetry --version
Poetry version 1.1.13

Now I just want a pure vanilla setup, and verify the basics work:

$ poetry new poetry_demo --src
Created package poetry_demo in poetry_demo
$ cd poetry_demo/
$ poetry install
Creating virtualenv poetry-demo in /Users/barryredmond/dev/git/poetry_demo/.venv
Updating dependencies
Resolving dependencies... (0.1s)

Writing lock file

Package operations: 8 installs, 0 updates, 0 removals

  • Installing pyparsing (3.0.8)
  • Installing attrs (21.4.0)
  • Installing more-itertools (8.13.0)
  • Installing packaging (21.3)
  • Installing pluggy (0.13.1)
  • Installing py (1.11.0)
  • Installing wcwidth (0.2.5)
  • Installing pytest (5.4.3)

Installing the current project: poetry_demo (0.1.0)

$ poetry check
All set!

I think that should get me set up? Lets run the built in test:

$ poetry run pytest
================================================================================================ test session starts     =================================================================================================
platform darwin -- Python 3.10.4, pytest-5.4.3, py-1.11.0, pluggy-0.13.1
rootdir: /Users/barryredmond/dev/git/poetry_demo
collected 0 items / 1 error

======================================================================================================= ERRORS =======================================================================================================
_____________________________________________________________________________________     ERROR collecting tests/test_poetry_demo.py     _____________________________________________________________________________________
.venv/lib/python3.10/site-packages/py/_path/local.py:704: in pyimport
    __import__(modname)
<frozen importlib._bootstrap>:1027: in _find_and_load
    ???
<frozen importlib._bootstrap>:1006: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:688: in _load_unlocked
    ???
.venv/lib/python3.10/site-packages/_pytest/assertion/rewrite.py:143: in exec_module
    source_stat, co = _rewrite_test(fn, self.config)
.venv/lib/python3.10/site-packages/_pytest/assertion/rewrite.py:330: in _rewrite_test
    co = compile(tree, fn, "exec", dont_inherit=True)
E   TypeError: required field "lineno" missing from alias
============================================================================================== short test summary info ===============================================================================================
ERROR tests/test_poetry_demo.py - TypeError: required field "lineno" missing from alias
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================================================== 1 error in 0.05s     ==================================================================================================

And just to verify that the setup looks about right for anyone looking at this:

$ tree
.
├── README.rst
├── poetry.lock
├── pyproject.toml
├── src
│   └── poetry_demo
│       └── __init__.py
└── tests
    ├── __init__.py
    ├── __pycache__
    │   └── __init__.cpython-310.pyc
    └── test_poetry_demo.py

4 directories, 7 files

.

$ cat tests/test_poetry_demo.py
from poetry_demo import __version__


def test_version():
    assert __version__ == '0.1.0'

.

$ cat src/poetry_demo/__init__.py
__version__ = '0.1.0'

I did also try it without the --src but it's the same.


Solution

  • The pytest version that poetry includes at the moment when using poetry new is outdated and doesn't work with python3.10. You have to update pytest with poetry add --dev pytest@latest.

    From poetry 1.2 on, poetry will no longer define any default dependencies when initialize a project via poetry new.