Skip to content

Conversation

@swapnil-veer
Copy link
Contributor

@swapnil-veer swapnil-veer commented Feb 7, 2024

Here I have used datetime module. By which we can take inputs in date format.
image
image

Fixes #3

Summary by CodeRabbit

  • Refactor
    • Improved the handling of date and time for device attributes to enhance accuracy and performance.
    • Updated version from "0.2.0" to "0.3.0" in the application.
  • Tests
    • Added import statement for datetime module in test files.
    • Changed date string comparisons to datetime objects in test files for improved testing accuracy.
    • Added a new test method to handle incorrect datetime formats in test files.

@coderabbitai
Copy link

coderabbitai bot commented Feb 7, 2024

Warning

Rate Limit Exceeded

@Seluj78 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 17 minutes and 10 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 41e0ef6 and 530f211.

Walkthrough

The recent update enhances the ShellHubDevice class in device.py by transitioning temporal data attributes to datetime objects, boosting precision and manageability of timestamps.

Changes

File Change Summary
.../models/device.py Updated last_seen, status_updated_at, and created_at to use datetime objects.

Assessment against linked issues

Objective Addressed Explanation
Convert datetime strings to datetime objects (#3)

🐇✨
Changes in the code, oh how they flow,
Time now dances, in precision we glow.
From strings to dates, a leap so bold,
In datetime's embrace, our moments unfold.
🕰️🌟

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 521a6f2 and e38415e.
Files selected for processing (1)
  • shellhub/models/device.py (3 hunks)
Additional comments: 3
shellhub/models/device.py (3)
  • 44-44: Ensure all external calls to ShellHubDevice's last_seen attribute are updated to handle datetime objects instead of strings.
  • 48-48: Ensure all external calls to ShellHubDevice's status_updated_at attribute are updated to handle datetime objects instead of strings.
  • 49-49: Ensure all external calls to ShellHubDevice's created_at attribute are updated to handle datetime objects instead of strings.

Copy link
Owner

@Seluj78 Seluj78 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment

Along with that you are missing an update on the version found in __init__.py and to update unit tests :)

@pep8speaks
Copy link

pep8speaks commented Feb 7, 2024

Hello @swapnil-veer! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2024-02-09 09:56:59 UTC

Copy link
Owner

@Seluj78 Seluj78 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need to update the version, fix the commit messages, and solve the other comments from coderabbit

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e38415e and 7bf19cb.
Files selected for processing (1)
  • shellhub/models/device.py (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • shellhub/models/device.py

@swapnil-veer
Copy link
Contributor Author

Are that's all with this issue?

@Seluj78
Copy link
Owner

Seluj78 commented Feb 7, 2024

No ! As you can see, the tests aren't passing so you'll need to fix that, as well as add some tests. There is also the review from coderabbit you need to implement

@Seluj78
Copy link
Owner

Seluj78 commented Feb 8, 2024

@swapnil-veer Don't hesitate to let me know if you are lost and tell me where you are lost I'll be happy to guide you. Otherwise I can always edit the PR myself.

@swapnil-veer
Copy link
Contributor Author

swapnil-veer commented Feb 8, 2024

@Seluj78
I just stuck at the test
How to resolve that where my code is breaking at the tests
Can you please guide me

@Seluj78
Copy link
Owner

Seluj78 commented Feb 8, 2024

@swapnil-veer Before fixing the tests, look at the comments @coderabbitai made on your code and implement them (Checking if the value passed is a correct ISO datetime string.)

Once you have done that, you can easily fix the tests (They are failing because they aren't an ISO format, so maybe fromisoformat is incorrect)

@coderabbitai
Copy link

coderabbitai bot commented Feb 8, 2024

@Seluj78 To ensure that the values passed are correct ISO datetime strings, you can use datetime.fromisoformat within a try-except block to catch any ValueError exceptions that occur when the string is not in the correct ISO format. Here's a snippet to illustrate this approach:

from datetime import datetime

def convert_iso_datetime(iso_string):
    try:
        return datetime.fromisoformat(iso_string)
    except ValueError:
        # Handle the error, e.g., by logging or raising a custom exception
        print(f"Invalid ISO datetime string: {iso_string}")
        return None

This method will attempt to convert the string to a datetime object using the ISO format. If the string is not in the correct format, it will catch the ValueError and allow you to handle the error appropriately, such as logging the issue or raising a custom exception. Implementing this validation should help address the issue with the failing tests, assuming they fail due to incorrect datetime string formats.

@swapnil-veer
Copy link
Contributor Author

swapnil-veer commented Feb 8, 2024

@coderabbitai
I guess datetime.fromisoformat has been already checking is string in the iso format or not
image

@coderabbitai
Copy link

coderabbitai bot commented Feb 8, 2024

@swapnil-veer You're correct that datetime.fromisoformat inherently checks if the string is in ISO format and raises a ValueError if it's not. The suggestion to use a try-except block is to ensure your code can gracefully handle such errors, allowing for more controlled error management, such as logging the issue or providing a fallback mechanism, rather than letting the exception terminate the program unexpectedly. This approach is particularly useful in maintaining robustness and reliability in your application, especially when dealing with external inputs that might not always conform to expected formats.

@Seluj78
Copy link
Owner

Seluj78 commented Feb 8, 2024

I think first of all, you need to make sure that the data passed from shellhub is indeed a ISO format, otherwise this won't work at all

@coderabbitai
Copy link

coderabbitai bot commented Feb 8, 2024

@swapnil-veer You're correct that datetime.fromisoformat inherently checks if the string is in ISO 8601 format and raises a ValueError if it's not. The purpose of using a try-except block, as suggested, is not to perform a pre-check but to gracefully handle the situation where the string is not in the correct format. This approach allows for custom error handling, such as logging the error or providing a fallback mechanism, ensuring that your application can respond appropriately without crashing. It's a best practice for robust error handling in scenarios where the input format might vary or be incorrect.

@swapnil-veer
Copy link
Contributor Author

swapnil-veer commented Feb 8, 2024

Hello @Seluj78
Sorry I am unable to understand how to pass the tests of coderabbit
Can you please do it?

@swapnil-veer swapnil-veer deleted the datetime_str_to_datetime_object branch February 8, 2024 14:37
@swapnil-veer swapnil-veer restored the datetime_str_to_datetime_object branch February 8, 2024 14:37
@swapnil-veer swapnil-veer reopened this Feb 8, 2024
@Seluj78 Seluj78 force-pushed the datetime_str_to_datetime_object branch 2 times, most recently from 26e994f to 3e8814f Compare February 8, 2024 14:56
@Seluj78
Copy link
Owner

Seluj78 commented Feb 8, 2024

@swapnil-veer Done :)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between bbce49e and 26e994f.
Files selected for processing (3)
  • shellhub/init.py (1 hunks)
  • shellhub/models/device.py (3 hunks)
  • tests/test_devices.py (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • shellhub/models/device.py
Additional comments: 6
shellhub/__init__.py (1)
  • 2-2: Version update to "0.3.0" correctly reflects the significant changes made, adhering to Semantic Versioning.
tests/test_devices.py (5)
  • 2-2: Import of datetime module is correctly added to support the conversion of datetime strings to datetime objects in tests.
  • 121-121: Conversion of last_seen to a datetime object using datetime.fromisoformat() is correctly implemented.
  • 125-125: Conversion of status_updated_at to a datetime object using datetime.fromisoformat() is correctly implemented.
  • 126-126: Conversion of created_at to a datetime object using datetime.fromisoformat() is correctly implemented.
  • 131-162: The addition of test_get_incorrect_datetime_format correctly tests the SDK's behavior when encountering incorrect datetime formats, ensuring robust error handling.

@Seluj78
Copy link
Owner

Seluj78 commented Feb 8, 2024

Now as you can see the tests fail on Python 3.7, 3.8, 3.9 and 3.10 which means something has changed on the datetime parsing since 3.11 and 3.12 are working.

see https://www.reddit.com/r/Python/comments/163ugsm/psa_as_of_python_311_datetimefromisoformat/

@Seluj78
Copy link
Owner

Seluj78 commented Feb 8, 2024

@swapnil-veer would you like to try and add a fix for this ?

@swapnil-veer
Copy link
Contributor Author

I think not getting close to solution
How you run that multiple test with multiple versions, I am sure that is for our app must be useful from any versions
Can we simulate that test in local
Actually I would like to see your solution !
From my point of we have to ensure that string must be in Iso format nut how we can check it

@swapnil-veer
Copy link
Contributor Author

@Seluj78
For my curiousity
Why we are not using django rest framework in this project
That will help us in various ways
That will connect models with database

@Cpavrai
Copy link
Collaborator

Cpavrai commented Feb 8, 2024

@Seluj78 For my curiousity Why we are not using django rest framework in this project That will help us in various ways That will connect models with database

Yes, why do we not? It would be very powerful! @Seluj78

@Seluj78
Copy link
Owner

Seluj78 commented Feb 8, 2024

From my point of we have to ensure that string must be in Iso format nut how we can check it

I've already implemented that in the code, I've pushed on the branch

How you run that multiple test with multiple versions, I am sure that is for our app must be useful from any versions

This already exists thanks to tox. I recommend you look up on how to use it

Why we are not using django rest framework in this project

Because this is an SDK that connects to an external API made by the @shellhub-io team (https://shellhub.io) so we don't need to connect to their database, just interact with their API

@Seluj78
Copy link
Owner

Seluj78 commented Feb 9, 2024

@swapnil-veer I will take over this PR and fix the last bugs, you'll be able to see in the commits how I did it :)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between bbce49e and 837940e.
Files selected for processing (3)
  • shellhub/init.py (1 hunks)
  • shellhub/models/device.py (3 hunks)
  • tests/test_devices.py (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • shellhub/init.py
  • shellhub/models/device.py
Additional comments: 2
tests/test_devices.py (2)
  • 122-169: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [110-130]

The conversion to datetime objects for last_seen, status_updated_at, and created_at attributes is correctly implemented. This aligns with the PR's objective to enhance date and time handling.

  • 135-166: The implementation of test_get_incorrect_datetime_format correctly tests the handling of incorrect datetime formats by expecting a ShellHubApiError. This addition enhances the robustness of the SDK by ensuring proper error handling.

@Seluj78 Seluj78 force-pushed the datetime_str_to_datetime_object branch from 837940e to 93cb35a Compare February 9, 2024 09:47
@codecov
Copy link

codecov bot commented Feb 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (41e0ef6) 81.27% compared to head (530f211) 82.18%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #10      +/-   ##
==========================================
+ Coverage   81.27%   82.18%   +0.90%     
==========================================
  Files           4        4              
  Lines         235      247      +12     
  Branches       28       30       +2     
==========================================
+ Hits          191      203      +12     
  Misses         36       36              
  Partials        8        8              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Seluj78 Seluj78 force-pushed the datetime_str_to_datetime_object branch from 93cb35a to 12abdeb Compare February 9, 2024 09:51
Fix issue at status_updated_at

Fix: PEP 8 correction

Fix: Version correction

Added check for datetime string format + tests
@Seluj78 Seluj78 force-pushed the datetime_str_to_datetime_object branch from 12abdeb to 06a3355 Compare February 9, 2024 09:55
@Seluj78 Seluj78 force-pushed the datetime_str_to_datetime_object branch from 06a3355 to 530f211 Compare February 9, 2024 09:56
@Seluj78 Seluj78 merged commit e9080a3 into Seluj78:main Feb 9, 2024
@swapnil-veer swapnil-veer deleted the datetime_str_to_datetime_object branch February 9, 2024 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convert datetime strings to datetime objects

4 participants