Skip to content

fix(authenticator): handle missing URL in repository config - #11081

Merged
radoering merged 6 commits into
python-poetry:mainfrom
webdevsamran:fix/authenticator-bytes-url
Nov 15, 2026
Merged

radoering merged 6 commits into
python-poetry:mainfrom
webdevsamran:fix/authenticator-bytes-url

Conversation

@webdevsamran

Copy link
Copy Markdown
Contributor

Pull Request Check List

Resolves: #11080

  • Added tests for changed code.
  • Updated documentation for changed code.

Description

In Poetry 2.5, AuthenticatorRepositoryConfig computes path segments via _path_segments(self.path) where self.path is extracted from urllib.parse.urlsplit(self.url).

If self.url or a candidate request URL is passed as bytes (e.g. from environment sources, credentials, or custom plugin hooks), urlsplit returns a SplitResultBytes where path is also bytes. Passing a bytes path to _path_segments causes path.split("/") to raise:

TypeError: a bytes-like object is required, not 'str'

Solution

  • In AuthenticatorRepositoryConfig.__post_init__, decode self.url to str if it is an instance of bytes.
  • In _path_segments and path_match_key, accept str | bytes and decode bytes before segment extraction.
  • In _get_repository_config_for_url, decode url if it is bytes before URL parsing.

Verification

Added regression tests in tests/utils/test_authenticator.py covering:

  • AuthenticatorRepositoryConfig with bytes URL and bytes request paths.
  • _path_segments with bytes inputs.
  • Authenticator.get_repository_config_for_url with bytes URL.

Ran the full authenticator test suite (78 passed in 6.40s) and verified formatting with ruff.


🤖 AI-assisted contribution — logic and code authored with LLM support, reviewed and verified by Samran Asif (@webdevsamran).

@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 reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. If the new bytes-to-text normalization or path matching is wrong, a bytes URL could select the wrong repository configuration and cause credentials or certificate settings to be applied to an unintended endpoint. Reverting stops future misrouting, but any credentials already sent to that endpoint cannot be recovered.


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

@webdevsamran
webdevsamran force-pushed the fix/authenticator-bytes-url branch from 34b7f7b to 1db8d0b Compare September 22, 2026 09:18

@kokokoXUY kokokoXUY left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The confirmed #11080 reproducer is an empty [repositories.company-internal] section with no url, so Config.get(..., "url") returns None rather than a bytes URL. urlsplit(None) produces a byte-valued result with path=b''; this PR's _path_segments() now decodes that empty path, allowing AuthenticatorRepositoryConfig to be constructed with url=None and byte-valued scheme, netloc, and path. The startup exception disappears, but the invalid repository entry remains silently present instead of producing the clear configuration error requested in the issue.

Could you add a regression test with a repository section missing url and reject it with a descriptive error at the configuration boundary? The bytes-input support can be considered separately if there is a supported path that produces bytes URLs.

@kokokoXUY

Copy link
Copy Markdown
Contributor

Follow-up to my earlier review, with a correction: I have since verified that
raising a configuration error as soon as the url-less entry is seen does not
work either, because poetry config builds the Poetry object as well:

$ poetry config repositories.company-internal https://company-internal.host
The repository 'company-internal' is missing a url. Please set it with `poetry config repositories.company-internal <url>`.
$ echo $?
1

So rejecting the entry at the configuration boundary would leave users unable
to repair config.toml with the CLI, which is why the comprehensible error is
better placed where the repository is actually used. I opened #11114 for that
path (poetry publish -r <name>); it is complementary to this PR.

For this PR, a regression test that uses the reproducer from the issue (an
empty [repositories.<name>] section, i.e. url is None rather than
bytes) would be valuable, since the current tests cover the bytes input
only.

@webdevsamran

Copy link
Copy Markdown
Contributor Author

Hi @kokokoXUY, thank you for the detailed analysis and follow-up!

I have implemented the requested reproducer regression test in commit 36862ac:

  1. AuthenticatorRepositoryConfig.__post_init__:

    • Explicitly handles self.url is None by initializing scheme = "", netloc = "", path = "", and _path_segments = [], ensuring string fields remain typed as str rather than falling back to SplitResultBytes with byte-valued fields.
  2. Regression tests in tests/utils/test_authenticator.py:

    • test_repository_config_supports_none_url: directly validates AuthenticatorRepositoryConfig with url=None.
    • test_authenticator_handles_empty_repository_section_without_url: uses the exact reproducer from Poetry 2.5.0/2.5.1 TypeError str instead of bytes-like object #11080 (an empty [repositories.company-internal] section with no url key), verifying that configured_repositories populates safely without raising TypeError.

All changes signed off and pushed to the PR branch.

@radoering

Copy link
Copy Markdown
Member

As per #11080 (comment) this may not be the desired fix:

I do not think, there is a use case to have a repository without a URL. Thus, we probably should just provide a comprehensible error message.

The type hint of AuthenticatorConfig.url declares its type as str and I think we should stick with it. We should just raise a comprehensible error message instead of creating an AuthenticatorConfig without an URL.

@webdevsamran

Copy link
Copy Markdown
Contributor Author

Hi @radoering,

Thank you for the guidance! I agree that preserving the str contract on AuthenticatorRepositoryConfig.url and raising a descriptive, actionable error upfront is the cleaner design.

I have updated the implementation in commit 33bd0be8:

  1. AuthenticatorRepositoryConfig:
    • Preserved url: str without allowing None.
    • In __post_init__, validates self.url and raises PoetryError:
      The repository '<name>' is missing a url. Please set it with poetry config repositories. .
  2. Authenticator.configured_repositories:
    • Checks if a repository defined under [repositories.<name>] in configuration lacks a url. If so, raises PoetryError immediately with the clear fix command instead of constructing an invalid repository config.
  3. Tests in tests/utils/test_authenticator.py:
    • Added test_repository_config_raises_error_for_missing_url validating direct instantiation without a URL raises PoetryError.
    • Added test_authenticator_raises_error_for_empty_repository_section_without_url using the reproducer from Poetry 2.5.0/2.5.1 TypeError str instead of bytes-like object #11080 (empty [repositories.company-internal] section) and asserting the exact clear error message.

All unit tests are passing and changes have been pushed to this PR branch.

@radoering radoering left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I do not think we have to handle bytes because they only resulted from an empty URL. See #11080 (comment)

@webdevsamran

Copy link
Copy Markdown
Contributor Author

Hi @radoering,

Thank you! You are completely right — the bytes handling was only added because urlsplit(None) produced SplitResultBytes when an empty repository section was encountered. Now that missing URLs raise a clear PoetryError upfront, URLs and paths are guaranteed to be strings.

In commit de59ab2c:

  1. Removed all bytes normalization and decoding logic from AuthenticatorRepositoryConfig.__post_init__, path_match_key, _get_repository_config_for_url, and _path_segments.
  2. Restored path and URL signatures strictly to str.
  3. Removed the redundant bytes tests from tests/utils/test_authenticator.py, keeping test coverage focused on asserting that empty/missing repository configurations raise the comprehensible PoetryError.

All unit tests pass cleanly.

@sourcery-ai

sourcery-ai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Linked issues

Checked at commit 618a563

#11080 Poetry 2.5.0/2.5.1 TypeError str instead of bytes-like object · 1 of 1 met

# Requirement Status Notes
1 Handle a repository configuration entry with an undefined URL. ✅ Met The authenticator now raises a PoetryError with the repository name and instructions for setting its URL, instead of failing with the reported TypeError.

sourcery-ai[bot]
sourcery-ai Bot previously approved these changes Nov 13, 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.

webdevsamran and others added 6 commits November 15, 2026 06:55
In Poetry 2.5, `AuthenticatorRepositoryConfig` computes path segments via
`_path_segments(self.path)` where `self.path` is extracted from
`urllib.parse.urlsplit(self.url)`. If `self.url` or a candidate request URL
is passed as `bytes`, `urlsplit` returns `SplitResultBytes` where `path` is
also `bytes`, causing `path.split("/")` to raise `TypeError: a bytes-like
object is required, not 'str'`.

Fix this by ensuring `self.url` is decoded to `str` in `__post_init__`,
and having `_path_segments`, `path_match_key`, and
`_get_repository_config_for_url` decode `bytes` inputs to `str` before
segment splitting and parsing.

Added regression tests in `tests/utils/test_authenticator.py` covering
bytes URLs, bytes paths, and whole-path repository matching.

Fixes python-poetry#11080

Signed-off-by: Samran Asif <[email protected]>

AI-assisted contribution — logic and code authored with LLM support,
reviewed and verified by Samran Asif (webdevsamran).

@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.

@radoering radoering changed the title fix(authenticator): handle bytes URL and path in repository config fix(authenticator): handle missing URL in repository config Nov 15, 2026
@radoering
radoering enabled auto-merge (squash) November 15, 2026 06:03
@radoering
radoering merged commit 86093b2 into python-poetry:main Nov 15, 2026
52 checks passed
radoering pushed a commit that referenced this pull request Nov 21, 2026
mwalbeck pushed a commit to mwalbeck/docker-python-poetry that referenced this pull request Nov 28, 2026
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [poetry](https://github.com/python-poetry/poetry) ([changelog](https://python-poetry.org/history/)) | patch | `2.5.1` → `2.5.2` |

---

### Release Notes

<details>
<summary>python-poetry/poetry (poetry)</summary>

### [`v2.5.2`](https://github.com/python-poetry/poetry/blob/HEAD/CHANGELOG.md#252---2026-11-21)

[Compare Source](python-poetry/poetry@2.5.1...2.5.2)

##### Changed

- Allow `pkginfo>=2.0` ([#&#8203;11239](python-poetry/poetry#11239)).
- Report a comprehensible error if a configured repository is missing a URL ([#&#8203;11081](python-poetry/poetry#11081),
  [#&#8203;11114](python-poetry/poetry#11114),
  [#&#8203;11242](python-poetry/poetry#11242)).
- Report a comprehensible error for an invalid `--priority` in `poetry source add` instead of failing with a `KeyError` ([#&#8203;11175](python-poetry/poetry#11175)).

##### Fixed

- Fix an issue where `poetry install` built an sdist before checking it against its lock file hash ([#&#8203;11253](python-poetry/poetry#11253)).
- Fix an issue where `poetry install` rejected a distribution even though one of the hashes provided by the package index matched the hash locked for that file. ([#&#8203;11131](python-poetry/poetry#11131)).
- Fix an issue where uninstalling a path dependency failed if its source did not exist anymore ([#&#8203;11093](python-poetry/poetry#11093)).
- Fix an issue where `poetry version prerelease` failed for a dev release ([#&#8203;11217](python-poetry/poetry#11217)).

##### Docs

- Document the options of `poetry debug resolve` ([#&#8203;11211](python-poetry/poetry#11211)).
- Clarify `solver.min-release-age-exclude` and `solver.min-release-age-exclude-source` ([#&#8203;11087](python-poetry/poetry#11087)).
- Distinguish package and upload repository URLs ([#&#8203;11143](python-poetry/poetry#11143)).
- Explain the certificate configuration for system Git ([#&#8203;11145](python-poetry/poetry#11145)).
- Add separate directory and archive path dependency examples ([#&#8203;11079](python-poetry/poetry#11079)).
- Use the plugin repository for the `poetry-export` pre-commit hook ([#&#8203;11213](python-poetry/poetry#11213)).
- Fix the incorrect `tool.poetry` section name in the FAQ ([#&#8203;11150](python-poetry/poetry#11150)).

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4yNjAuMCIsInVwZGF0ZWRJblZlciI6IjQzLjI2MC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Reviewed-on: https://git.walbeck.it/mwalbeck/docker-python-poetry/pulls/1778
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 2.5.0/2.5.1 TypeError str instead of bytes-like object

3 participants