Skip to content

Commit

Permalink
Improve Identifier validation (#1705)
Browse files Browse the repository at this point in the history
  • Loading branch information
erlendvollset committed Apr 4, 2024
1 parent e6c7f1c commit 1075699
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [7.32.5] - 2024-04-4
### Improved
- Type validation of identifiers

## [7.32.4] - 2024-03-28
### Fixed
- Several methods for `DatapointsArray` that previously failed for string datapoints due to bad handling
Expand Down
2 changes: 1 addition & 1 deletion cognite/client/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations

__version__ = "7.32.4"
__version__ = "7.32.5"
__api_subversion__ = "20230101"
2 changes: 2 additions & 0 deletions cognite/client/utils/_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def as_primitive(self) -> str | int: ...

class Identifier(Generic[T_ID]):
def __init__(self, value: T_ID) -> None:
if not isinstance(value, (int, str)):
raise TypeError(f"Expected id/external_id to be of type int or str, got {value} of type {type(id)}")
self.__value: T_ID = value

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "cognite-sdk"

version = "7.32.4"
version = "7.32.5"
description = "Cognite Python SDK"
readme = "README.md"
documentation = "https://cognite-sdk-python.readthedocs-hosted.com"
Expand Down
11 changes: 11 additions & 0 deletions tests/tests_unit/test_utils/test_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ def test_of_either__bad_input(self, id, external_id, err_msg):
with pytest.raises(ValueError, match=err_msg):
Identifier.of_either(id, external_id)

def test_handles_id_type_correctly(self):
# int is ok
Identifier(1)

# string is ok
Identifier("abc")

# anything else is not ok
with pytest.raises(TypeError, match="Expected id/external_id to be of type int or str"):
Identifier(object())


class TestIdentifierSequence:
@pytest.mark.parametrize(
Expand Down

0 comments on commit 1075699

Please sign in to comment.