Skip to content

Commit b050014

Browse files
chore: Improve typeguard validator naming (#2666)
Co-authored-by: Håkon V. Treider <haakonvt@gmail.com>
1 parent a233d8a commit b050014

8 files changed

Lines changed: 33 additions & 26 deletions

File tree

cognite/client/_api/datapoint_tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
DatapointsArray,
3535
DatapointsQuery,
3636
)
37-
from cognite.client.utils._auxiliary import exactly_one_is_not_none, is_finite, is_unlimited
37+
from cognite.client.utils._auxiliary import exactly_one_is_not_none, is_non_negative_int, is_unlimited
3838
from cognite.client.utils._datapoints import (
3939
AggregateDatapoints,
4040
DatapointsRaw,
@@ -300,7 +300,7 @@ def _verify_and_convert_granularity(granularity: str | None) -> tuple[str | None
300300
def _verify_and_convert_limit(limit: int | None) -> int | None:
301301
if is_unlimited(limit):
302302
return None
303-
elif is_finite(limit): # limit=0 is accepted by the API
303+
elif is_non_negative_int(limit): # limit=0 is accepted by the API
304304
try:
305305
# We don't want weird stuff like numpy dtypes etc:
306306
return int(limit)

cognite/client/_api/datapoints.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@
5252
from cognite.client.utils._auxiliary import (
5353
exactly_one_is_not_none,
5454
find_duplicates,
55-
is_finite,
56-
is_positive,
55+
is_positive_int,
5756
split_into_chunks,
5857
split_into_n_parts,
5958
unpack_items,
@@ -673,15 +672,15 @@ async def __call__(
673672
# get 10k/100k datapoints per request. Thus, we round up the given chunk size to the nearest integer multiple of 100k,
674673
# then subdivide and yield client-side (we use the raw limit also when dealing with aggregates):
675674
request_limit = self._DPS_LIMIT_RAW * math.ceil(chunk_size_datapoints / self._DPS_LIMIT_RAW)
676-
if not is_finite(chunk_size_datapoints) or (
675+
if not is_positive_int(chunk_size_datapoints) or (
677676
chunk_size_datapoints != request_limit and request_limit % chunk_size_datapoints
678677
):
679678
raise ValueError(
680679
"The 'chunk_size_datapoints' must be a positive integer that evenly divides 100k OR an integer multiple of 100k "
681680
f"(to ensure efficient API usage), not {chunk_size_datapoints}."
682681
)
683682

684-
if not (chunk_size_time_series is None or is_positive(chunk_size_time_series)):
683+
if not (chunk_size_time_series is None or is_positive_int(chunk_size_time_series)):
685684
raise ValueError(
686685
f"'chunk_size_time_series' must be a positive integer or None, not {chunk_size_time_series}"
687686
)

cognite/client/_api/raw/rows.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from cognite.client.utils._auxiliary import (
1313
drop_none_values,
1414
find_duplicates,
15-
is_finite,
15+
is_non_negative_int,
1616
is_unlimited,
1717
split_into_chunks,
1818
unpack_items,
@@ -179,7 +179,7 @@ async def _list_generator_concurrent(
179179

180180
concurrency_limit = global_config.concurrency_settings.raw.read
181181
partitions = min(partitions, concurrency_limit)
182-
if is_finite(limit):
182+
if is_non_negative_int(limit):
183183
partitions = min(partitions, concurrency_limit, math.ceil(limit / 20_000))
184184
if chunk_size is not None and limit < chunk_size:
185185
raise ValueError(f"chunk_size ({chunk_size}) should be much smaller than limit ({limit})")
@@ -226,7 +226,7 @@ async def _fetch_and_enqueue(iterator: AsyncIterator[RowList]) -> None:
226226
await asyncio.sleep(0.5)
227227
continue
228228

229-
if not is_finite(limit):
229+
if not is_non_negative_int(limit):
230230
yield chunk
231231
continue
232232

cognite/client/_sync_api/datapoints.py

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cognite/client/_sync_api/raw/rows.py

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cognite/client/config.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from cognite.client._version import __api_subversion__
1010
from cognite.client.credentials import CredentialProvider
11-
from cognite.client.utils._auxiliary import is_finite, is_positive, load_resource_to_dict
11+
from cognite.client.utils._auxiliary import is_non_negative_int, is_positive_int, load_resource_to_dict
1212
from cognite.client.utils._concurrency import ConcurrencySettings
1313
from cognite.client.utils._importing import local_import
1414

@@ -78,21 +78,21 @@ def __init__(self) -> None:
7878
self.file_upload_chunk_size: int | None = None
7979
self.silence_feature_preview_warnings: bool = False
8080

81-
def __setattr__(self, name: str, value: Any) -> None:
81+
def __setattr__(self, name: str, val: Any) -> None:
8282
# Why __setattr__ instead of just more use of @property? It is to avoid breaking a bunch of existing
8383
# inspection code (which would then need special handling). Setting global config options is a rare
8484
# one-off type event, so overhead is no issue.
8585
match name:
86-
case "max_retries" | "max_retries_connect" | "max_retry_backoff" if not is_finite(value):
87-
raise ValueError(f"{name} must be a non-negative integer, got {value!r}")
86+
case "max_retries" | "max_retries_connect" | "max_retry_backoff" if not is_non_negative_int(val):
87+
raise ValueError(f"{name} must be a non-negative integer, got {val!r}")
8888

89-
case "max_connection_pool_size" if not is_positive(value):
90-
raise ValueError(f"max_connection_pool_size must be a positive integer, got {value!r}")
89+
case "max_connection_pool_size" if not is_positive_int(val):
90+
raise ValueError(f"max_connection_pool_size must be a positive integer, got {val!r}")
9191

92-
case "file_download_chunk_size" | "file_upload_chunk_size" if value is not None and not is_positive(value):
93-
raise ValueError(f"{name} must be a positive integer or None, got {value!r}")
92+
case "file_download_chunk_size" | "file_upload_chunk_size" if val is not None and not is_positive_int(val):
93+
raise ValueError(f"{name} must be a positive integer or None, got {val!r}")
9494

95-
super().__setattr__(name, value)
95+
super().__setattr__(name, val)
9696

9797
@property
9898
def max_workers(self) -> int:
@@ -219,7 +219,9 @@ def __init__(
219219
self.timeout = timeout or 60
220220
self.file_transfer_timeout = file_transfer_timeout or 600
221221
if debug:
222-
self.debug = True
222+
from cognite.client.utils._logging import _configure_logger_for_debug_mode
223+
224+
_configure_logger_for_debug_mode()
223225
self._validate_config()
224226

225227
if not global_config.disable_pypi_version_check:

cognite/client/data_classes/data_modeling/data_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from cognite.client.data_classes._base import CogniteResource, UnknownCogniteResource
1111
from cognite.client.data_classes.data_modeling.ids import ContainerId, NodeId
12-
from cognite.client.utils._auxiliary import is_positive, rename_and_exclude_keys
12+
from cognite.client.utils._auxiliary import is_positive_int, rename_and_exclude_keys
1313
from cognite.client.utils._text import convert_all_keys_recursive
1414

1515
logger = logging.getLogger(__name__)
@@ -141,7 +141,7 @@ class ListablePropertyType(PropertyType, ABC):
141141
max_list_size: int | None = None
142142

143143
def __post_init__(self) -> None:
144-
if is_positive(self.max_list_size) and not self.is_list:
144+
if is_positive_int(self.max_list_size) and not self.is_list:
145145
raise ValueError("is_list must be True if max_list_size is set")
146146

147147

cognite/client/utils/_auxiliary.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ def no_op(x: T) -> T:
4848
return x
4949

5050

51-
def is_finite(limit: Any) -> TypeGuard[int]:
51+
def is_non_negative_int(limit: Any) -> TypeGuard[int]:
5252
return isinstance(limit, int) and limit >= 0
5353

5454

55-
def is_positive(limit: Any) -> TypeGuard[int]:
55+
def is_positive_int(limit: Any) -> TypeGuard[int]:
5656
return isinstance(limit, int) and limit > 0
5757

5858

0 commit comments

Comments
 (0)