Repository: python-poetry/poetry

Issue:
AttributeError when adding a dependency group when private repo present (Poetry 1.2.0a2)
<!-- Checked checkbox should look like this: [x] -->
- [ ] I am on the [latest](https://github.com/python-poetry/poetry/releases/latest) Poetry version.
- [x] I have searched the [issues](https://github.com/python-poetry/poetry/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).

Not on latest, because I am on 1.2.0a2, which is newer than latest.

- **OS version and name**: Linux Debian Bullseye in a container
- **Poetry version**: 1.2.0a2
- **pyproject.toml file**:
```
[tool.poetry]
name = "foobar"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.9"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[[tool.poetry.source]]
name = "foo"
url = "https://foo.bar/simple/"
```

## Issue
When using a private repo (i.e. having a `[[tool.poetry.source]]` section to pyproject.toml), and adding a group dependency (e.g. running `poetry add --group test pytest`, e.g. the feature added in #4260) for a group that does not yet exist in the pyproject.toml, poetry raises an exception.

```
# poetry add --group test pytest

  AttributeError

  'dict' object has no attribute '_insert_after'

  at /usr/local/lib/python3.9/site-packagesconsole/commands/add.py:114 in handle
      110│         else:
      111│             if "group" not in poetry_content:
      112│                 group_table = table()
      113│                 group_table._is_super_table = True
    → 114│                 poetry_content.value._insert_after("dependencies", "group", group_table)
      115│
      116│             groups = poetry_content["group"]
      117│             if group not in groups:
      118│                 group_table = parse_toml(
```

Even though the example above has an invalid URL for the poetry source, this error happens even when the URL is valid and the necessary credentials to authenticate are present (e.g. a valid Google Artifact Repository repo), and regardless of whether the source is primary or secondary.

Also note that the dependency being added isn't even hosted inside the private repo. (In this case, trying to add pytest to a group called "test".)

Removing the `[[tool.poetry.source]]` section allows Poetry to properly add the dependency group and dependency.

The above pyproject.toml was generated using the following:
```
docker run -it --rm python:3.9-slim-bullseye bash

# Within the container:
pip install poetry==1.2.0a2
poetry init --name foobar -n
cat >> pyproject.toml <<EOF

[[tool.poetry.source]]
name = "foo"
url = "https://foo.bar/simple/"
EOF
# Then run poetry add --group test pytest to see the error
```

Full output when running with -vvv
```
# poetry -vvv add pytest --group test
Loading configuration file /root/.config/pypoetry/config.toml
Adding repository phc (https://us-central1-python.pkg.dev/foobar/python/simple) and setting it as secondary
Using virtualenv: /venv

  Stack trace:

  7  /usr/local/lib/python3.9/site-packages/cleo/application.py:330 in run
      328│
      329│             try:
    → 330│                 exit_code = self._run(io)
      331│             except Exception as e:
      332│                 if not self._catch_exceptions:

  6  /usr/local/lib/python3.9/site-packages/poetry/console/application.py:180 in _run
      178│         self._load_plugins(io)
      179│
    → 180│         return super()._run(io)
      181│
      182│     def _configure_io(self, io: IO) -> None:

  5  /usr/local/lib/python3.9/site-packages/cleo/application.py:425 in _run
      423│                 io.set_input(ArgvInput(argv))
      424│
    → 425│         exit_code = self._run_command(command, io)
      426│         self._running_command = None
      427│

  4  /usr/local/lib/python3.9/site-packages/cleo/application.py:467 in _run_command
      465│
      466│         if error is not None:
    → 467│             raise error
      468│
      469│         return event.exit_code

  3  /usr/local/lib/python3.9/site-packages/cleo/application.py:451 in _run_command
      449│
      450│             if event.command_should_run():
    → 451│                 exit_code = command.run(io)
      452│             else:
      453│                 exit_code = ConsoleCommandEvent.RETURN_CODE_DISABLED

  2  /usr/local/lib/python3.9/site-packages/cleo/commands/base_command.py:118 in run
      116│         io.input.validate()
      117│
    → 118│         status_code = self.execute(io)
      119│
      120│         if status_code is None:

  1  /usr/local/lib/python3.9/site-packages/cleo/commands/command.py:85 in execute
       83│
       84│         try:
    →  85│             return self.handle()
       86│         except KeyboardInterrupt:
       87│             return 1

  AttributeError

  'dict' object has no attribute '_insert_after'

  at /usr/local/lib/python3.9/site-packages/poetry/console/commands/add.py:114 in handle
      110│         else:
      111│             if "group" not in poetry_content:
      112│                 group_table = table()
      113│                 group_table._is_super_table = True
    → 114│                 poetry_content.value._insert_after("dependencies", "group", group_table)
      115│
      116│             groups = poetry_content["group"]
      117│             if group not in groups:
      118│                 group_table = parse_toml(
```


Discussion:
Did some more investigation. This error only happens if the `[[tool.poetry.source]]` section is the last one in the file. If the order of the clauses is swapped around like so
```
[tool.poetry]
name = "foobar"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.9"

[[tool.poetry.source]]
name = "foo"
url = "https://foo.bar/simple/"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
```
(Notice that `[build-system]` and `[[tool.poetry.source]]` have swapped positions)

I noticed this because I was trying to create the above test case using a different command instead of the append `poetry source add foo http://foo.bar/simple`, which added the source before build-system.
I have the same error, also on version Poetry (version 1.2.0a2) but for me it occurs when references  a local library ie `somelib` that has it's own pyproject.toml

Can be traced back to the update 1.2.0a2 here:

https://github.com/python-poetry/poetry/commit/4b8384cd354ee9127d379cfaea6cd08283ac17d7#diff-8005012c00c842592c28b01560b1f9522146f351b70df58fadfec5c0245bd523

Unsure of the specifics, but it if you don't have a previously toml [tool.poetry.group] (or an tool.poetry.group.whatever.dependencies
)
this blows up:
https://github.com/python-poetry/poetry/blob/4b8384cd354ee9127d379cfaea6cd08283ac17d7/poetry/console/commands/add.py#L105-L114
An error in the same line happened with me when trying to add a new dev dependency to a project that was created with Poetry v1.1.11.

Replacing `[tool.poetry.dev-dependencies]` with `[tool.poetry.group.dev.dependencies]` solved the problem.
I can reproduce quickly this with
```
poetry init -q
echo "\n[tool.poetry.plugins]" >> pyproject.toml
poetry add --group dev pytest
```
on
```
poetry --version
Poetry (version 1.2.0a2)
```
produces
```
Creating virtualenv demo in /private/tmp/demo/.venv

  AttributeError

  'dict' object has no attribute '_insert_after'

  at ~/Library/Application Support/pypoetry/venv/lib/python3.9/site-packages/poetry/console/commands/add.py:114 in handle
      110│         else:
      111│             if "group" not in poetry_content:
      112│                 group_table = table()
      113│                 group_table._is_super_table = True
    → 114│                 poetry_content.value._insert_after("dependencies", "group", group_table)
      115│
      116│             groups = poetry_content["group"]
      117│             if group not in groups:
      118│                 group_table = parse_toml(
```

I confirm that moving the `[build-system]` section at the end solves the problem
> I confirm that moving the [build-system] section at the end solves the problem

Same here on `1.2.0b1`
Root cause is at https://github.com/sdispater/tomlkit/issues/196

Resolving patch:
diff --git a/src/poetry/console/commands/add.py b/src/poetry/console/commands/add.py
--- a/src/poetry/console/commands/add.py
+++ b/src/poetry/console/commands/add.py
@@ -126,9 +126,7 @@ def handle(self) -> int:
             section = poetry_content["dependencies"]
         else:
             if "group" not in poetry_content:
-                poetry_content.value._insert_after(
-                    "dependencies", "group", table(is_super_table=True)
-                )
+                poetry_content["group"] = table(is_super_table=True)
 
             groups = poetry_content["group"]
             if group not in groups:


Test patch:
diff --git a/tests/console/commands/test_remove.py b/tests/console/commands/test_remove.py
--- a/tests/console/commands/test_remove.py
+++ b/tests/console/commands/test_remove.py
@@ -67,9 +67,7 @@ def test_remove_without_specific_group_removes_from_all_groups(
 """
     )
     content["tool"]["poetry"]["dependencies"]["foo"] = "^2.0.0"
-    content["tool"]["poetry"].value._insert_after(
-        "dependencies", "group", groups_content["tool"]["poetry"]["group"]
-    )
+    content["tool"]["poetry"]["group"] = groups_content["tool"]["poetry"]["group"]
     app.poetry.file.write(content)
 
     app.poetry.package.add_dependency(Factory.create_dependency("foo", "^2.0.0"))
@@ -122,9 +120,7 @@ def test_remove_without_specific_group_removes_from_specific_groups(
 """
     )
     content["tool"]["poetry"]["dependencies"]["foo"] = "^2.0.0"
-    content["tool"]["poetry"].value._insert_after(
-        "dependencies", "group", groups_content["tool"]["poetry"]["group"]
-    )
+    content["tool"]["poetry"]["group"] = groups_content["tool"]["poetry"]["group"]
     app.poetry.file.write(content)
 
     app.poetry.package.add_dependency(Factory.create_dependency("foo", "^2.0.0"))
@@ -177,9 +173,7 @@ def test_remove_does_not_live_empty_groups(
 """
     )
     content["tool"]["poetry"]["dependencies"]["foo"] = "^2.0.0"
-    content["tool"]["poetry"].value._insert_after(
-        "dependencies", "group", groups_content["tool"]["poetry"]["group"]
-    )
+    content["tool"]["poetry"]["group"] = groups_content["tool"]["poetry"]["group"]
     app.poetry.file.write(content)
 
     app.poetry.package.add_dependency(Factory.create_dependency("foo", "^2.0.0"))