Repository: python-poetry/poetry

Issue:
`poetry add --extras security requests` fails with `ValueError`
- [ ] ~I am on the [latest](https://github.com/sdispater/poetry/releases/latest) Poetry version.~
- [x] I am on the [master](https://github.com/sdispater/poetry/tree/master) branch of Poetry.
- [x] It can be reproduced on the [latest beta version](https://github.com/sdispater/poetry/releases/tag/1.0.0b1) of Poetry.
- [x] I have searched the [issues](https://github.com/sdispater/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).

- **OS version and name**: `any`
- **Poetry version**: `1.0.0a0` — `1.0.0b1`
- **~Link of a [Gist](https://gist.github.com/) with the contents of your~ pyproject.toml file**: this bug could be reproduced with any `pyproject.toml`, so you can just generate a new one using `poetry init`.

## Issue
Using `poetry` from `master`/`develop` branches or any recent alpha/beta release, it's not possible to add the `requests` package with the `security` extra.

```bash
$ poetry -V
Poetry version 1.0.0b1
$ poetry add --extras security requests -v
Using virtualenv: /home/mim/.cache/pypoetry/virtualenvs/test-poetry-requests-security-XPJu19SV-py3.7
Using version ^2.22.0 for requests

Updating dependencies
Resolving dependencies... (0.1s)

[ValueError]
Array has mixed types elements

Traceback (most recent call last):
  File "/home/mim/.poetry/lib/poetry/_vendor/py3.7/clikit/console_application.py", line 132, in run
    status_code = command.handle(parsed_args, io)
  File "/home/mim/.poetry/lib/poetry/_vendor/py3.7/clikit/api/command/command.py", line 119, in handle
    status_code = self._do_handle(args, io)
  File "/home/mim/.poetry/lib/poetry/_vendor/py3.7/clikit/api/command/command.py", line 167, in _do_handle
    return getattr(handler, handler_method)(args, io, self)
  File "/home/mim/.poetry/lib/poetry/_vendor/py3.7/cleo/commands/command.py", line 92, in wrap_handle
    return self.handle()
  File "/home/mim/.poetry/lib/poetry/console/commands/add.py", line 149, in handle
    status = installer.run()
  File "/home/mim/.poetry/lib/poetry/installation/installer.py", line 74, in run
    self._do_install(local_repo)
  File "/home/mim/.poetry/lib/poetry/installation/installer.py", line 189, in _do_install
    self._write_lock_file(local_repo)
  File "/home/mim/.poetry/lib/poetry/installation/installer.py", line 290, in _write_lock_file
    updated_lock = self._locker.set_lock_data(self._package, repo.packages)
  File "/home/mim/.poetry/lib/poetry/packages/locker.py", line 149, in set_lock_data
    lock['package'] = packages
  File "/home/mim/.poetry/lib/poetry/_vendor/py3.7/tomlkit/container.py", line 551, in __setitem__
    self.append(key, value)
  File "/home/mim/.poetry/lib/poetry/_vendor/py3.7/tomlkit/container.py", line 91, in append
    item = _item(item)
  File "/home/mim/.poetry/lib/poetry/_vendor/py3.7/tomlkit/items.py", line 62, in item
    i = item(_v)
  File "/home/mim/.poetry/lib/poetry/_vendor/py3.7/tomlkit/items.py", line 46, in item
    val[k] = item(v, _parent=val)
  File "/home/mim/.poetry/lib/poetry/_vendor/py3.7/tomlkit/items.py", line 70, in item
    a.append(v)
  File "/home/mim/.poetry/lib/poetry/_vendor/py3.7/tomlkit/items.py", line 778, in append
    raise ValueError('Array has mixed types elements')
```


Discussion:
Hi @sdispater,
I've already submitted a PR with a fix and an explanation of this bug.
It is #1439. Did you see it?
I see you recently commited af71f6c0e585ac20a029e1eccb6501898adb6395, so I'm quite confused now :confused:
Why did you completely ignore the [proposed fix](https://github.com/sdispater/poetry/pull/1439)?
@sdispater
Even worse, af71f6c0e585ac20a029e1eccb6501898adb6395 doesn't cover all the cases due the code causing this issue [is still there](https://github.com/sdispater/poetry/blob/af71f6c0e585ac20a029e1eccb6501898adb6395/poetry/packages/locker.py#L238-L241).
I've updated #1439, so could you please take a look?
Thanks!
@Jamim Sorry for the confusion but while I saw the issue I did not see your related PR and I made the commit just before your comment mentionin your PR. I just commented and requested changes on your PR.

Resolving patch:
diff --git a/poetry/packages/locker.py b/poetry/packages/locker.py
--- a/poetry/packages/locker.py
+++ b/poetry/packages/locker.py
@@ -218,11 +218,6 @@ def _dump_package(self, package):  # type: (poetry.packages.Package) -> dict
 
             if dependency.pretty_name not in dependencies:
                 dependencies[dependency.pretty_name] = []
-            else:
-                # Ensure we don't have mixed types in the lock file
-                for i, spec in enumerate(dependencies[dependency.pretty_name]):
-                    if not isinstance(spec, dict):
-                        dependencies[dependency.pretty_name][i] = {"version": spec}
 
             constraint = {"version": str(dependency.pretty_constraint)}
 
@@ -235,10 +230,15 @@ def _dump_package(self, package):  # type: (poetry.packages.Package) -> dict
             if not dependency.python_constraint.is_any():
                 constraint["python"] = str(dependency.python_constraint)
 
-            if len(constraint) == 1:
-                dependencies[dependency.pretty_name].append(constraint["version"])
-            else:
-                dependencies[dependency.pretty_name].append(constraint)
+            dependencies[dependency.pretty_name].append(constraint)
+
+        # All the constraints should have the same type,
+        # but we want to simplify them if it's possible
+        for dependency, constraints in tuple(dependencies.items()):
+            if all(len(constraint) == 1 for constraint in constraints):
+                dependencies[dependency] = [
+                    constraint["version"] for constraint in constraints
+                ]
 
         data = {
             "name": package.pretty_name,


Test patch: