cognitedata / cognite-sdk-python

Cognite Python SDK
https://cognite-sdk-python.readthedocs-hosted.com/
Apache License 2.0
77 stars 27 forks source link

TypeError: '<=' not supported between instances of 'int' and 'str' in /cognite/client/utils/_time.py", line 97, in ms_to_datetime in if not (MIN_TIMESTAMP_MS <= ms <= MAX_TIMESTAMP_MS) #1623

Closed andriangashper closed 5 months ago

andriangashper commented 5 months ago

System information (please complete the following information):

Describe the bug In cognite/client/utils/_time.py One needs to make sure that item[k] is integer. Otherwise In a Type error in the ms_to_datetime funciton (see error at the end)

def _convert_and_isoformat_time_attrs_in_dict(item: dict) -> dict:
    for k in TIME_ATTRIBUTES.intersection(item):
        try:
            item[k] = ms_to_datetime(item[k]).isoformat(sep=" ", timespec="milliseconds")
        except ValueError:
            pass
    return item

To Reproduce Runnable code reproducing the error.

"""
Test creating a data model in akerbp-sandbox
"""

# Authenticate with akerbp-sandbox
from cognite.client import CogniteClient, ClientConfig
from cognite.client.credentials import OAuthInteractive
from variables import (
    TENANT_ID,
    CDF_CLIENT_ID,
    CDF_CLIENT_NAME,
    CDF_BASE_URL,
)

# Create OAuth credentials
credentials = OAuthInteractive(
    authority_url=f"https://login.microsoftonline.com/{TENANT_ID}",
    client_id=CDF_CLIENT_ID,  # type: ignore
    scopes=[f"{CDF_BASE_URL}/.default"],
)

# Create client configurations
configurations = ClientConfig(
    client_name=CDF_CLIENT_NAME,  # type: ignore
    base_url=CDF_BASE_URL,
    project="akerbp-sandbox",  # type: ignore
    credentials=credentials,
)

# Initialize client
client = CogniteClient(configurations)

# Create a data model
from cognite.client.data_classes.data_modeling import DataModelApply
from cognite.client.data_classes.data_modeling import SpaceApply
from cognite.client.exceptions import CogniteAPIError

def apply_space(client: CogniteClient):
    space = client.data_modeling.spaces.apply(
        SpaceApply(
            space="test_sp_aad_cdf_mapping",
            name="test space for AAD CDF mapping",
        )
    )
    print(space)

def apply_data_model(client: CogniteClient):
    dm = client.data_modeling.data_models.apply(
        DataModelApply(
            space="test_sp_aad_cdf_mapping",
            external_id="test_dm_aad_cdf_mapping",
            version="v1",
            description="mapping between AAD members and CDF resources",
            name="test AAD CDF mapping",
        )
    )
    print(dm)

def apply_dml_to_data_model(client: CogniteClient):
    dm = client.data_modeling.graphql.apply_dml(
        id=("test_sp_aad_cdf_mapping", "test_dm_aad_cdf_mapping", "v1"),
        dml="""
type Group {
    id: String!
    name: String!
    members: [Member]
} 
type Member {
    id: String!
    name: String!
    type: String!
    groups: [Group]
}   
"""
)
    print("Done!")

def main() -> None:
    try:
        apply_data_model(client)
    except CogniteAPIError:
        apply_space(client)
        apply_data_model(client)

    apply_dml_to_data_model(client)

if __name__ == "__main__":
    main()

Expected behavior

{
    "space": "test_sp_aad_cdf_mapping",
    "external_id": "test_dm_aad_cdf_mapping",
    "version": "v1",
    "created_time": "2024-02-15T13:18:18.986Z",
    "last_updated_time": "2024-02-16T08:55:53.150Z"
}

Screenshots

Traceback (most recent call last):
  File "/Users/andrian.gasper@cognitedata.com/Cognite/CDF-Aker-BP-Project/Extract-info-AAD/remote_repo/inso-dataops-incubator/integrating-aad-and-cdf-group-data/src/test.py", line 144, in <module>
    main()
  File "/Users/andrian.gasper@cognitedata.com/Cognite/CDF-Aker-BP-Project/Extract-info-AAD/remote_repo/inso-dataops-incubator/integrating-aad-and-cdf-group-data/src/test.py", line 140, in main
    apply_dml_to_data_model(client)
  File "/Users/andrian.gasper@cognitedata.com/Cognite/CDF-Aker-BP-Project/Extract-info-AAD/remote_repo/inso-dataops-incubator/integrating-aad-and-cdf-group-data/src/test.py", line 130, in apply_dml_to_data_model
    print(dm)
  File "/Users/andrian.gasper@cognitedata.com/Cognite/CDF-Aker-BP-Project/Extract-info-AAD/remote_repo/inso-dataops-incubator/integrating-aad-and-cdf-group-data/.venv/lib/python3.11/site-packages/cognite/client/data_classes/_base.py", line 126, in __str__
    item = convert_and_isoformat_time_attrs(self.dump(camel_case=False))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrian.gasper@cognitedata.com/Cognite/CDF-Aker-BP-Project/Extract-info-AAD/remote_repo/inso-dataops-incubator/integrating-aad-and-cdf-group-data/.venv/lib/python3.11/site-packages/cognite/client/utils/_time.py", line 209, in convert_and_isoformat_time_attrs
    return _convert_and_isoformat_time_attrs_in_dict(item)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrian.gasper@cognitedata.com/Cognite/CDF-Aker-BP-Project/Extract-info-AAD/remote_repo/inso-dataops-incubator/integrating-aad-and-cdf-group-data/.venv/lib/python3.11/site-packages/cognite/client/utils/_time.py", line 191, in _convert_and_isoformat_time_attrs_in_dict
    item[k] = ms_to_datetime(item[k]).isoformat(sep=" ", timespec="milliseconds")
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andrian.gasper@cognitedata.com/Cognite/CDF-Aker-BP-Project/Extract-info-AAD/remote_repo/inso-dataops-incubator/integrating-aad-and-cdf-group-data/.venv/lib/python3.11/site-packages/cognite/client/utils/_time.py", line 97, in ms_to_datetime
    if not (MIN_TIMESTAMP_MS <= ms <= MAX_TIMESTAMP_MS):
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '<=' not supported between instances of 'int' and 'str'
haakonvt commented 5 months ago

Hey @andriangashper, thanks for reporting! 😄

I just ran your steps above to reproduce but didn't get any errors. I then looked at your stack trace which says while in main(), it tried to run apply_dml_to_data_model(client) and failed at step print(dm) (which seems to be missing in your example). With this change, when I try to print the same object as you, I now also get the same error!

I've made a fix and linked the PR above. I will let you know once the fix is live, probably Monday morning!

haakonvt commented 5 months ago

Just kidding, here's the link (🤦 ) : https://github.com/cognitedata/cognite-sdk-python/pull/1628

haakonvt commented 5 months ago

Fixed in 7.20.1 @andriangashper

andriangashper commented 5 months ago

@haakonvt Great. Thanks!