Skip to content

Commit 365100f

Browse files
author
Joonalai
committed
Warn about importing gdal directly
1 parent ac9b068 commit 365100f

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Just call `flake8 .` in your package or `flake your.py`.
3333
* `QGS103`: Don't use from-imports from PyQt directly ([example](#QGS103))
3434
* `QGS104`: Don't use imports from PyQt directly ([example](#QGS104))
3535
* `QGS105`: Don't pass QgisInterface as an argument ([example](#QGS105))
36+
* `QGS106`: Don't import gdal directly, import if from osgeo package ([example](#QGS106))
37+
3638

3739

3840
You might have good reasons to ignore some rules.
@@ -108,3 +110,14 @@ def some_function(somearg):
108110
def classFactory(iface):
109111
# preferably do not pass the iface to plugin
110112
```
113+
114+
### QGS106
115+
116+
```python
117+
# Bad
118+
import gdal
119+
import ogr
120+
121+
# Good
122+
from osgeo import gdal
123+
```

flake8_qgis/flake8_qgis.py

+16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"QGS105 Do not pass iface (QgisInterface) as an argument, "
2727
"instead import it: 'from qgs.utils import iface'"
2828
)
29+
QGS106 = "QGS106 Use 'from osgeo import {members}' " "instead of 'import {members}'"
2930

3031

3132
def _get_qgs101_and_103(
@@ -139,6 +140,20 @@ def _get_qgs105(node: ast.FunctionDef) -> List[Tuple[int, int, str]]:
139140
return errors
140141

141142

143+
def _get_qgs106(node: ast.Import) -> List[Tuple[int, int, str]]:
144+
errors: List[Tuple[int, int, str]] = []
145+
for alias in node.names:
146+
if alias.name in ("gdal", "ogr"):
147+
errors.append(
148+
(
149+
node.lineno,
150+
node.col_offset,
151+
QGS106.format(members=alias.name),
152+
)
153+
)
154+
return errors
155+
156+
142157
class Visitor(ast.NodeVisitor):
143158
def __init__(self) -> None:
144159
self.errors: List[Tuple[int, int, str]] = []
@@ -151,6 +166,7 @@ def visit_ImportFrom(self, node: ast.ImportFrom) -> Any: # noqa N802
151166
def visit_Import(self, node: Import) -> Any: # noqa N802
152167
self.errors += _get_qgs102(node)
153168
self.errors += _get_qgs104(node)
169+
self.errors += _get_qgs106(node)
154170
self.generic_visit(node)
155171

156172
def visit_FunctionDef(self, node: FunctionDef) -> Any: # noqa N802

tests/test_flake8_qgis.py

+15
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,18 @@ def some_method(somearg, iface):
151151
"QGS105 Do not pass iface (QgisInterface) as an argument, instead import "
152152
"it: 'from qgs.utils import iface'"
153153
)
154+
155+
156+
def test_QGS106_pass():
157+
ret = _results("from osgeo import gdal")
158+
ret = ret.union(_results("from osgeo import ogr"))
159+
assert ret == set()
160+
161+
162+
def test_QGS106():
163+
ret = _results("import gdal")
164+
ret = ret.union(_results("import ogr"))
165+
assert ret == {
166+
"1:0 QGS106 Use 'from osgeo import gdal' instead of 'import gdal'",
167+
"1:0 QGS106 Use 'from osgeo import ogr' instead of 'import ogr'",
168+
}

0 commit comments

Comments
 (0)