python-poetry/poetry · error · ValueError

You cannot remove the [repositories] section

Error message

You cannot remove the [repositories] section

What it means

Raises ValueError in ConfigCommand.handle() when the user tries to unset the entire [repositories] section rather than a specific repository. The repo_regex matches group(1) as None (no repository name specified), and the check at line 235-236 blocks removal of the whole section to prevent accidental loss of all configured repositories.

Source

Thrown at src/poetry/console/commands/config.py:236

        values: list[str] = self.argument("value")

        if setting_key in self.unique_config_values:
            if self.option("unset"):
                config.config_source.remove_property(setting_key)
                return 0

            return self._handle_single_value(
                config.config_source,
                setting_key,
                self.unique_config_values[setting_key],
                values,
            )

        # handle repositories
        m = re.match(repo_regex, self.argument("key"))
        if m:
            if not m.group(1):
                raise ValueError("You cannot remove the [repositories] section")

            repo_name = m.group(1)

            if self.option("unset"):
                repo = config.get(["repositories", repo_name])
                if repo is None:
                    raise ValueError(f"There is no {repo_name} repository defined")

                if m.group(2):
                    # Unset a specific sub-property
                    config.config_source.remove_property(
                        ["repositories", repo_name, m.group(2)]
                    )
                else:
                    config.config_source.remove_property(["repositories", repo_name])

                return 0

View on GitHub (pinned to 92b74dcfe3)

Solutions

  1. Specify the individual repository to unset: `poetry config repositories.myrepo --unset`.
  2. Unset each repository individually if you need to remove all of them.

Example fix

# before (error)
poetry config repositories --unset
# after
poetry config repositories.myrepo --unset
Defensive patterns

Strategy: validation

Validate before calling

import re

def validate_repo_unset(key: str) -> None:
    m = re.match(r"^repos?(?:itories)?(?:\.(.+?)(?:\.(url))?)?$", key)
    if m and not m.group(1):
        raise ValueError("Cannot unset the entire [repositories] section; specify a repo name")

Type guard

import re

def has_specific_repo_name(key: str) -> bool:
    m = re.match(r"^repos?(?:itories)?(?:\.(.+?)(?:\.(url))?)?$", key)
    return bool(m and m.group(1))

Prevention

When it happens

Trigger: Running `poetry config repositories --unset` or `poetry config repo --unset` — the repo_regex matches but group(1) is empty/None.

Common situations: User intends to remove one repository but forgets the name, or attempts a bulk unset of all repositories.

Related errors


AI-assisted analysis of python-poetry/poetry@92b74dcfe3 (2026-08-04). Data as JSON: /data/errors/c42d89e65d77d7e7.json. Report an issue: GitHub.