python-poetry/poetry · error · ValueError
Invalid version constraint: {constraint}
Error message
Invalid version constraint: {constraint} What it means
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.
Source
Thrown at src/poetry/console/commands/init.py:535
@staticmethod
def _validate_package(package: str | None) -> str | None:
if package and len(package.split()) > 2:
raise ValueError("Invalid package definition.")
return package
@staticmethod
def _validate_version_constraint(constraint: str | None) -> str | None:
from poetry.core.constraints.version import parse_constraint
constraint = (constraint or "").strip()
if not constraint:
return None
try:
parse_constraint(constraint)
except ValueError as e:
raise ValueError(f"Invalid version constraint: {constraint}") from e
return constraint
def _get_pool(self) -> RepositoryPool:
from poetry.config.config import Config
from poetry.repositories import RepositoryPool
from poetry.repositories.pypi_repository import PyPiRepository
if isinstance(self, EnvCommand):
return self.poetry.pool
if self._pool is None:
self._pool = RepositoryPool()
pool_size = Config.create().installer_max_workers
self._pool.add_repository(PyPiRepository(pool_size=pool_size))
return self._pool
View on GitHub (pinned to 92b74dcfe3)
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')"`.
Example fix
# before ~>=1.0 # after ^1.0
Defensive patterns
Strategy: validation
Validate before calling
from poetry.core.constraints.version import parse_constraint
constraint = "^1.2"
try:
parse_constraint(constraint)
except ValueError:
raise SystemExit(f"invalid constraint: {constraint}") Type guard
def is_valid_constraint(value: str | None) -> bool:
from poetry.core.constraints.version import parse_constraint
value = (value or "").strip()
if not value:
return True # blank is allowed
try:
parse_constraint(value)
return True
except ValueError:
return False Prevention
- Use Poetry caret/tilde or PEP 440 operators with no spaces.
- Leave blank to skip.
- Unit-test constraints before committing a CI matrix.
When it happens
Trigger: 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.
Common situations: Mixing PEP 440 and npm-style syntax; trailing operators; copy-pasting a constraint from a non-Python ecosystem.
Related errors
- Invalid author string. Must be in the format: John Smith <jo
- Invalid package definition.
- Setting {self.argument('key')} does not exist
- You can only pass one value.
- "{value}" is an invalid value for {key}
AI-assisted analysis of python-poetry/poetry@92b74dcfe3 (2026-08-04).
Data as JSON: /data/errors/b4363d620699ac70.json.
Report an issue: GitHub.