Skip to content

Commit 741238b

Browse files
authored
Properly parse ISO date strings (#473)
Pre Python 3.11 the `datetime.fromisoformat` isn't a generic date time parser and can only be used to parse ISO dates generated by the python `datatime` module. This PR normalized the date string the same way the google oauth2 library does by discarding the timezone and millisecond part of the date string. ## Changes Make date parsing for tokens from the CLI more robust ## Tests * Added unit test * Manually tested with CLI auth provider
1 parent 5955333 commit 741238b

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

databricks/sdk/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ def __init__(self, cmd: List[str], token_type_field: str, access_token_field: st
346346

347347
@staticmethod
348348
def _parse_expiry(expiry: str) -> datetime:
349-
for fmt in ("%Y-%m-%d %H:%M:%S.%f", "%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S.%f%z"):
349+
expiry = expiry.rstrip("Z").split(".")[0]
350+
for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S"):
350351
try:
351352
return datetime.strptime(expiry, fmt)
352353
except ValueError as e:

tests/test_core.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import random
77
import string
88
import typing
9+
from datetime import datetime
910
from http.server import BaseHTTPRequestHandler
1011
from typing import Iterator, List
1112

@@ -14,10 +15,10 @@
1415

1516
from databricks.sdk import WorkspaceClient
1617
from databricks.sdk.azure import ENVIRONMENTS, AzureEnvironment
17-
from databricks.sdk.core import (ApiClient, Config, CredentialsProvider,
18-
DatabricksCliTokenSource, DatabricksError,
19-
HeaderFactory, StreamingResponse,
20-
databricks_cli)
18+
from databricks.sdk.core import (ApiClient, CliTokenSource, Config,
19+
CredentialsProvider, DatabricksCliTokenSource,
20+
DatabricksError, HeaderFactory,
21+
StreamingResponse, databricks_cli)
2122
from databricks.sdk.service.catalog import PermissionsChange
2223
from databricks.sdk.service.iam import AccessControlRequest
2324
from databricks.sdk.version import __version__
@@ -52,6 +53,14 @@ def test_databricks_cli_token_source_not_installed(config, monkeypatch):
5253
DatabricksCliTokenSource(config)
5354

5455

56+
@pytest.mark.parametrize("date_string,expected",
57+
[("2023-12-01T15:19:48.007742617Z", datetime(2023, 12, 1, 15, 19, 48)),
58+
("2023-12-05T15:59:01.40081+11:00", datetime(2023, 12, 5, 15, 59, 1)),
59+
("2023-12-06 10:06:05", datetime(2023, 12, 6, 10, 6, 5))])
60+
def test_databricks_cli_token_parse_expiry(date_string, expected):
61+
assert CliTokenSource._parse_expiry(date_string) == expected
62+
63+
5564
def write_small_dummy_executable(path: pathlib.Path):
5665
cli = path.joinpath('databricks')
5766
cli.write_text('#!/bin/sh\necho "hello world"\n')

0 commit comments

Comments
 (0)