Skip IP parsing for hostnames in CIDR proxy patterns
PA
5h ago+4+4+0123456789-0-0-0123456789
Summary
With a CIDR entry in NO_PROXY (since #1165), URLPattern.matches() calls ipaddress.ip_address(other.host) for every request. For a normal hostname this raises and catches a ValueError once per CIDR mount. A common Kubernetes/corporate NO_PROXY has three of those, and the mounts are built even without any proxy configured.
An IP literal always starts with a digit (IPv4) or contains : (IPv6), so anything else can skip the parse.
Client._transport_for_url() for https://api.openai.com/... with NO_PROXY=localhost,127.0.0.1,::1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,.svc,.cluster.local,.internal:
| µs/call | |
|---|---|
| main | 5.15 |
| this PR | 1.21 |
Checklist
- I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
- I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
- I've updated the documentation accordingly.