Skip to content

with_path silently promotes a bare-relative URL to an absolute-path reference #1770

Description

@HrachShah

When is called with a path that does not start with on a bare-relative URL (e.g. ), the method unconditionally prepends , silently turning a path-relative reference into an absolute-path reference. The result resolves differently against any base URL.

Repro

>>> from yarl import URL
>>> url = URL('foo/bar')
>>> str(url.with_path('abs'))
'/abs'   # should be 'abs' — the URL is bare-relative, not absolute
>>> url.with_path('abs').is_absolute()
True     # was False before .with_path()

Expected

The path's leading slash status should be preserved across for URLs without a netloc: a bare-relative URL stays bare-relative unless the caller explicitly passes a slash-prefixed path.

Workaround

Pass an explicit to keep the slash-prefixed result (this case still works). The fix preserves that behavior.

Risk

Any caller relying on the old behavior of getting back when passing to a bare-relative URL will see a behavior change. That caller can pass explicitly to opt in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions