-
Notifications
You must be signed in to change notification settings - Fork 22
added rule GCI 110 - avoid wildcard imports #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hassineabd
wants to merge
2
commits into
green-code-initiative:main
Choose a base branch
from
hassineabd:rules/GCI109-avoid-wildcard-imports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
22 changes: 22 additions & 0 deletions
22
src/it/test-projects/creedengo-python-plugin-test-project/src/avoidWildcardImports.py
This file contains hidden or 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,22 @@ | ||
| ### Non-compliant cases ### | ||
| from math import * # Noncompliant {{Avoid wildcard imports}} | ||
| from my_module import * # Noncompliant {{Avoid wildcard imports}} | ||
| def some_function(): | ||
| from collections import * # Noncompliant {{Avoid wildcard imports}} | ||
| return deque() | ||
| if True: | ||
| from json import * # Noncompliant {{Avoid wildcard imports}} | ||
|
|
||
|
|
||
| ### Compliant cases ### | ||
| from math import sqrt, pi, sin, cos | ||
| import os | ||
|
|
||
| import my_module | ||
|
|
||
| from datetime import datetime as dt | ||
|
|
||
| def another_function(): | ||
| from collections import deque | ||
| return deque() | ||
|
|
This file contains hidden or 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
44 changes: 44 additions & 0 deletions
44
src/main/java/org/greencodeinitiative/creedengo/python/checks/AvoidWildcardImportsCheck.java
This file contains hidden or 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,44 @@ | ||
| /* | ||
| * creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs | ||
| * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.python.checks; | ||
|
|
||
| import org.sonar.check.Rule; | ||
| import org.sonar.plugins.python.api.PythonSubscriptionCheck; | ||
| import org.sonar.plugins.python.api.SubscriptionContext; | ||
| import org.sonar.plugins.python.api.tree.ImportFrom; | ||
| import org.sonar.plugins.python.api.tree.Tree; | ||
|
|
||
| @Rule(key = "GCI110") | ||
| public class AvoidWildcardImportsCheck extends PythonSubscriptionCheck { | ||
|
|
||
| public static final String DESCRIPTION = "Avoid wildcard imports"; | ||
|
|
||
| @Override | ||
| public void initialize(Context context) { | ||
| context.registerSyntaxNodeConsumer(Tree.Kind.IMPORT_FROM, this::visitImportFrom); | ||
| } | ||
|
|
||
| private void visitImportFrom(SubscriptionContext context) { | ||
| ImportFrom importFrom = (ImportFrom) context.syntaxNode(); | ||
|
|
||
| if (importFrom.isWildcardImport()) { | ||
| context.addIssue(importFrom, DESCRIPTION); | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| "GCI106", | ||
| "GCI107", | ||
| "GCI108", | ||
| "GCI110", | ||
| "GCI203", | ||
| "GCI404" | ||
| ] | ||
|
|
||
30 changes: 30 additions & 0 deletions
30
...t/java/org/greencodeinitiative/creedengo/python/checks/AvoidWildcardImportsCheckTest.java
This file contains hidden or 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,30 @@ | ||
| /* | ||
| * creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs | ||
| * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.python.checks; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.sonar.python.checks.utils.PythonCheckVerifier; | ||
|
|
||
| public class AvoidWildcardImportsCheckTest { | ||
|
|
||
| @Test | ||
| public void test() { | ||
| PythonCheckVerifier.verify("src/test/resources/checks/avoidWildcardImports.py", new AvoidWildcardImportsCheck()); | ||
| } | ||
| } | ||
|
|
This file contains hidden or 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,33 @@ | ||
| ### Non-compliant cases ### | ||
| from math import * # Noncompliant {{Avoid wildcard imports}} | ||
| from os import * # Noncompliant {{Avoid wildcard imports}} | ||
| from sys import * # Noncompliant {{Avoid wildcard imports}} | ||
| from my_module import * # Noncompliant {{Avoid wildcard imports}} | ||
| def some_function(): | ||
| from collections import * # Noncompliant {{Avoid wildcard imports}} | ||
| return deque() | ||
| from typing import * # Noncompliant {{Avoid wildcard imports}} | ||
| from itertools import * # Noncompliant {{Avoid wildcard imports}} | ||
| if True: | ||
| from json import * # Noncompliant {{Avoid wildcard imports}} | ||
|
|
||
|
|
||
|
|
||
| ### Compliant cases ### | ||
| from math import sqrt, pi, sin, cos | ||
| from os import path, listdir | ||
| import math | ||
| import os | ||
|
|
||
| import my_module | ||
|
|
||
| from datetime import datetime as dt | ||
|
|
||
| def another_function(): | ||
| from collections import deque | ||
| return deque() | ||
|
|
||
| from typing import List, Dict, Optional | ||
|
|
||
|
|
||
| import collections |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The line numbers in the integration test don't match the actual wildcard import lines in the test file. Based on the test file, wildcard imports are on lines 2, 3, 5, and 8, not the specified lines.