Poetry: Poetry messes up pyproject.toml when [build-system] is not at the end (with UnexpectedCharError)

Created on 13 Aug 2020  路  2Comments  路  Source: python-poetry/poetry

  • [x] I am on the latest Poetry version.
  • [x] I have searched the issues of this repo and believe that this is not a duplicate.
  • [x] If an exception occurs when executing a command, I executed it again in debug mode (-vvv option).

Issue

When I run poetry add -D pytest-mock -vvv on the pyproject.toml file linked here above, Poetry gives me the following error stacktrace:

$ poetry add -D pytest-mock -vvv
Using virtualenv: C:\projects\code_sandbox\sandbox_python\hypermodern_python\.venv
PyPI: 45 packages found for pytest-mock *
Using version ^3.2.0 for pytest-mock


[UnexpectedCharError]
Unexpected character: '[' at line 34 col 19

Traceback (most recent call last):
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\clikit\console_application.py", line 131, in run
    status_code = command.handle(parsed_args, io)
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\clikit\api\command\command.py", line 120, in handle
    status_code = self._do_handle(args, io)
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\clikit\api\command\command.py", line 171, in _do_handle
    return getattr(handler, handler_method)(args, io, self)
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\cleo\commands\command.py", line 92, in wrap_handle
    return self.handle()
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\poetry\console\commands\add.py", line 148, in handle
    self.io, self.env, self.poetry.package, self.poetry.locker, self.poetry.pool
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\poetry\console\commands\command.py", line 10, in poetry
    return self.application.poetry
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\poetry\console\application.py", line 49, in poetry
    self._poetry = Factory().create_poetry(Path.cwd())
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\poetry\factory.py", line 40, in create_poetry
    local_config = TomlFile(poetry_file.as_posix()).read()
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\tomlkit\toml_file.py", line 20, in read
    return loads(f.read())
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\tomlkit\api.py", line 34, in loads
    return parse(string)
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\tomlkit\api.py", line 51, in parse
    return Parser(string).parse()
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\tomlkit\parser.py", line 153, in parse
    key, value = self._parse_table()
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\tomlkit\parser.py", line 1056, in _parse_table
    item = self._parse_item()
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\tomlkit\parser.py", line 302, in _parse_item
    return self._parse_key_value(True)
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\tomlkit\parser.py", line 391, in _parse_key_value
    cws, comment, trail = self._parse_comment_trail()
  File "c:\users\laure\.local\pipx\venvs\poetry\lib\site-packages\tomlkit\parser.py", line 339, in _parse_comment_trail
    raise self.parse_error(UnexpectedCharError, c)

and gives me a new pyproject.toml which is invalid:

[tool.poetry]
name = "c2ba_hypermodern_python"
version = "0.1.0"
description = "Project built by going through Hypermodern Python tutorial"
homepage = "https://gitlab.com/c2ba_sandbox/sandbox_python/hypermodern_python"
repository = "https://gitlab.com/c2ba_sandbox/sandbox_python/"
authors = ["Celeborn2BeAlive <[email protected]>"]
keywords = ["hypermodern", "python", "tutorial"]

[tool.poetry.dependencies]
python = "^3.7"
typer = "^0.3.1"
requests = "^2.24.0"
colorama = "^0.4.3"

[tool.poetry.dev-dependencies]
black = "^19.10b0"
pytest = "^6.0.1"
coverage = {extras = ["toml"], version = "^5.2.1"}
pytest-cov = "^2.10.0"
pytest-mock = "^3.2.0"

[tool.poetry.scripts]
hypermodern-python = "c2ba_hypermodern_python.console:app"

[tool.coverage.paths]
source = ["src", "*/site-packages"]

[tool.coverage.run]
branch = true
source = ["c2ba_hypermodern_python"]

[tool.coverage.report]
show_missing = true[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

So it seems it tries to rewrite the [build-system] section at the end but fails to do it properly.

If I put the [build-system] section at the end before running poetry add, it works correctly.

I don't know if the specification of the pyproject.toml file impose this section to be at the end. If it does then you can close this issue (in that case maybe poetry should warn the user that the section should be at the end ?)

If the specification says nothing about that, then poetry should work on this case, and I think it should let the [build-system] section at the same position in the file, not try to put it at the end.

Thanks.

Bug

All 2 comments

Looks to be a duplicate of #2782

As @abn mentioned, the problem resolved in latest pre release(1.1.0b2.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jhrmnn picture jhrmnn  路  3Comments

nikaro picture nikaro  路  3Comments

jackemuk picture jackemuk  路  3Comments

sobolevn picture sobolevn  路  3Comments

mozartilize picture mozartilize  路  3Comments