From 6ab887f6fbabe173d9163955e512de404eecd633 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:54:23 +0200 Subject: [PATCH] Create rule S6886 (#3572) * Create rule S6886 * Specify rule * CR fixes * CR fixes * Modify rule classification and impacts * Invert two words --------- Co-authored-by: maksim-grebeniuk-sonarsource Co-authored-by: Maksim Grebeniuk Co-authored-by: Ghislain Piot --- rules/S6886/metadata.json | 2 ++ rules/S6886/python/metadata.json | 23 ++++++++++++++++ rules/S6886/python/rule.adoc | 46 ++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 rules/S6886/metadata.json create mode 100644 rules/S6886/python/metadata.json create mode 100644 rules/S6886/python/rule.adoc diff --git a/rules/S6886/metadata.json b/rules/S6886/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S6886/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S6886/python/metadata.json b/rules/S6886/python/metadata.json new file mode 100644 index 00000000000..5d5548f24c3 --- /dev/null +++ b/rules/S6886/python/metadata.json @@ -0,0 +1,23 @@ +{ + "title": "offset-naive datetime.time and datetime.datetime objects should not be compared with offset-aware ones", + "type": "BUG", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-6886", + "sqKey": "S6886", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "RELIABILITY": "HIGH" + }, + "attribute": "LOGICAL" + } +} diff --git a/rules/S6886/python/rule.adoc b/rules/S6886/python/rule.adoc new file mode 100644 index 00000000000..113a597a202 --- /dev/null +++ b/rules/S6886/python/rule.adoc @@ -0,0 +1,46 @@ +This rule raises an issue when trying to perform comparison or arithmetic operations between `offset-naive` and `offset-aware` Datetimes. + +== Why is this an issue? + +`datetime.datetime` and `datetime.time` objects may be categorized as "aware" or "naive" depending on whether or not they include timezone information. + +Comparison or arithmetic operations between `offset-naive` and `offset-aware` datetimes raise a `TypeError`. + +== How to fix it +Perform comparison or arithmetic operations only between offset-naive or only between offset-aware Datetimes. + +=== Code examples + +==== Noncompliant code example + +[source,python,diff-id=1,diff-type=noncompliant] +---- +import datetime +from pytz import timezone + +tz = timezone('America/New_York') +dt1 = datetime.datetime(2023, 5, 23, tzinfo=tz) +dt2 = datetime.datetime(2023, 5, 24) + +if dt1 < dt2: # Noncompliant: TypeError: can't compare offset-naive and offset-aware datetimes + ... +---- + +==== Compliant solution + +[source,python,diff-id=1,diff-type=compliant] +---- +import datetime +from pytz import timezone + +tz = timezone('America/New_York') +dt1 = datetime.datetime(2023, 5, 23, tzinfo=tz) +dt2 = datetime.datetime(2023, 5, 24, timezone.utc) + +if dt1 < dt2: # OK + ... +---- + +== Resources +=== Documentation +* Python documentation - https://docs.python.org/3/library/datetime.html#aware-and-naive-objects[Aware and Naive Objects]