From 68244bd6978f7316606fe21f94219930485e6119 Mon Sep 17 00:00:00 2001 From: ghislainpiot Date: Tue, 4 Jun 2024 07:53:23 +0000 Subject: [PATCH 1/4] Create rule S6979 --- rules/S6979/metadata.json | 2 ++ rules/S6979/python/metadata.json | 25 ++++++++++++++++++ rules/S6979/python/rule.adoc | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 rules/S6979/metadata.json create mode 100644 rules/S6979/python/metadata.json create mode 100644 rules/S6979/python/rule.adoc diff --git a/rules/S6979/metadata.json b/rules/S6979/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S6979/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S6979/python/metadata.json b/rules/S6979/python/metadata.json new file mode 100644 index 00000000000..b6cccfcadbd --- /dev/null +++ b/rules/S6979/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-6979", + "sqKey": "S6979", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "HIGH", + "RELIABILITY": "MEDIUM", + "SECURITY": "LOW" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S6979/python/rule.adoc b/rules/S6979/python/rule.adoc new file mode 100644 index 00000000000..4bd440f87a8 --- /dev/null +++ b/rules/S6979/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,text,diff-id=1,diff-type=noncompliant] +---- +FIXME +---- + +==== Compliant solution + +[source,text,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 aba502f1c1ac5d87b4266b976ca88fcb8b4b5bb7 Mon Sep 17 00:00:00 2001 From: Ghislain Piot Date: Tue, 4 Jun 2024 11:44:49 +0200 Subject: [PATCH 2/4] First iteration of rule --- rules/S6979/python/metadata.json | 10 ++--- rules/S6979/python/rule.adoc | 64 +++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/rules/S6979/python/metadata.json b/rules/S6979/python/metadata.json index b6cccfcadbd..90db0b43665 100644 --- a/rules/S6979/python/metadata.json +++ b/rules/S6979/python/metadata.json @@ -1,10 +1,10 @@ { - "title": "FIXME", + "title": "\"torch.tensor\" should be used instead of \"torch.autograd.Variable\"", "type": "CODE_SMELL", "status": "ready", "remediation": { "func": "Constant\/Issue", - "constantCost": "5min" + "constantCost": "2min" }, "tags": [ ], @@ -13,12 +13,10 @@ "sqKey": "S6979", "scope": "All", "defaultQualityProfiles": ["Sonar way"], - "quickfix": "unknown", + "quickfix": "targeted", "code": { "impacts": { - "MAINTAINABILITY": "HIGH", - "RELIABILITY": "MEDIUM", - "SECURITY": "LOW" + "MAINTAINABILITY": "MEDIUM" }, "attribute": "CONVENTIONAL" } diff --git a/rules/S6979/python/rule.adoc b/rules/S6979/python/rule.adoc index 4bd440f87a8..8cc8363db12 100644 --- a/rules/S6979/python/rule.adoc +++ b/rules/S6979/python/rule.adoc @@ -1,44 +1,66 @@ -FIXME: add a description - -// If you want to factorize the description uncomment the following line and create the file. -//include::../description.adoc[] +This rule raises when a `torch.autograd.Variable` is instantiated == Why is this an issue? -FIXME: remove the unused optional headers (that are commented out) +The Pytorch Variable API has been deprecated. Autograd now works as expected with tensors that have the `requires_grad` attribute set to `True`. -//=== What is the potential impact? +The Variable API now returns tensors anyways, so there should not be any breaking changes. == How to fix it -//== How to fix it in FRAMEWORK NAME + +Replace the call to `torch.autograd.Variable` with a call to `torch.tensor` and set the `requires_grad` attribute to `True` if needed. === Code examples ==== Noncompliant code example -[source,text,diff-id=1,diff-type=noncompliant] +[source,python,diff-id=1,diff-type=noncompliant] ---- -FIXME +import torch + +x = torch.autograd.Variable(torch.tensor([1.0]), requires_grad=True) # Noncompliant +x2 = torch.autograd.Variable(torch.tensor([1.0])) # Noncompliant ---- ==== Compliant solution -[source,text,diff-id=1,diff-type=compliant] +[source,python,diff-id=1,diff-type=compliant] ---- -FIXME +import torch + +x = torch.tensor([1.0], requires_grad=True) +x2 = torch.tensor([1.0]) ---- -//=== How does this work? -//=== Pitfalls +== Resources +=== Documentation + +* Pytorch documentation - https://pytorch.org/docs/stable/autograd.html#variable-deprecated[Variable API] + + +ifdef::env-github,rspecator-view[] + +(visible only on this page) + +== Implementation specification + +Should be pretty straighforward to implement. + +=== Message + +Primary : Replace with call to `torch.tensor`. + + +=== Issue location + +Primary : Name of the function call + +=== Quickfix -//=== Going the extra mile +Might be tricky to know how to call the `torch.tensor` function. +If there is an import like `from torch import tensor`, then replace with `tensor(...)` +If not, then replace with `torch.tensor(...)` -//== Resources -//=== Documentation -//=== Articles & blog posts -//=== Conference presentations -//=== Standards -//=== External coding guidelines -//=== Benchmarks +endif::env-github,rspecator-view[] From dab39b5d33043f965cadcac734629f5714447901 Mon Sep 17 00:00:00 2001 From: Ghislain Piot Date: Wed, 5 Jun 2024 14:19:10 +0200 Subject: [PATCH 3/4] Review comment and typo --- rules/S6979/python/rule.adoc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rules/S6979/python/rule.adoc b/rules/S6979/python/rule.adoc index 8cc8363db12..7d4a0262c24 100644 --- a/rules/S6979/python/rule.adoc +++ b/rules/S6979/python/rule.adoc @@ -1,10 +1,10 @@ -This rule raises when a `torch.autograd.Variable` is instantiated +This rule raises when a `torch.autograd.Variable` is instantiated. == Why is this an issue? -The Pytorch Variable API has been deprecated. Autograd now works as expected with tensors that have the `requires_grad` attribute set to `True`. +The Pytorch Variable API has been deprecated. The behavior of Variables is now provided by the Pytorch tensors and can be controlled with the `requires_grad` parameter. -The Variable API now returns tensors anyways, so there should not be any breaking changes. +The Variable API now returns tensors anyway, so there should not be any breaking changes. == How to fix it From aa98cdfee2205bd2d2fbd7ac23bdf7276bc314b3 Mon Sep 17 00:00:00 2001 From: Sebastian Zumbrunn Date: Fri, 6 Sep 2024 17:16:23 +0200 Subject: [PATCH 4/4] update issue message --- rules/S6979/python/rule.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/S6979/python/rule.adoc b/rules/S6979/python/rule.adoc index 7d4a0262c24..a5cc295ab2a 100644 --- a/rules/S6979/python/rule.adoc +++ b/rules/S6979/python/rule.adoc @@ -49,7 +49,7 @@ Should be pretty straighforward to implement. === Message -Primary : Replace with call to `torch.tensor`. +Primary : Replace this call with a call to "torch.tensor". === Issue location