From b9540a1d77f6cf71d7412d5bc2bd760b75a1950c Mon Sep 17 00:00:00 2001 From: Ankit Patil Date: Tue, 9 May 2023 14:56:19 +0530 Subject: [PATCH 1/5] Add compliant and noncompliant examples of python/notebook-variable-redefinition@v1.0 --- ...book_variable_redefinition_compliant.ipynb | 84 +++++++++++++++++ ...k_variable_redefinition_noncompliant.ipynb | 93 +++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb create mode 100644 src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_noncompliant.ipynb diff --git a/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb new file mode 100644 index 0000000..c531ef1 --- /dev/null +++ b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb @@ -0,0 +1,84 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n", + "#### SPDX-License-Identifier: Apache-2.0" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# {fact rule=notebook-variable-redefinition@v1.0 defects=0}\n", + "import math\t" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "x = int(input('x:'))\n", + "y = int(input('y:'))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "z = '5'" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "z: 44.64302857109943\n" + ] + } + ], + "source": [ + "# Compliant: even though there are 2 different types assigned to variable `z`, it is only used within the same cells.\n", + "z = math.sqrt(x**2 + y**2)\n", + "print ('z:', z)\n", + "# {/fact}" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.8" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_noncompliant.ipynb b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_noncompliant.ipynb new file mode 100644 index 0000000..8528b63 --- /dev/null +++ b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_noncompliant.ipynb @@ -0,0 +1,93 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n", + "#### SPDX-License-Identifier: Apache-2.0" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# {fact rule=notebook-variable-redefinition@v1.0 defects=1}\n", + "import math" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "x = int(input('x:'))\n", + "y = int(input('y:'))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "z = '5'" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "z = math.sqrt(x**2 + y**2)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "z: 34.17601498127012\n" + ] + } + ], + "source": [ + "# Noncompliant: variable `z` is assigned to 2 different types of values in above cells 3 and 4. \n", + "# Then in cell 5, the value of `z` is used in a `print()` call.\n", + "print ('z:', z)\n", + "# {/fact}" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.8" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} From d0ed36f1ebe1ec0fed2d24aa366e557fb4ca2271 Mon Sep 17 00:00:00 2001 From: ankit-amazon <125257518+ankit-amazon@users.noreply.github.com> Date: Tue, 9 May 2023 21:03:37 +0530 Subject: [PATCH 2/5] Update notebook_variable_redefinition_compliant.ipynb --- .../notebook_variable_redefinition_compliant.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb index c531ef1..c56474c 100644 --- a/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb +++ b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb @@ -52,7 +52,7 @@ } ], "source": [ - "# Compliant: even though there are 2 different types assigned to variable `z`, it is only used within the same cells.\n", + "# Compliant: Even though there are 2 different types assigned to variable z in two different cells, it is only used within one of the two cells.\n", "z = math.sqrt(x**2 + y**2)\n", "print ('z:', z)\n", "# {/fact}" From 08c68070001b16720b9e396859f5f5e6224372c4 Mon Sep 17 00:00:00 2001 From: ankit-amazon <125257518+ankit-amazon@users.noreply.github.com> Date: Tue, 9 May 2023 21:06:33 +0530 Subject: [PATCH 3/5] Update notebook_variable_redefinition_compliant.ipynb --- .../notebook_variable_redefinition_compliant.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb index c56474c..fbd0f99 100644 --- a/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb +++ b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb @@ -52,7 +52,7 @@ } ], "source": [ - "# Compliant: Even though there are 2 different types assigned to variable z in two different cells, it is only used within one of the two cells.\n", + "# Compliant: Even though there are 2 different types assigned to variable `z` in two different cells, it is only used within one of the two cells.\n", "z = math.sqrt(x**2 + y**2)\n", "print ('z:', z)\n", "# {/fact}" From 8f417ce46924454655edd400d28420a8a81fb680 Mon Sep 17 00:00:00 2001 From: Ankit Patil Date: Fri, 19 May 2023 12:18:42 +0530 Subject: [PATCH 4/5] Remove ipynb and add py files for python/notebook-variable-redefinition --- .../notebook_variable_redefinition.py | 34 +++++++ ...book_variable_redefinition_compliant.ipynb | 84 ----------------- ...k_variable_redefinition_noncompliant.ipynb | 93 ------------------- 3 files changed, 34 insertions(+), 177 deletions(-) create mode 100644 src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition.py delete mode 100644 src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb delete mode 100644 src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_noncompliant.ipynb diff --git a/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition.py b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition.py new file mode 100644 index 0000000..d268b7c --- /dev/null +++ b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition.py @@ -0,0 +1,34 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +# {fact rule=notebook-variable-redefinition@v1.0 defects=1} +# —— Code Cell 1, Execution Count 1 —— # +import math +# —— Code Cell 2, Execution Count 2 —— # +x = int(input('x:')) +y = int(input('y:')) +# —— Code Cell 3, Execution Count 3 —— # +z = '5' +# —— Code Cell 4, Execution Count 4 —— # +z = math.sqrt(x**2 + y**2) +# —— Code Cell 5, Execution Count 5 —— # +# Noncompliant: variable `z` is assigned to different types of values in above +# cells 3 and 4. Then in cell 5, the value of `z` is used in a `print()` call. +print('z:', z) +# {/fact} + + +# {fact rule=notebook-variable-redefinition@v1.0 defects=0} +# —— Code Cell 1, Execution Count 1 —— # +import math +# —— Code Cell 2, Execution Count 2 —— # +x = int(input('x:')) +y = int(input('y:')) +# —— Code Cell 3, Execution Count 3 —— # +z = '5' +# —— Code Cell 4, Execution Count 4 —— # +# Compliant: Even though there are 2 different types assigned to variable `z` +# in two different cells, it is only used within one of the two cells. +z = math.sqrt(x**2 + y**2) +print('z:', z) +# {/fact} diff --git a/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb deleted file mode 100644 index fbd0f99..0000000 --- a/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_compliant.ipynb +++ /dev/null @@ -1,84 +0,0 @@ -{ - "cells": [ - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n", - "#### SPDX-License-Identifier: Apache-2.0" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# {fact rule=notebook-variable-redefinition@v1.0 defects=0}\n", - "import math\t" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "x = int(input('x:'))\n", - "y = int(input('y:'))" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "z = '5'" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "z: 44.64302857109943\n" - ] - } - ], - "source": [ - "# Compliant: Even though there are 2 different types assigned to variable `z` in two different cells, it is only used within one of the two cells.\n", - "z = math.sqrt(x**2 + y**2)\n", - "print ('z:', z)\n", - "# {/fact}" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.8" - }, - "orig_nbformat": 4 - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_noncompliant.ipynb b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_noncompliant.ipynb deleted file mode 100644 index 8528b63..0000000 --- a/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition_noncompliant.ipynb +++ /dev/null @@ -1,93 +0,0 @@ -{ - "cells": [ - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n", - "#### SPDX-License-Identifier: Apache-2.0" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# {fact rule=notebook-variable-redefinition@v1.0 defects=1}\n", - "import math" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "x = int(input('x:'))\n", - "y = int(input('y:'))" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "z = '5'" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "z = math.sqrt(x**2 + y**2)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "z: 34.17601498127012\n" - ] - } - ], - "source": [ - "# Noncompliant: variable `z` is assigned to 2 different types of values in above cells 3 and 4. \n", - "# Then in cell 5, the value of `z` is used in a `print()` call.\n", - "print ('z:', z)\n", - "# {/fact}" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.8" - }, - "orig_nbformat": 4 - }, - "nbformat": 4, - "nbformat_minor": 2 -} From aacfa6dbebea598c5a23b4f102a5321ad3e9b9d0 Mon Sep 17 00:00:00 2001 From: ankit-amazon <125257518+ankit-amazon@users.noreply.github.com> Date: Tue, 23 May 2023 11:06:11 +0530 Subject: [PATCH 5/5] Update notebook_variable_redefinition.py --- .../notebook_variable_redefinition.py | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition.py b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition.py index d268b7c..03b1a8d 100644 --- a/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition.py +++ b/src/python/detectors/notebook_variable_redefinition/notebook_variable_redefinition.py @@ -2,31 +2,32 @@ # SPDX-License-Identifier: Apache-2.0 # {fact rule=notebook-variable-redefinition@v1.0 defects=1} -# —— Code Cell 1, Execution Count 1 —— # +# —— Code Cell 1 —— # import math -# —— Code Cell 2, Execution Count 2 —— # +# —— Code Cell 2 —— # x = int(input('x:')) y = int(input('y:')) -# —— Code Cell 3, Execution Count 3 —— # +# —— Code Cell 3 —— # z = '5' -# —— Code Cell 4, Execution Count 4 —— # +# —— Code Cell 4 —— # z = math.sqrt(x**2 + y**2) -# —— Code Cell 5, Execution Count 5 —— # +# —— Code Cell 5 —— # # Noncompliant: variable `z` is assigned to different types of values in above -# cells 3 and 4. Then in cell 5, the value of `z` is used in a `print()` call. +# cells 3 and 4. Then in cell 5, the value of `z` is used in a `print()` call, +# which can have different result depending on the execution order of cells. print('z:', z) # {/fact} # {fact rule=notebook-variable-redefinition@v1.0 defects=0} -# —— Code Cell 1, Execution Count 1 —— # +# —— Code Cell 1 —— # import math -# —— Code Cell 2, Execution Count 2 —— # +# —— Code Cell 2 —— # x = int(input('x:')) y = int(input('y:')) -# —— Code Cell 3, Execution Count 3 —— # +# —— Code Cell 3 —— # z = '5' -# —— Code Cell 4, Execution Count 4 —— # +# —— Code Cell 4 —— # # Compliant: Even though there are 2 different types assigned to variable `z` # in two different cells, it is only used within one of the two cells. z = math.sqrt(x**2 + y**2)