import re

from .empty_constraint import EmptyConstraint
from .patterns import BASIC_CONSTRAINT
from .patterns import CARET_CONSTRAINT
from .patterns import TILDE_CONSTRAINT
from .patterns import TILDE_PEP440_CONSTRAINT
from .patterns import X_CONSTRAINT
from .version import Version
from .version_constraint import VersionConstraint
from .version_range import VersionRange
from .version_union import VersionUnion


def parse_constraint(constraints):  # type: (str) -> VersionConstraint
    if constraints == "*":
        return VersionRange()

    or_constraints = re.split(r"\s*\|\|?\s*", constraints.strip())
    or_groups = []
    for constraints in or_constraints:
        and_constraints = re.split(
            "(?<!^)(?<![=>< ,]) *(?<!-)[, ](?!-) *(?!,|$)", constraints
        )
        constraint_objects = []

        if len(and_constraints) > 1:
            for constraint in and_constraints:
                constraint_objects.append(parse_single_constraint(constraint))
        else:
            constraint_objects.append(parse_single_constraint(and_constraints[0]))

        if len(constraint_objects) == 1:
            constraint = constraint_objects[0]
        else:
            constraint = constraint_objects[0]
            for next_constraint in constraint_objects[1:]:
                constraint = constraint.intersect(next_constraint)

        or_groups.append(constraint)

    if len(or_groups) == 1:
        return or_groups[0]
    else:
        return VersionUnion.of(*or_groups)


def parse_single_constraint(constraint):  # type: (str) -> VersionConstraint
    m = re.match(r"(?i)^v?[xX*](\.[xX*])*$", constraint)
    if m:
        return VersionRange()

    # Tilde range
    m = TILDE_CONSTRAINT.match(constraint)
    if m:
        version = Version.parse(m.group(1))

        high = version.stable.next_minor
        if len(m.group(1).split(".")) == 1:
            high = version.stable.next_major

        return VersionRange(
            version, high, include_min=True, always_include_max_prerelease=True
        )

    # PEP 440 Tilde range (~=)
    m = TILDE_PEP440_CONSTRAINT.match(constraint)
    if m:
        precision = 1
        if m.group(3):
            precision += 1

            if m.group(4):
                precision += 1

        version = Version.parse(m.group(1))

        if precision == 2:
            low = version
            high = version.stable.next_major
        else:
            low = Version(version.major, version.minor, 0)
            high = version.stable.next_minor

        return VersionRange(
            low, high, include_min=True, always_include_max_prerelease=True
        )

    # Caret range
    m = CARET_CONSTRAINT.match(constraint)
    if m:
        version = Version.parse(m.group(1))

        return VersionRange(
            version,
            version.next_breaking,
            include_min=True,
            always_include_max_prerelease=True,
        )

    # X Range
    m = X_CONSTRAINT.match(constraint)
    if m:
        op = m.group(1)
        major = int(m.group(2))
        minor = m.group(3)

        if minor is not None:
            version = Version(major, int(minor), 0)

            result = VersionRange(
                version,
                version.next_minor,
                include_min=True,
                always_include_max_prerelease=True,
            )
        else:
            if major == 0:
                result = VersionRange(max=Version(1, 0, 0))
            else:
                version = Version(major, 0, 0)

                result = VersionRange(
                    version,
                    version.next_major,
                    include_min=True,
                    always_include_max_prerelease=True,
                )

        if op == "!=":
            result = VersionRange().difference(result)

        return result

    # Basic comparator
    m = BASIC_CONSTRAINT.match(constraint)
    if m:
        op = m.group(1)
        version = m.group(2)

        if version == "dev":
            version = "0.0-dev"

        try:
            version = Version.parse(version)
        except ValueError:
            raise ValueError(
                "Could not parse version constraint: {}".format(constraint)
            )

        if op == "<":
            return VersionRange(max=version)
        elif op == "<=":
            return VersionRange(max=version, include_max=True)
        elif op == ">":
            return VersionRange(min=version)
        elif op == ">=":
            return VersionRange(min=version, include_min=True)
        elif op == "!=":
            return VersionUnion(VersionRange(max=version), VersionRange(min=version))
        else:
            return version

    raise ValueError("Could not parse version constraint: {}".format(constraint))

poetry/semver/__init__.py
import re

from .empty_constraint import EmptyConstraint
from .patterns import BASIC_CONSTRAINT
from .patterns import CARET_CONSTRAINT
from .patterns import TILDE_CONSTRAINT
from .patterns import TILDE_PEP440_CONSTRAINT
from .patterns import X_CONSTRAINT
from .version import Version
from .version_constraint import VersionConstraint
from .version_range import VersionRange
from .version_union import VersionUnion


def parse_constraint(constraints):  # type: (str) -> VersionConstraint
    if constraints == "*":
        return VersionRange()

    or_constraints = re.split(r"\s*\|\|?\s*", constraints.strip())
    or_groups = []
    for constraints in or_constraints:
        and_constraints = re.split(
            "(?<!^)(?<![=>< ,]) *(?<!-)[, ](?!-) *(?!,|$)", constraints
        )
        constraint_objects = []

        if len(and_constraints) > 1:
            for constraint in and_constraints:
                constraint_objects.append(parse_single_constraint(constraint))
        else:
            constraint_objects.append(parse_single_constraint(and_constraints[0]))

        if len(constraint_objects) == 1:
            constraint = constraint_objects[0]
        else:
            constraint = constraint_objects[0]
            for next_constraint in constraint_objects[1:]:
                constraint = constraint.intersect(next_constraint)

        or_groups.append(constraint)

    if len(or_groups) == 1:
        return or_groups[0]
    else:
        return VersionUnion.of(*or_groups)


def parse_single_constraint(constraint):  # type: (str) -> VersionConstraint
    m = re.match(r"(?i)^v?[xX*](\.[xX*])*$", constraint)
    if m:
        return VersionRange()

    # Tilde range
    m = TILDE_CONSTRAINT.match(constraint)
    if m:
        version = Version.parse(m.group(1))

        high = version.stable.next_minor
        if len(m.group(1).split(".")) == 1:
            high = version.stable.next_major

        return VersionRange(
            version, high, include_min=True, always_include_max_prerelease=True
        )

    # PEP 440 Tilde range (~=)
    m = TILDE_PEP440_CONSTRAINT.match(constraint)
    if m:
        precision = 1
        if m.group(3):
            precision += 1

            if m.group(4):
                precision += 1

        version = Version.parse(m.group(1))

        if precision == 2:
            low = version
            high = version.stable.next_major
        else:
            low = Version(version.major, version.minor, version.patch)
            high = version.stable.next_minor

        return VersionRange(
            low, high, include_min=True, always_include_max_prerelease=True
        )

    # Caret range
    m = CARET_CONSTRAINT.match(constraint)
    if m:
        version = Version.parse(m.group(1))

        return VersionRange(
            version,
            version.next_breaking,
            include_min=True,
            always_include_max_prerelease=True,
        )

    # X Range
    m = X_CONSTRAINT.match(constraint)
    if m:
        op = m.group(1)
        major = int(m.group(2))
        minor = m.group(3)

        if minor is not None:
            version = Version(major, int(minor), 0)

            result = VersionRange(
                version,
                version.next_minor,
                include_min=True,
                always_include_max_prerelease=True,
            )
        else:
            if major == 0:
                result = VersionRange(max=Version(1, 0, 0))
            else:
                version = Version(major, 0, 0)

                result = VersionRange(
                    version,
                    version.next_major,
                    include_min=True,
                    always_include_max_prerelease=True,
                )

        if op == "!=":
            result = VersionRange().difference(result)

        return result

    # Basic comparator
    m = BASIC_CONSTRAINT.match(constraint)
    if m:
        op = m.group(1)
        version = m.group(2)

        if version == "dev":
            version = "0.0-dev"

        try:
            version = Version.parse(version)
        except ValueError:
            raise ValueError(
                "Could not parse version constraint: {}".format(constraint)
            )

        if op == "<":
            return VersionRange(max=version)
        elif op == "<=":
            return VersionRange(max=version, include_max=True)
        elif op == ">":
            return VersionRange(min=version)
        elif op == ">=":
            return VersionRange(min=version, include_min=True)
        elif op == "!=":
            return VersionUnion(VersionRange(max=version), VersionRange(min=version))
        else:
            return version

    raise ValueError("Could not parse version constraint: {}".format(constraint))

poetry/semver/__init__.py
diff --git a/poetry/semver/__init__.py b/poetry/semver/__init__.py
index 40b3cd4ec0c..58ca3a1edc3 100644
--- a/poetry/semver/__init__.py
+++ b/poetry/semver/__init__.py
@@ -79,7 +79,7 @@ def parse_single_constraint(constraint):  # type: (str) -> VersionConstraint
             low = version
             high = version.stable.next_major
         else:
-            low = Version(version.major, version.minor, 0)
+            low = Version(version.major, version.minor, version.patch)
             high = version.stable.next_minor
 
         return VersionRange(
diff --git a/tests/semver/test_main.py b/tests/semver/test_main.py
index d6df3560d2d..27b8c13561f 100644
--- a/tests/semver/test_main.py
+++ b/tests/semver/test_main.py
@@ -67,7 +67,7 @@ def test_parse_constraint_wildcard(input, constraint):
         ("~0.3", VersionRange(Version(0, 3, 0), Version(0, 4, 0), True)),
         ("~3.5", VersionRange(Version(3, 5, 0), Version(3, 6, 0), True)),
         ("~=3.5", VersionRange(Version(3, 5, 0), Version(4, 0, 0), True)),  # PEP 440
-        ("~=3.5.3", VersionRange(Version(3, 5, 0), Version(3, 6, 0), True)),  # PEP 440
+        ("~=3.5.3", VersionRange(Version(3, 5, 3), Version(3, 6, 0), True)),  # PEP 440
     ],
 )
 def test_parse_constraint_tilde(input, constraint):

`~=` version constraints (PEP 440) do not correctly set the lower version
<!--
  Hi there! Thank you for discovering and submitting an issue.

  Before you submit this; let's make sure of a few things.
  Please make sure the following boxes are ticked if they are correct.
  If not, please try and fulfill these first.
-->

<!-- Checked checkbox should look like this: [x] -->
- [x] I am on the [latest](https://github.com/sdispater/poetry/releases/latest) Poetry version.
- [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).

<!--
  Once those are done, if you're able to fill in the following list with your information,
  it'd be very helpful to whoever handles the issue.
-->

- **OS version and name**: Windows 10
- **Poetry version**: 0.12.16
- **Link of a [Gist](https://gist.github.com/) with the contents of your pyproject.toml file**: N/A

## Issue
<!-- Now feel free to write your issue, but please be descriptive! Thanks again 🙌 ❤️ -->

Found this while fixing handling of pre-release (#1150)

PEP 440 says
- `~=V.N` maps to `>= V.N, == V.*`

Examples
```
~= 2.2
>= 2.2, == 2.*

~= 1.4.5
>= 1.4.5, == 1.4.*

~= 2.2.0
>= 2.2.0, == 2.2.*

~= 1.4.5.0
>= 1.4.5.0, == 1.4.5.*
```

but looking at the tests `~=3.5.3` maps to `>=3.5.0, < 3.6` when it should map to `>=3.5.3, <3.6`. The trailing `3` should be setting the floor for the version
