Skip to content

Commit 454983c

Browse files
committed
feat: add GCI110 (Python) — avoid wildcard imports; docs + matrix + changelog (severity INFO, scoped exceptions)
1 parent e8ad36f commit 454983c

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Add rule GCI109: Avoid Using Exceptions for Control Flow (Python)
13+
- Add rule GCI110: Avoid wildcard imports in Python (`from module import *`)
1314

1415
### Changed
1516

RULES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Some are applicable for different technologies.
8686
| GCI107 | Data : Avoid Iterative Matrix Operations | Use vectorization by the usage of the built-in functions of TensorFlow, NumPy or Pandas | | 🚫 | 🚫 | 🚫 || 🚫 | 🚫 | 🚫 | [documentation](https://github.com/green-code-initiative/creedengo-rules-specifications/tree/main/src/main/rules/GCI107) |
8787
| GCI108 | Prefer Append Left | When you want to insert an element at the beginning of a list, it's better to use a deques or a double linkedlist. | |||||||| [documentation](https://github.com/green-code-initiative/creedengo-rules-specifications/tree/main/src/main/rules/GCI108) |
8888
| GCI109 | Avoid Using Exceptions for Control Flow | Using exceptions for control flow (loops, defaults, branching) is expensive in Python. Prefer idiomatic alternatives like dict.get(), enumerate(), getattr() with defaults, or proper iteration patterns. | | 🚫 | 🚫 | 🚫 || 🚫 | 🚫 | 🚫 | [documentation](https://github.com/green-code-initiative/creedengo-rules-specifications/tree/main/src/main/rules/GCI109) |
89+
| GCI110 | Python: Avoid wildcard imports (`from module import *`) | Wildcard imports bind many names and run module top‑level init, slightly increasing import time and memory. Prefer explicit named imports or module aliases; allow exceptions for `__init__.py` re‑exports or modules with `__all__`. | | 🚫 | 🚫 | 🚫 || 🚫 | 🚫 | 🚫 | [documentation](https://github.com/green-code-initiative/creedengo-rules-specifications/tree/main/src/main/rules/GCI110) |
8990
| GCI203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | 🚧 | 🚀 | 🚀 || 🚀 | 🚀 | 🚫 | [documentation](https://github.com/green-code-initiative/creedengo-rules-specifications/tree/main/src/main/rules/GCI203) |
9091
| GCI404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 || 🚫 | 🚫 | 🚫 | [documentation](https://github.com/green-code-initiative/creedengo-rules-specifications/tree/main/src/main/rules/GCI404) |
9192
| GCI522 | Sobriety: Brightness Override | To avoid draining the battery, iOS and Android devices adapt the brightness of the screen depending on the environment light. | | 🚫 | 🚫 || 🚫 | 🚫 | 🚫 | 🚫 | [documentation](https://github.com/green-code-initiative/creedengo-rules-specifications/tree/main/src/main/rules/GCI522) |

src/main/rules/GCI110/GCI110.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"title": "Avoid wildcard imports in Python (from module import *)",
3+
"type": "CODE_SMELL",
4+
"status": "ready",
5+
"remediation": {
6+
"func": "Constant/Issue",
7+
"constantCost": "10min"
8+
},
9+
"tags": [
10+
"creedengo",
11+
"eco-design",
12+
"performance",
13+
"python",
14+
"bad-practice",
15+
"memory"
16+
],
17+
"defaultSeverity": "Info"
18+
}
19+
20+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
= GCI110 — Avoid wildcard imports in Python (`from module import *`)
2+
3+
== Why this rule?
4+
5+
Wildcard imports bind many names into the current namespace and execute all top‑level module initialization.
6+
This can slightly increase import time, parsing, and memory. Multiplied across many modules or cold starts,
7+
this adds avoidable CPU/energy. It also hurts analyzability and maintainability by obscuring where names come from.
8+
9+
This rule is advisory for eco‑design: the effect is modest compared to algorithmic issues, so severity is Info.
10+
11+
== Rule scope
12+
13+
- Flags top‑level statements: `from module import *`.
14+
- Focuses on readability/maintainability with secondary eco‑impact (CPU/memory at import time).
15+
16+
== Exceptions (when it’s acceptable)
17+
18+
- Package aggregation files (`__init__.py`) that re‑export a curated API.
19+
- If the target module defines `__all__`, making the export surface explicit.
20+
- Generated code, quick scripts/REPL/notebooks, or educational material.
21+
22+
== Non‑compliant
23+
24+
[source,python]
25+
----
26+
from utils import *
27+
process(data)
28+
----
29+
30+
== Compliant alternatives
31+
32+
[source,python]
33+
----
34+
# Explicit named imports
35+
from utils import parse, process
36+
37+
# Or module import with alias
38+
import utils as u
39+
u.process(data)
40+
----
41+
42+
== Public API re‑exports
43+
44+
[source,python]
45+
----
46+
# In package/__init__.py
47+
from .parse import parse
48+
from .process import process
49+
__all__ = ["parse", "process"]
50+
----
51+
52+
== Configuration parameters (recommended)
53+
54+
- `allowInInit` (default: true): don’t report in `__init__.py`.
55+
- `ignoreTests` (default: true): ignore paths like `tests/**`, `**/test_*.py`.
56+
- `ignoreMigrations` (default: true): ignore `**/migrations/**`.
57+
- `ignoreIfHasAll` (default: true): don’t report if imported module defines `__all__`.
58+
59+
== References
60+
61+
- PEP 8 — imports: discourages `from module import *` in most cases.
62+
- Real Python — Python imports and `__all__`.
63+

0 commit comments

Comments
 (0)