Skip to content

Commit

Permalink
Clean up type hints in tests
Browse files Browse the repository at this point in the history
Many tests were completely untyped, many had incorrect typing.

This doesn't fix everything, there's still a lot of type errors in the
tests, but it is an improvement. I'll maybe make a few more PRs cleaning
up a bit more in the tests, but some of it is also fine to leave as it
might get replaced altogether in the next major release.
  • Loading branch information
mathialo committed Dec 12, 2024
1 parent 62c5b5a commit fcff685
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 168 deletions.
12 changes: 6 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from dataclasses import dataclass
from enum import Enum
from typing import Generator, List, Optional, Tuple
from typing import Generator

import pytest

Expand All @@ -27,16 +27,16 @@ class ETestType(Enum):
@dataclass
class ParamTest:
test_type: ETestType
external_ids: Optional[List[str]] = None
database_name: Optional[str] = None
table_name: Optional[str] = None
space: Optional[str] = None
external_ids: list[str] | None = None
database_name: str | None = None
table_name: str | None = None
space: str | None = None


@pytest.fixture
def set_upload_test(
set_test_parameters: ParamTest, set_client: CogniteClient
) -> Generator[Tuple[CogniteClient, ParamTest], None, None]:
) -> Generator[tuple[CogniteClient, ParamTest], None, None]:
client = set_client
test_parameter = set_test_parameters
clean_test(client, test_parameter)
Expand Down
3 changes: 1 addition & 2 deletions tests/tests_integration/test_assets_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import os
import random
from typing import Tuple

import pytest

Expand All @@ -37,7 +36,7 @@ def set_test_parameters() -> ParamTest:


@pytest.mark.parametrize("functions_runtime", ["true", "false"])
def test_assets_upload_queue_upsert(set_upload_test: Tuple[CogniteClient, ParamTest], functions_runtime: str):
def test_assets_upload_queue_upsert(set_upload_test: tuple[CogniteClient, ParamTest], functions_runtime: str) -> None:
os.environ["COGNITE_FUNCTION_RUNTIME"] = functions_runtime
client, test_parameter = set_upload_test
queue = AssetUploadQueue(cdf_client=client)
Expand Down
4 changes: 2 additions & 2 deletions tests/tests_integration/test_configtools_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DummyConfig(BaseConfig):
dummy_secret: str


def test_dataset_resolve(set_client: CogniteClient):
def test_dataset_resolve(set_client: CogniteClient) -> None:
client = set_client
data_set_name: str = "Extractor Utils Test Data Set"
data_set_extid: str = "extractorUtils-testdataset"
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_dataset_resolve(set_client: CogniteClient):
assert config2.cognite.get_data_set(client).name == data_set_name


def test_keyvault_and_remote(set_client: CogniteClient):
def test_keyvault_and_remote(set_client: CogniteClient) -> None:
# Set up extraction pipeline
data_set_name: str = "Extractor Utils Test Data Set"
data_set_extid: str = "extractorUtils-testdataset"
Expand Down
16 changes: 8 additions & 8 deletions tests/tests_integration/test_file_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import pathlib
import random
import time
from typing import Callable, Optional, Tuple
from typing import Callable

import pytest

Expand Down Expand Up @@ -53,7 +53,7 @@ def set_test_parameters() -> ParamTest:


def await_is_uploaded_status(
client: CogniteClient, external_id: Optional[str] = None, instance_id: Optional[NodeId] = None
client: CogniteClient, external_id: str | None = None, instance_id: NodeId | None = None
) -> None:
for _ in range(10):
if external_id is not None:
Expand All @@ -68,7 +68,7 @@ def await_is_uploaded_status(


@pytest.mark.parametrize("functions_runtime", ["true", "false"])
def test_file_upload_queue(set_upload_test: Tuple[CogniteClient, ParamTest], functions_runtime: str) -> None:
def test_file_upload_queue(set_upload_test: tuple[CogniteClient, ParamTest], functions_runtime: str) -> None:
os.environ["COGNITE_FUNCTION_RUNTIME"] = functions_runtime
client, test_parameter = set_upload_test
queue = FileUploadQueue(cdf_client=client, overwrite_existing=True, max_queue_size=2)
Expand Down Expand Up @@ -145,7 +145,7 @@ def test_file_upload_queue(set_upload_test: Tuple[CogniteClient, ParamTest], fun


@pytest.mark.parametrize("functions_runtime", ["true", "false"])
def test_bytes_upload_queue(set_upload_test: Tuple[CogniteClient, ParamTest], functions_runtime: str) -> None:
def test_bytes_upload_queue(set_upload_test: tuple[CogniteClient, ParamTest], functions_runtime: str) -> None:
os.environ["COGNITE_FUNCTION_RUNTIME"] = functions_runtime
client, test_parameter = set_upload_test
queue = BytesUploadQueue(cdf_client=client, overwrite_existing=True, max_queue_size=1)
Expand Down Expand Up @@ -193,7 +193,7 @@ def test_bytes_upload_queue(set_upload_test: Tuple[CogniteClient, ParamTest], fu


@pytest.mark.parametrize("functions_runtime", ["true", "false"])
def test_big_file_upload_queue(set_upload_test: Tuple[CogniteClient, ParamTest], functions_runtime: str) -> None:
def test_big_file_upload_queue(set_upload_test: tuple[CogniteClient, ParamTest], functions_runtime: str) -> None:
os.environ["COGNITE_FUNCTION_RUNTIME"] = functions_runtime
client, test_parameter = set_upload_test
queue = BytesUploadQueue(cdf_client=client, overwrite_existing=True, max_queue_size=1)
Expand Down Expand Up @@ -228,7 +228,7 @@ def test_big_file_upload_queue(set_upload_test: Tuple[CogniteClient, ParamTest],
assert len(bigfile2) == 10_000_000


def test_big_file_stream(set_upload_test: Tuple[CogniteClient, ParamTest]) -> None:
def test_big_file_stream(set_upload_test: tuple[CogniteClient, ParamTest]) -> None:
client, test_parameter = set_upload_test
queue = IOFileUploadQueue(cdf_client=client, overwrite_existing=True, max_queue_size=1)
queue.max_file_chunk_size = 6_000_000
Expand All @@ -238,7 +238,7 @@ def test_big_file_stream(set_upload_test: Tuple[CogniteClient, ParamTest]) -> No

class BufferedReadWithLength(io.BufferedReader):
def __init__(
self, raw: io.RawIOBase, buffer_size: int, len: int, on_close: Optional[Callable[[], None]] = None
self, raw: io.RawIOBase, buffer_size: int, len: int, on_close: Callable[[], None] | None = None
) -> None:
super().__init__(raw, buffer_size)
# Do not remove even if it appears to be unused. :P
Expand Down Expand Up @@ -279,7 +279,7 @@ def read_file() -> BufferedReadWithLength:
assert len(bigfile2) == 10_000_000


def test_update_files(set_upload_test: Tuple[CogniteClient, ParamTest]) -> None:
def test_update_files(set_upload_test: tuple[CogniteClient, ParamTest]) -> None:
client, test_parameter = set_upload_test
queue = BytesUploadQueue(cdf_client=client, overwrite_existing=True, max_queue_size=1)

Expand Down
3 changes: 1 addition & 2 deletions tests/tests_integration/test_raw_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import os
import random
import time
from typing import Tuple

import pytest

Expand All @@ -35,7 +34,7 @@ def set_test_parameters() -> ParamTest:


@pytest.mark.parametrize("functions_runtime", ["true", "false"])
def test_raw_upload_queue(set_upload_test: Tuple[CogniteClient, ParamTest], functions_runtime: str):
def test_raw_upload_queue(set_upload_test: tuple[CogniteClient, ParamTest], functions_runtime: str) -> None:
os.environ["COGNITE_FUNCTION_RUNTIME"] = functions_runtime
client, test_parameter = set_upload_test
queue = RawUploadQueue(cdf_client=client, max_queue_size=500)
Expand Down
12 changes: 6 additions & 6 deletions tests/tests_integration/test_time_series_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import string
import time
from datetime import datetime, timezone
from typing import Tuple
from typing import Any

import pytest

Expand All @@ -38,7 +38,7 @@ def set_test_parameters() -> ParamTest:
return test_parameter


def test_time_series_upload_queue1(set_upload_test: Tuple[CogniteClient, ParamTest]) -> None:
def test_time_series_upload_queue1(set_upload_test: tuple[CogniteClient, ParamTest]) -> None:
client, test_parameter = set_upload_test
created = client.time_series.create(
[
Expand All @@ -49,7 +49,7 @@ def test_time_series_upload_queue1(set_upload_test: Tuple[CogniteClient, ParamTe

last_point = {"timestamp": 0}

def store_latest(points):
def store_latest(points: Any) -> None:
last_point["timestamp"] = max(last_point["timestamp"], *[ts["datapoints"][-1][0] for ts in points])

queue = TimeSeriesUploadQueue(cdf_client=client, post_upload_function=store_latest, max_upload_interval=1)
Expand Down Expand Up @@ -82,7 +82,7 @@ def store_latest(points):
queue.stop()


def test_time_series_upload_queue2(set_upload_test: Tuple[CogniteClient, ParamTest]) -> None:
def test_time_series_upload_queue2(set_upload_test: tuple[CogniteClient, ParamTest]) -> None:
client, test_parameter = set_upload_test
client.time_series.create(TimeSeries(external_id=test_parameter.external_ids[0]))

Expand All @@ -109,7 +109,7 @@ def test_time_series_upload_queue2(set_upload_test: Tuple[CogniteClient, ParamTe
queue.stop()


def test_time_series_upload_queue_create_missing(set_upload_test: Tuple[CogniteClient, ParamTest]) -> None:
def test_time_series_upload_queue_create_missing(set_upload_test: tuple[CogniteClient, ParamTest]) -> None:
client, test_parameter = set_upload_test

queue = TimeSeriesUploadQueue(cdf_client=client, create_missing=True)
Expand Down Expand Up @@ -144,7 +144,7 @@ def test_time_series_upload_queue_create_missing(set_upload_test: Tuple[CogniteC
queue.stop()


def test_time_seires_with_status(set_upload_test: Tuple[CogniteClient, ParamTest]) -> None:
def test_time_seires_with_status(set_upload_test: tuple[CogniteClient, ParamTest]) -> None:
client, test_parameter = set_upload_test

queue = TimeSeriesUploadQueue(cdf_client=client, create_missing=True)
Expand Down
12 changes: 7 additions & 5 deletions tests/tests_unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from dataclasses import dataclass, field
from typing import Callable
from unittest.mock import patch

import pytest

from cognite.client import CogniteClient
from cognite.extractorutils import Extractor
from cognite.extractorutils.configtools import BaseConfig, StateStoreConfig
from cognite.extractorutils.statestore import LocalStateStore, NoStateStore
Expand All @@ -42,13 +44,13 @@ class ConfigWithoutStates(BaseConfig):
source: SourceConfig


def test_load_config():
def test_load_config() -> None:
e1 = Extractor(name="my_extractor1", description="description", config_class=ConfigWithStates)
e1._initial_load_config("tests/tests_unit/dummyconfig.yaml")
assert isinstance(e1.config, ConfigWithStates)


def test_load_config_keyvault():
def test_load_config_keyvault() -> None:
e7 = Extractor(name="my_extractor7", description="description", config_class=ConfigWithoutStates)
e7._initial_load_config("tests/tests_unit/dummyconfig_keyvault.yaml")

Expand All @@ -58,7 +60,7 @@ def test_load_config_keyvault():


@patch("cognite.client.CogniteClient")
def test_load_state_store(get_client_mock):
def test_load_state_store(get_client_mock: Callable[[], CogniteClient]) -> None:
e2 = Extractor(name="my_extractor2", description="description", config_class=ConfigWithStates)
e2._initial_load_config("tests/tests_unit/dummyconfig.yaml")
e2.cognite_client = get_client_mock()
Expand Down Expand Up @@ -95,7 +97,7 @@ def test_load_state_store(get_client_mock):


@pytest.mark.order(1)
def test_config_getter():
def test_config_getter() -> None:
with pytest.raises(ValueError):
Extractor.get_current_config()

Expand All @@ -110,7 +112,7 @@ def test_config_getter():


@pytest.mark.order(2)
def test_state_store_getter():
def test_state_store_getter() -> None:
with pytest.raises(ValueError):
Extractor.get_current_statestore()

Expand Down
Loading

0 comments on commit fcff685

Please sign in to comment.