Skip to content

Commit a03205a

Browse files
ai: apply changes for #914 (1 review thread)
Addresses: - #3799812091 at src/databricks/sql/backend/kernel/auth_bridge.py:258 Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>
1 parent 5409fae commit a03205a

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/databricks/sql/backend/kernel/auth_bridge.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def kernel_auth_kwargs(
273273
"auth_type": "oauth-u2m",
274274
"client_id": client_id or PYSQL_OAUTH_CLIENT_ID,
275275
"redirect_port": (
276-
int(redirect_port)
276+
_coerce_redirect_port(redirect_port)
277277
if client_id and redirect_port is not None
278278
else PYSQL_OAUTH_REDIRECT_PORT_RANGE[0]
279279
),
@@ -312,6 +312,23 @@ def kernel_auth_kwargs(
312312
)
313313

314314

315+
def _coerce_redirect_port(redirect_port: Any) -> int:
316+
"""Coerce an ``oauth_redirect_port`` value (which may arrive as a string,
317+
e.g. from a DSN) to an int.
318+
319+
A non-numeric value is a caller error; surface it as a PEP 249
320+
``ProgrammingError`` (as ``_normalize_scopes`` does for malformed
321+
``oauth_scopes``) rather than a bare ``ValueError``, so callers get a
322+
consistent, actionable exception type for garbled input."""
323+
try:
324+
return int(redirect_port)
325+
except (TypeError, ValueError):
326+
raise ProgrammingError(
327+
f"oauth_redirect_port must be an integer (or a string parseable as "
328+
f"one), got {redirect_port!r}."
329+
)
330+
331+
315332
def _normalize_scopes(scopes: Any) -> Optional[list]:
316333
"""Normalise an ``oauth_scopes`` value to a list of strings, or
317334
``None`` to let the kernel apply its defaults.

tests/unit/test_kernel_auth_bridge.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,21 @@ def test_u2m_redirect_port_coerced_to_int(self):
346346
assert kwargs["redirect_port"] == 8021
347347
assert isinstance(kwargs["redirect_port"], int)
348348

349+
def test_u2m_redirect_port_non_numeric_raises_programming_error(self):
350+
# A garbled oauth_redirect_port is a caller error of the same class
351+
# as a malformed oauth_scopes, so it must surface as a PEP 249
352+
# ProgrammingError (not a bare ValueError from int()) for a
353+
# consistent, actionable exception type.
354+
with pytest.raises(ProgrammingError, match="oauth_redirect_port must be"):
355+
kernel_auth_kwargs(
356+
_FakeOAuthProvider(),
357+
{
358+
"auth_type": "databricks-oauth",
359+
"oauth_client_id": "custom-client",
360+
"oauth_redirect_port": "not-a-port",
361+
},
362+
)
363+
349364
def test_u2m_redirect_port_ignored_without_client_id(self):
350365
# A bare oauth_redirect_port (no explicit client_id) must NOT be
351366
# forwarded: it would be paired with the default databricks-sql-python

0 commit comments

Comments
 (0)