Repository navigation
URL.Parse reads IRIs with iri=True - #639
Conversation
A URL of RFC 3986 holds ASCII characters only; 'https://www.müller.de/' is an IRI (RFC 3987) with an internationalized domain name. URL.Parse(url, iri=True) maps it to a URL as RFC 3987 section 3.1 describes, so the URL object is an RFC 3986 URL either way: * A Unicode host is converted to its A-labels by IDNA 2008 (RFC 5890/5891), via the PyPI package 'idna': idna.encode(host, uts46=True). UTS #46 mapping is what the WHATWG URL Standard's "domain to ASCII" does in browsers - case folding (MÜLLER.de), width mapping, the ideographic full stop - and is non-transitional: 'straße.de' -> 'xn--strae-oqa.de', where IDNA 2003 and Python's built-in 'idna' codec give 'strasse.de', another domain. A host IDNA 2008 rejects ('☃.net') raises URLError with idna's reason as note. * Every other non-ASCII character (user information, path, query, fragment) is percent-encoded as UTF-8; percent-encoded triplets stay. * An error found in the converted URL names it, and a note names the IRI. * Strict mode (default) is unchanged; its syntax error for a URL holding a Unicode character gets a note offering 'iri=True'. Host.UnicodeHostname decodes A-labels (idna.decode per 'xn--' label, so an ASCII label like 'my_host', which IDNA 2008 would reject, is kept). A hostname without A-labels needs no 'idna' package. 'idna' is optional: new extra 'idna' (idna >= 3.20, the latest release), lazy import where a Unicode host is converted or an A-label decoded, else MissingDependencyError naming 'pyTooling[idna]'. tests/unit/requirements.txt requires it, so CI runs the IRI testcases. Docs: URL.rst section on IRIs/IDNs (RFC 3986, RFC 3987, RFC 5890/5891, UTS #46, WHATWG URL Standard) with the www.müller.de example; Dependency.rst, Installation.rst, conf.py list the extra; News.rst. Co-Authored-By: Patrick Lehmann <Paebbels@gmail.com>
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| Complexity | 1 medium |
🟢 Metrics 52 complexity · 0 duplication
Metric Results Complexity 52 Duplication 0
🟢 Coverage 100.00% diff coverage · +0.02% coverage variation
Metric Results Coverage variation ✅ +0.02% coverage variation (-1.00%) Diff coverage ✅ 100.00% diff coverage Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (6473d71) 16704 15326 91.75% Head commit (16f7370) 16754 (+50) 15376 (+50) 91.78% (+0.02%) Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch:
<coverage of head commit> - <coverage of common ancestor commit>Diff coverage details
Coverable lines Covered lines Diff coverage Pull request (#639) 53 53 100.00% Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified:
<covered lines added or modified>/<coverable lines added or modified> * 100%
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #639 +/- ##
==========================================
+ Coverage 89.22% 89.25% +0.03%
==========================================
Files 63 63
Lines 16704 16754 +50
Branches 2562 2570 +8
==========================================
+ Hits 14904 14954 +50
Misses 1378 1378
Partials 422 422
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
New Features
pyTooling.GenericPath.URL:URL.Parse(url, *, iri=False)reads an IRI (RFC 3987) withiri=True, and maps it to a URL as RFC 3987 §3.1 describes, so theURLobject is an RFC 3986 URL either way andstr()renders it in ASCII:https://www.müller.de/→ hostwww.xn--mller-kva.de./wiki/Straße→/wiki/Stra%C3%9Fe. A percent-encoded triplet stays as it is; a character an IRI forbids too (a space, U+3000) is rejected as before, with the IRI in the message.URLErrorwith idna's reason as note: "Host '☃.net' is no domain name of IDNA 2008 when parsing IRI 'https://☃.net/'.", note "IDNA: Codepoint U+2603 at position 1 of '☃' not allowed".iri, what parses is unchanged. A syntax error of a URL holding a Unicode character gets the note "A URL holds ASCII characters only. Read an IRI, which holds Unicode characters, with 'iri=True'." -https://www.müller.de/raised the bare "Syntax error when parsing URL 'https://www.müller.de/'." since URL.Parse rejects strings that aren't URLs #636.Host.UnicodeHostnamereturns the hostname with its A-labels decoded to Unicode:www.xn--mller-kva.de→www.müller.de. Only labels starting withxn--are decoded, so an ASCII label IDNA 2008 would reject (my_host) is kept; an invalid A-label (xn--zz) raisesURLErrorwith idna's reason as note.idna(pip install pyTooling[idna],idna >= 3.20). The package is imported where it is needed - converting a Unicode host, decoding an A-label - and its absence raisesMissingDependencyErrornamingpyTooling[idna]. An IRI whose host is ASCII, and a hostname without A-labels, need no package.Why IDNA 2008 with UTS #46, through the PyPI package
idna:idnacodec implements IDNA 2003:"straße.de".encode("idna")isb"strasse.de", another domain than thexn--strae-oqa.deDENIC registers, and it accepts symbols IDNA 2008 disallows (☃.net→xn--n3h.net).idna.encode(host, uts46=True)is the mapping the WHATWG URL Standard prescribes for browsers (domain to ASCII: UTS v2.0.0 #46, non-transitional): case folding (MÜLLER.de→xn--mller-kva.de), width mapping (müller。de, with the ideographic full stop as a dot),ßkept. Withoututs46, an upper-case letter is an error.transitionalis left at its defaultFalse; idna 3.20 deprecates and ignores it anyway.idnais the packagerequestsandyarl(aiohttp) depend on, so most installations carry it already. 3.20 is the latest release (2026-09-17).Documentation
doc/DataStructures/Path/URL.rsthas a section Internationalized Resource Identifiers (IRI): what an IRI and an A-label are, the mapping ofiri=True, thewww.müller.deexample in an Example admonition, why the host is mapped as browsers map it (UTS v2.0.0 #46, non-transitional,straße.de,☃.net), a table of the specifications - RFC 3986, RFC 3987, RFC 5890/5891, UTS v2.0.0 #46, WHATWG URL Standard - and what each decides, and theidnaextra.URL.Parse'siriparameter names IRI and RFC 3987; its doc-string andHost.UnicodeHostname's reference RFC 3987 §3.1, RFC 5890, RFC 5891 and UTS v2.0.0 #46. The module doc-string points toiri=True.doc/Dependency.rstlists theidnaextra's table (doc/conf.pyentrypointidna),doc/Installation.rstthe extra in the list and as a tab.doc/News.rstlists the feature with the otherpyTooling.GenericPath.URLitems.Unit Tests
tests/unit/Path/URL.py:IRIs: strict mode rejectshttps://www.müller.de/(message and note) and adds no note to an ASCII syntax error;iri=Truegives the A-label (with port, trailing dot), IDNA 2008 (straße.de→xn--strae-oqa.de), UTS v2.0.0 #46 mapping (upper case, full-width letters, ideographic full stop), percent-encoding of path, query and fragment, of user and password, of a path under an IPv6 host, a percent-encoded triplet kept, thestr()round trip (the converted URL is ASCII and parses withoutiriinto the same string), ASCII URLs read the same with and withoutiri, an IDN IDNA rejects (☃.net, an empty label), the note on an error in the converted URL, missing scheme, forbidden characters (space, U+3000), theiriparameter check, and withoutidna(import blocked):MissingDependencyErrorwith dependency, extra and note, while an IRI with an ASCII host still parses.UnicodeHostnames: A-label decoded (also upper caseXN--), ASCII labels kept beside an A-label, hostnames without A-labels (DNS name, IPv4, IPv6,my_host) unchanged - also withoutidna-, an invalid A-label, andMissingDependencyErrorwithoutidna.tests/unit/requirements.txtrequiresidna >= 3.20, so the IRI testcases run in CI.Related Issues and Pull-Requests
URL.Parsereads strict RFC 3986 there, andiri=Truemaps an IRI onto that parser.iri, the RFCs and thewww.müller.deexample in the documentation, IDNA 2008 viaidnabehind an extraidna).ComputePacboyPackagesmaps the extrapyTooling[idna]and the requirementidnato the MSYS2 packagepython-idna.