{"id":"b4363d620699ac70","repo":"python-poetry/poetry","slug":"invalid-version-constraint-constraint","errorCode":null,"errorMessage":"Invalid version constraint: {constraint}","messagePattern":"Invalid version constraint: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/poetry/console/commands/init.py","lineNumber":535,"sourceCode":"    @staticmethod\n    def _validate_package(package: str | None) -> str | None:\n        if package and len(package.split()) > 2:\n            raise ValueError(\"Invalid package definition.\")\n\n        return package\n\n    @staticmethod\n    def _validate_version_constraint(constraint: str | None) -> str | None:\n        from poetry.core.constraints.version import parse_constraint\n\n        constraint = (constraint or \"\").strip()\n        if not constraint:\n            return None\n\n        try:\n            parse_constraint(constraint)\n        except ValueError as e:\n            raise ValueError(f\"Invalid version constraint: {constraint}\") from e\n\n        return constraint\n\n    def _get_pool(self) -> RepositoryPool:\n        from poetry.config.config import Config\n        from poetry.repositories import RepositoryPool\n        from poetry.repositories.pypi_repository import PyPiRepository\n\n        if isinstance(self, EnvCommand):\n            return self.poetry.pool\n\n        if self._pool is None:\n            self._pool = RepositoryPool()\n            pool_size = Config.create().installer_max_workers\n            self._pool.add_repository(PyPiRepository(pool_size=pool_size))\n\n        return self._pool\n","sourceCodeStart":517,"sourceCodeEnd":553,"githubUrl":"https://github.com/python-poetry/poetry/blob/92b74dcfe348d0e01e14d40d6c1fa47a4ee04a54/src/poetry/console/commands/init.py#L517-L553","documentation":"ValueError from `_validate_version_constraint` (init.py:532-535) when `poetry.core.constraints.version.parse_constraint` raises. Wraps the underlying ValueError to surface the offending constraint string. Used by `poetry init` when prompting for a version constraint.","triggerScenarios":"Entering `>=1.0,<2` with invalid punctuation; unbalanced parentheses `(>=1.0`; unknown operators `~>=1.0`; empty constraint after stripping is allowed (returns None), so this fires only on syntactically non-empty but unparseable input.","commonSituations":"Mixing PEP 440 and npm-style syntax; trailing operators; copy-pasting a constraint from a non-Python ecosystem.","solutions":["Use PEP 440 / Poetry constraint syntax: `^1.2`, `~2.0`, `>=1.0,<2.0`, `==1.4.*`.","Leave the prompt blank to skip the constraint.","Test with `python -c \"from poetry.core.constraints.version import parse_constraint; parse_constraint('^1.2')\"`."],"exampleFix":"# before\n~>=1.0\n# after\n^1.0","handlingStrategy":"validation","validationCode":"from poetry.core.constraints.version import parse_constraint\nconstraint = \"^1.2\"\ntry:\n    parse_constraint(constraint)\nexcept ValueError:\n    raise SystemExit(f\"invalid constraint: {constraint}\")","typeGuard":"def is_valid_constraint(value: str | None) -> bool:\n    from poetry.core.constraints.version import parse_constraint\n    value = (value or \"\").strip()\n    if not value:\n        return True  # blank is allowed\n    try:\n        parse_constraint(value)\n        return True\n    except ValueError:\n        return False","tryCatchPattern":null,"preventionTips":["Use Poetry caret/tilde or PEP 440 operators with no spaces.","Leave blank to skip.","Unit-test constraints before committing a CI matrix."],"tags":["init","versioning","validation"],"analyzedSha":"92b74dcfe348d0e01e14d40d6c1fa47a4ee04a54","analyzedAt":"2026-08-04T20:33:34.072Z","schemaVersion":2}