-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
473c682
commit ac24b76
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"title": "datetime.datetime objects should not be compared with datetime.date objects", | ||
"type": "BUG", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-6899", | ||
"sqKey": "S6899", | ||
"scope": "All", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "unknown", | ||
"code": { | ||
"impacts": { | ||
"RELIABILITY": "HIGH" | ||
}, | ||
"attribute": "LOGICAL" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
This rule raises an issue when trying to perform comparison or arithmetic operations between `datetime.date` and `datetime.datetime` objects. | ||
|
||
== Why is this an issue? | ||
Despite the fact that the `datetime.datetime` is inherited from the `datetime.date` they are not compatible from comparison and arithmetical operations perspective. | ||
Trying to compare a `datetime.datetime` with a `datetime.date` objects leads to a `TypeError`. | ||
|
||
== How to fix it | ||
Make sure that comparison or arithmetic operations are performed between compatible types e.g. convert `datetime.datetime` to `datetime.date` or vice versa. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,python,diff-id=1,diff-type=noncompliant] | ||
---- | ||
from datetime import datetime, date | ||
dt = datetime.now() | ||
d = date.today() | ||
if dt < d: # Noncompliant: TypeError: can't compare datetime.datetime to datetime.date | ||
... | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,python,diff-id=1,diff-type=compliant] | ||
---- | ||
from datetime import datetime, date | ||
dt = datetime.now() | ||
d = date.today() | ||
if dt.date() < d: # OK | ||
... | ||
---- | ||
|
||
//=== How does this work? | ||
|
||
//=== Pitfalls | ||
|
||
//=== Going the extra mile | ||
|
||
|
||
== Resources | ||
=== Documentation | ||
* Python documentation - https://docs.python.org/3/library/datetime.html#datetime-objects[Datetime Objects] | ||
* Python documentation - https://docs.python.org/3/library/datetime.html#date-objects[Date Objects] | ||
//=== Articles & blog posts | ||
//=== Conference presentations | ||
//=== Standards | ||
//=== External coding guidelines | ||
//=== Benchmarks |