fix ruff linting errors

This commit is contained in:
Bill Peck 2026-02-24 13:29:28 -05:00
parent e87b0fd9ec
commit 895bb1c7d5
No known key found for this signature in database
GPG Key ID: F179F385E3D1E013
2 changed files with 15 additions and 15 deletions

View File

@ -243,15 +243,15 @@ class WildcardURLPattern(Pattern):
def __eq__(self, other: typing.Any) -> bool:
return isinstance(other, WildcardURLPattern) and self.pattern == other.pattern
class IPNetPattern(Pattern):
def __init__(self, ip_net: str) -> None:
try:
addr, range = ip_net.split('/', 1)
if addr[0] == '[' and addr[-1] == ']':
addr, range = ip_net.split("/", 1)
if addr[0] == "[" and addr[-1] == "]":
addr = addr[1:-1]
ip_net = f'{addr}/{range}'
ip_net = f"{addr}/{range}"
except ValueError:
pass # not a range
self.net = ipaddress.ip_network(ip_net)
@ -261,7 +261,7 @@ class IPNetPattern(Pattern):
return ipaddress.ip_address(other.host) in self.net
except ValueError:
return False
@property
def priority(self) -> tuple[int, int, int]:
return -1, 0, 0 # higher priority than URLPatterns
@ -274,17 +274,17 @@ class IPNetPattern(Pattern):
def __eq__(self, other: typing.Any) -> bool:
return isinstance(other, IPNetPattern) and self.net == other.net
URLPattern = IPNetPattern | WildcardURLPattern
def build_url_pattern(pattern: str) -> URLPattern:
try:
proto, rest = pattern.split('://', 1)
if proto == 'all' and '/' in rest:
return IPNetPattern(rest)
except ValueError: # covers .split() and IPNetPattern
proto, rest = pattern.split("://", 1)
if proto == "all" and "/" in rest:
return IPNetPattern(rest)
except ValueError: # covers .split() and IPNetPattern
pass
return WildcardURLPattern(pattern)
@ -302,4 +302,4 @@ def is_ipv6_hostname(hostname: str) -> bool:
ipaddress.IPv6Address(hostname.split("/")[0])
except Exception:
return False
return True
return True

View File

@ -128,10 +128,10 @@ def test_get_environment_proxies(environment, proxies):
("http://", "https://example.com", False),
("all://", "https://example.com:123", True),
("", "https://example.com:123", True),
('all://192.168.0.0/24', 'http://192.168.0.1', True),
('all://192.168.0.0/24', 'https://192.168.1.1', False),
('all://[2001:db8:abcd:0012::]/64', 'http://[2001:db8:abcd:12::1]', True),
('all://[2001:db8:abcd:0012::]/64', 'http://[2001:db8:abcd:13::1]:8080', False),
("all://192.168.0.0/24", "http://192.168.0.1", True),
("all://192.168.0.0/24", "https://192.168.1.1", False),
("all://[2001:db8:abcd:0012::]/64", "http://[2001:db8:abcd:12::1]", True),
("all://[2001:db8:abcd:0012::]/64", "http://[2001:db8:abcd:13::1]:8080", False),
],
)
def test_url_matches(pattern, url, expected):