Fix infinite recursion in `PySparkKustoClient.__repr__`, and improve all `__repr__` methods (#169)

This commit is contained in:
Yonatan Most 2021-08-02 12:09:13 +03:00 коммит произвёл GitHub
Родитель 9b5ce67610
Коммит d682465b92
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 24 добавлений и 5 удалений

Просмотреть файл

@ -131,7 +131,7 @@ class PyKustoClient(_ItemFetcher):
self.__client = (self._cached_get_client_for_cluster if use_global_cache else self._get_client_for_cluster)()
def __repr__(self) -> str:
return f'PyKustoClient({self.__cluster_name})'
return f"PyKustoClient('{self.__cluster_name}')"
def to_query_format(self) -> KQL:
return KQL(f'cluster("{self.__cluster_name}")')
@ -241,7 +241,7 @@ class Database(_ItemFetcher):
self._refresh_if_needed()
def __repr__(self) -> str:
return f'{self.__client}.Database({self.__name})'
return f"{self.__client}.Database('{self.__name}')"
def to_query_format(self) -> KQL:
return KQL(f'{self.__client.to_query_format()}.database("{self.__name}")')
@ -336,7 +336,8 @@ class Table(_ItemFetcher):
self._refresh_if_needed()
def __repr__(self) -> str:
return f'{self.__database}.Table({", ".join(self.__tables)})'
table_string = ', '.join(f"'{table}'" for table in self.__tables)
return f'{self.__database}.Table({table_string})'
def _new_item(self, name: str) -> BaseColumn:
return _AnyTypeColumn(name)

Просмотреть файл

@ -57,6 +57,13 @@ class PySparkKustoClient(PyKustoClient):
self.__format = 'com.microsoft.kusto.spark.synapse.datasource'
self.option('spark.synapse.linkedService', self.__linked_service)
def __repr__(self) -> str:
items = [self.__cluster_name]
if self.__linked_service is not None:
items.append(self.__linked_service)
item_string = ', '.join(f"'{item}'" for item in items)
return f"PySparkKustoClient({item_string})"
def refresh_device_auth(self) -> None:
"""
Run device authentication sequence, called in the client constructor. Call this method again if you need to re-authenticate.

Просмотреть файл

@ -134,7 +134,7 @@ class TestClientFetch(TestBase):
self.assertType(db['other_table']['foo'], _AnyTypeColumn)
# Dot notation error
self.assertRaises(
AttributeError("PyKustoClient(test_cluster.kusto.windows.net).Database(test_db) has no attribute 'test_table_1'"),
AttributeError("PyKustoClient('test_cluster.kusto.windows.net').Database('test_db') has no attribute 'test_table_1'"),
lambda: db.test_table_1
)
@ -225,7 +225,7 @@ class TestClientFetch(TestBase):
self.assertEqual(('mock_table', 'other_table'), tuple(client.test_db.get_table_names()))
self.assertEqual(('foo', 'bar', 'baz'), tuple(client.test_db.mock_table.get_columns_names()))
self.assertTrue({'foo', 'bar'} < set(dir(client.test_db.mock_table)))
self.assertEqual('PyKustoClient(test_cluster.kusto.windows.net).Database(test_db).Table(mock_table)', repr(client.test_db.mock_table))
self.assertEqual("PyKustoClient('test_cluster.kusto.windows.net').Database('test_db').Table('mock_table')", repr(client.test_db.mock_table))
def test_autocomplete_with_dot(self):
mock_client = MockKustoClient(

Просмотреть файл

@ -132,6 +132,17 @@ class TestClient(TestBase):
mock_spark_session.read.recorded_options,
)
def test_repr(self):
mock_spark_session = MockSparkSession(pd.DataFrame())
mock_spark_context = MockSparkContext('MOCK_TOKEN')
with patch('pykusto._src.pyspark_client.PySparkKustoClient._PySparkKustoClient__get_spark_session_and_context', lambda s: (mock_spark_session, mock_spark_context)):
device_auth_client = PySparkKustoClient('https://help.kusto.windows.net/', fetch_by_default=False)
linked_service_client = PySparkKustoClient('https://help.kusto.windows.net/', linked_service='MockLinkedKusto', fetch_by_default=False)
self.assertEqual("PySparkKustoClient('https://help.kusto.windows.net/')", repr(device_auth_client))
self.assertEqual("PySparkKustoClient('https://help.kusto.windows.net/', 'MockLinkedKusto')", repr(linked_service_client))
def test_linked_service_with_fetch(self):
rows = (
['test_db', 'mock_table', 'stringField', 'System.String'],