From ef1e4fb8876cb1cd7a0b32942ec6c445f1b6c29a Mon Sep 17 00:00:00 2001 From: Seppli11 Date: Tue, 12 Nov 2024 14:36:53 +0000 Subject: [PATCH 1/2] Create rule S7160 --- rules/S7160/metadata.json | 2 ++ rules/S7160/python/metadata.json | 25 ++++++++++++++++++ rules/S7160/python/rule.adoc | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 rules/S7160/metadata.json create mode 100644 rules/S7160/python/metadata.json create mode 100644 rules/S7160/python/rule.adoc diff --git a/rules/S7160/metadata.json b/rules/S7160/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S7160/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S7160/python/metadata.json b/rules/S7160/python/metadata.json new file mode 100644 index 00000000000..84c46f539ed --- /dev/null +++ b/rules/S7160/python/metadata.json @@ -0,0 +1,25 @@ +{ + "title": "FIXME", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-7160", + "sqKey": "S7160", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "HIGH", + "RELIABILITY": "MEDIUM", + "SECURITY": "LOW" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S7160/python/rule.adoc b/rules/S7160/python/rule.adoc new file mode 100644 index 00000000000..caae0d69054 --- /dev/null +++ b/rules/S7160/python/rule.adoc @@ -0,0 +1,44 @@ +FIXME: add a description + +// If you want to factorize the description uncomment the following line and create the file. +//include::../description.adoc[] + +== Why is this an issue? + +FIXME: remove the unused optional headers (that are commented out) + +//=== What is the potential impact? + +== How to fix it +//== How to fix it in FRAMEWORK NAME + +=== Code examples + +==== Noncompliant code example + +[source,python,diff-id=1,diff-type=noncompliant] +---- +FIXME +---- + +==== Compliant solution + +[source,python,diff-id=1,diff-type=compliant] +---- +FIXME +---- + +//=== How does this work? + +//=== Pitfalls + +//=== Going the extra mile + + +//== Resources +//=== Documentation +//=== Articles & blog posts +//=== Conference presentations +//=== Standards +//=== External coding guidelines +//=== Benchmarks From da38277c3f226b26b2c321e8d1efb7c55ec494cb Mon Sep 17 00:00:00 2001 From: Sebastian Zumbrunn Date: Tue, 12 Nov 2024 17:33:25 +0100 Subject: [PATCH 2/2] write initial rule draft --- rules/S7160/python/metadata.json | 6 ++--- rules/S7160/python/rule.adoc | 43 +++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/rules/S7160/python/metadata.json b/rules/S7160/python/metadata.json index 84c46f539ed..a31e0769c43 100644 --- a/rules/S7160/python/metadata.json +++ b/rules/S7160/python/metadata.json @@ -1,5 +1,5 @@ { - "title": "FIXME", + "title": "Dataclasses should be recreated using \"dataclasses.replace\"", "type": "CODE_SMELL", "status": "ready", "remediation": { @@ -16,9 +16,7 @@ "quickfix": "unknown", "code": { "impacts": { - "MAINTAINABILITY": "HIGH", - "RELIABILITY": "MEDIUM", - "SECURITY": "LOW" + "MAINTAINABILITY": "MEDIUM" }, "attribute": "CONVENTIONAL" } diff --git a/rules/S7160/python/rule.adoc b/rules/S7160/python/rule.adoc index caae0d69054..d80f19b6312 100644 --- a/rules/S7160/python/rule.adoc +++ b/rules/S7160/python/rule.adoc @@ -1,16 +1,11 @@ -FIXME: add a description - -// If you want to factorize the description uncomment the following line and create the file. -//include::../description.adoc[] - == Why is this an issue? -FIXME: remove the unused optional headers (that are commented out) +Instead of manually recreating dataclasses, ``++dataclasses.replace(...)++`` can be used to recreate dataclasses with modified values. + +Alternatively, the in Python 3.13 introduced, more general-purpose ``++copy.replace(...)++`` can be used to archive the same result. -//=== What is the potential impact? == How to fix it -//== How to fix it in FRAMEWORK NAME === Code examples @@ -18,25 +13,45 @@ FIXME: remove the unused optional headers (that are commented out) [source,python,diff-id=1,diff-type=noncompliant] ---- -FIXME +from dataclasses import dataclass + +@dataclass +class Point: + x: int + y: int + +p1 = Point(1, 2) +p2 = Point(p1.x, 3) # Noncompliant ---- ==== Compliant solution [source,python,diff-id=1,diff-type=compliant] ---- -FIXME +from dataclasses import dataclass +import dataclasses +import copy + +@dataclass +class Point: + x: int + y: int + +p1 = Point(1, 2) +p2 = dataclasses.replace(p1, y=3) # Compliant +p3 = copy.replace(p1, y=3) # Compliant ---- -//=== How does this work? - //=== Pitfalls //=== Going the extra mile -//== Resources -//=== Documentation +== Resources +=== Documentation + +* https://docs.python.org/3/library/dataclasses.html#dataclasses.replace +* https://docs.python.org/3/library/copy.html#copy.replace //=== Articles & blog posts //=== Conference presentations //=== Standards