poetry icon indicating copy to clipboard operation
poetry copied to clipboard

`poetry run` fails with a keyerror for poetry 2.0.0

Open bjoernricks opened this issue 1 year ago • 7 comments

Description

With the latest poetry release 2.0.0 calling a script via poetry run always fails with a KeyError.

  KeyError

  'format'

  at ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/core/masonry/utils/module.py:79 in __init__
       75│             self._package_includes.append(
       76│                 PackageInclude(
       77│                     self._path,
       78│                     package["include"],
    →  79│                     formats=package["format"],
       80│                     source=package.get("from"),
       81│                     target=package.get("to"),
       82│                 )
       83│             )

Steps to reproduce.

  • Install poetry via pipx
  • Create a temporary directory
  • Change into the temporary directory
  • Copy the following pyproject.toml into that new directory
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "tmp"
version = "0.1.0"
description = ""
authors = ["Foo Bar <[email protected]>"]
readme = "README.md"

packages = [{ include = "somepackage" }]

[tool.poetry.scripts]
sometool = "somepackage:main"
  • Create a someproject/__init__.py file in that temporary directory with the following content
def main():
    print("Hello from somepackage")
  • Call poetry run sometool

Workarounds

None

Poetry Installation Method

pipx

Operating System

Ubuntu 24.04

Poetry Version

Poetry (version 2.0.0)

Poetry Configuration

cache-dir = "/home/bricks/.cache/pypoetry"
installer.max-workers = null
installer.no-binary = null
installer.only-binary = null
installer.parallel = true
installer.re-resolve = true
keyring.enabled = true
requests.max-retries = 0
solver.lazy-wheel = true
system-git-client = false
virtualenvs.create = true
virtualenvs.in-project = true
virtualenvs.options.always-copy = false
virtualenvs.options.no-pip = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = "{cache-dir}/virtualenvs"  # /home/bricks/.cache/pypoetry/virtualenvs
virtualenvs.prompt = "{project_name}-py{python_version}"
virtualenvs.use-poetry-python = false


### Python Sysconfig

_No response_

### Example pyproject.toml

```TOML
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "tmp"
version = "0.1.0"
description = ""
authors = ["Foo Bar <[email protected]>"]
readme = "README.md"

packages = [{ include = "somepackage" }]

[tool.poetry.scripts]
sometool = "somepackage:main"

Poetry Runtime Logs

poetry run -vvv sometool

  KeyError

  'format'

  at ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/core/masonry/utils/module.py:79 in __init__
       75│             self._package_includes.append(
       76│                 PackageInclude(
       77│                     self._path,
       78│                     package["include"],
    →  79│                     formats=package["format"],
       80│                     source=package.get("from"),
       81│                     target=package.get("to"),
       82│                 )

bjoernricks avatar Jan 06 '25 11:01 bjoernricks

Thanks for reporting this @bjoernricks. I can confirm this issue.

This was introduced in poetry-core with https://github.com/python-poetry/poetry-core/pull/773. @radoering can we simply change https://github.com/python-poetry/poetry-core/pull/773/files#diff-62e4eae7349a9699c84076b1a65f0371d26b188e396dcee122356110960adeffR78 to package.get("format")? I mean it works, but I'm not sure if we kill something else. (Tests are still running fine).

Edit: Hm, I think we also need to reintroduce this lines:

formats = package.get("format")
if formats and not isinstance(formats, list):
    formats = [formats]

finswimmer avatar Jan 06 '25 12:01 finswimmer

Hi @bjoernricks, even I faced this issue, for now a quick workaround would be adding format in the packages packages = [{ include = "somepackage", format = ["sdist"] }]

Also it's mentioned in the poetry documentation here that If no format is specified, include defaults to only sdist. image

I think this handling needs to be added where if format is not specified it should default to sdist

anuragnatoo avatar Jan 06 '25 15:01 anuragnatoo

I don't know too much about poetry but I think it would be nice to trying to minimize problems on stable versions. One option other than add more checks on pytest would be to do a dev/pre release on pypi repository before moving it to a stable release on major version changes, so that the community can try it before going directly to stable https://pypi.org/project/poetry/#history

https://peps.python.org/pep-0440/#developmental-releases

aliceinwire avatar Jan 08 '25 06:01 aliceinwire

Hi @bjoernricks, even I faced this issue, for now a quick workaround would be adding format in the packages packages = [{ include = "somepackage", format = ["sdist"] }]

Also it's mentioned in the poetry documentation here that If no format is specified, include defaults to only sdist. image

I think this handling needs to be added where if format is not specified it should default to sdist

I think defaulting to sdist is true only for 2.0.0 on 1.8.5 doing poetry build without any format option gives

$ poetry build
Building kci-dev (0.1.1)
  - Building sdist
  - Built kci_dev-0.1.1.tar.gz
  - Building wheel
  - Built kci_dev-0.1.1-py3-none-any.whl

with 2.0.0 with sdist only format gives

$ poetry build
Building kci-dev (0.1.1)
  - Building sdist
  - Built kci_dev-0.1.1.tar.gz
  - Building wheel

No file/folder found for package kci-dev

aliceinwire avatar Jan 08 '25 06:01 aliceinwire

I suppose for packages include the default is [wheel, sdist].

bjoernricks avatar Jan 08 '25 07:01 bjoernricks

Dear all

I have the same issue and was quite perplex when greeted with a red 'format' message (there might seem to potential in improving the masonry logging in case of errors, as the exception stack was not printed on Windows). I searched quite some time in my own code before running poetry run --verbose my_script 🤦)

I have now added the appropriate key format to all my packages:

[tool.poetry]
packages = [
    { include = "package_a" },
    { include = "package_b" },
]

should now be:

[tool.poetry]
packages = [
    { include = "package_a", format = [
        "sdist",
        "wheel",
    ] },
    { include = "package_b", format = [
        "sdist",
        "wheel",
    ] },
]

Prior to the migration to Poetry 2.0, my package used to build to wheel by default in Poetry 1.8 and I depended upon this (maybe others as well). To not break compatibility I guess falling back to ["sdist", "wheel"] would be more appropriate than just ["sdist"].

What is/was the motivation behind the change to only build sdist? The current Poetry 2.0 release still uses sdist and wheel for the default package if not specified (see https://github.com/python-poetry/poetry-core/blame/5e5ae745d2f236f0758915b86f20d5638dad2d37/src/poetry/core/masonry/utils/module.py#L71)

Best regards Markus

Atokulus avatar Jan 08 '25 15:01 Atokulus

Faced the same issue here, which only happens if my project name = is different from the include = XX name, and the workaround is to simply specify format = sdist in your include packages, even though sdist is the default, from the documentation.

flipbit03 avatar Jan 08 '25 19:01 flipbit03

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Feb 09 '25 00:02 github-actions[bot]