@@ -124,10 +124,12 @@ def _bool_matcher(
124124@beartype
125125def _unused_local_url () -> str :
126126 """Return a URL for a local address with nothing listening on it."""
127- sock = socket .socket ()
128- sock .bind (("" , 0 ))
129- port = sock .getsockname ()[1 ] # pyrefly: ignore [unknown-variable-type]
130- sock .close ()
127+ with socket .socket () as sock :
128+ sock .bind (("" , 0 ))
129+ address = sock .getsockname ()
130+ assert isinstance (address , tuple )
131+ assert isinstance (address [1 ], int )
132+ port : int = address [1 ]
131133 return f"http://localhost:{ port } "
132134
133135
@@ -142,11 +144,7 @@ def request_unmocked_address() -> None:
142144 context of a ``responses`` mock which does not mock local
143145 addresses.
144146 """
145- sock = socket .socket ()
146- sock .bind (("" , 0 ))
147- port = sock .getsockname ()[1 ] # pyrefly: ignore [unknown-variable-type]
148- sock .close ()
149- _ = requests .get (url = f"http://localhost:{ port } " , timeout = 30 )
147+ _ = requests .get (url = _unused_local_url (), timeout = 30 )
150148
151149
152150@beartype
@@ -2075,27 +2073,19 @@ def test_httpx_unmocked_address_blocked() -> None:
20752073 """``MockVWS`` blocks ``httpx`` requests to non-Vuforia
20762074 addresses.
20772075 """
2078- sock = socket .socket ()
2079- sock .bind (("" , 0 ))
2080- port = sock .getsockname ()[1 ] # pyrefly: ignore [unknown-variable-type]
2081- sock .close ()
20822076 with MockVWS (), pytest .raises (expected_exception = httpx .ConnectError ):
2083- _ = httpx .get (url = f"http://localhost: { port } " , timeout = 30 )
2077+ _ = httpx .get (url = _unused_local_url () , timeout = 30 )
20842078
20852079 @staticmethod
20862080 def test_httpx_real_http () -> None :
20872081 """When ``real_http=True``, ``httpx`` requests to non-Vuforia
20882082 addresses are not blocked.
20892083 """
2090- sock = socket .socket ()
2091- sock .bind (("" , 0 ))
2092- port = sock .getsockname ()[1 ] # pyrefly: ignore [unknown-variable-type]
2093- sock .close ()
20942084 with (
20952085 MockVWS (real_http = True ),
20962086 pytest .raises (expected_exception = httpx .ConnectError ),
20972087 ):
2098- _ = httpx .get (url = f"http://localhost: { port } " , timeout = 30 )
2088+ _ = httpx .get (url = _unused_local_url () , timeout = 30 )
20992089
21002090
21012091class TestHttpx2AlsoIntercepted :
@@ -2138,27 +2128,19 @@ def test_httpx2_unmocked_address_blocked() -> None:
21382128 """``MockVWS`` blocks ``httpx2`` requests to non-Vuforia
21392129 addresses.
21402130 """
2141- sock = socket .socket ()
2142- sock .bind (("" , 0 ))
2143- port = sock .getsockname ()[1 ] # pyrefly: ignore [unknown-variable-type]
2144- sock .close ()
21452131 with MockVWS (), pytest .raises (expected_exception = httpx2 .ConnectError ):
2146- _ = httpx2 .get (url = f"http://localhost: { port } " , timeout = 30 )
2132+ _ = httpx2 .get (url = _unused_local_url () , timeout = 30 )
21472133
21482134 @staticmethod
21492135 def test_httpx2_real_http () -> None :
21502136 """When ``real_http=True``, ``httpx2`` requests to non-Vuforia
21512137 addresses are not blocked.
21522138 """
2153- sock = socket .socket ()
2154- sock .bind (("" , 0 ))
2155- port = sock .getsockname ()[1 ] # pyrefly: ignore [unknown-variable-type]
2156- sock .close ()
21572139 with (
21582140 MockVWS (real_http = True ),
21592141 pytest .raises (expected_exception = httpx2 .ConnectError ),
21602142 ):
2161- _ = httpx2 .get (url = f"http://localhost: { port } " , timeout = 30 )
2143+ _ = httpx2 .get (url = _unused_local_url () , timeout = 30 )
21622144
21632145
21642146class TestModelTargetWebAPI :
0 commit comments