Skip to content

fix(config): handle invalid repository configurations gracefully - #11057

Open
CAOShurong wants to merge 1 commit into
python-poetry:mainfrom
CAOShurong:fix-config-invalid-repo-url
Open

CAOShurong wants to merge 1 commit into
python-poetry:mainfrom
CAOShurong:fix-config-invalid-repo-url

Conversation

@CAOShurong

@CAOShurong CAOShurong commented Sep 11, 2026

Copy link
Copy Markdown

Pull Request Check List

Resolves: #10570

  • Added tests for changed code.
  • Updated documentation for changed code (not needed for this recovery-path fix).

When a repository in config.toml contains an invalid or nested configuration, Poetry could fail while reading it before recovery commands such as poetry config --unset or poetry config --list had a chance to run. Invalid individual URLs could reach urllib.parse.urlsplit, while a malformed top-level repositories value such as null, a list, or a scalar could fail even earlier during configuration traversal.

Changes:

  • Skip a malformed top-level repositories value with a warning.
  • Skip individual repositories whose URL is not a string.
  • Guard direct AuthenticatorRepositoryConfig construction against a non-string URL.
  • Catch PoetryError while locating local project configuration so poetry config can still recover.
  • Cover nested invalid URLs and four non-mapping top-level shapes.

Validation on exact head 87c580bc:

  • 127 focused authenticator/config-command tests passed.
  • Mypy passed for the two changed authenticator files.
  • The complete pre-commit hook set passed for the two changed authenticator files.
  • git diff --check passed.

AI assistance: OpenAI Codex assisted with the follow-up diagnosis, implementation, tests, and verification. The contributor reviewed and owns the submitted change.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/poetry/utils/authenticator.py" line_range="403-405" />
<code_context>
             self._configured_repositories = {}
             for repository_name in self._config.get("repositories", []):
                 url = self._config.get(["repositories", repository_name, "url"])
+                if not isinstance(url, str):
+                    logger.warning(
+                        "Repository '%s' has an invalid url configured in settings;"
</code_context>
<issue_to_address>
**issue (bug_risk):** `configured_repositories` still raises `TypeError` before reaching the new URL check when the top-level `repositories` setting is `None`, a list, or another non-iterable/non-mapping value, because it iterates the raw setting directly. Such a malformed repository configuration still prevents commands from recovering.

**Triggers:** When `config.toml` contains a malformed top-level `repositories` value rather than a mapping.

**Suggested fix:** Read the setting with a mapping default and skip or warn when it is not a mapping before iterating its repository names.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: src/poetry/utils/authenticator.py:405


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/poetry/utils/authenticator.py Outdated
@CAOShurong
CAOShurong force-pushed the fix-config-invalid-repo-url branch from c20e604 to 5e1f9ac Compare September 16, 2026 06:54
sourcery-ai[bot]
sourcery-ai Bot previously approved these changes Sep 16, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery assessment

Approved.

Skip malformed top-level repository mappings and individual non-string URLs so recovery commands can continue instead of failing during configuration traversal or URL parsing.

Guard direct repository-config construction, handle PoetryError while locating local config, and cover nested and top-level malformed cases.

Fixes python-poetry#10570.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery assessment

Approved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Poetry config can be messed up by incorrect user command

1 participant