Skip to content

Commit

Permalink
pythonlib: Remove star imports (OSGeo#1546)
Browse files Browse the repository at this point in the history
Replaces star import (from x import *) by regular imports (from x import y).

Enables the relevant Flake8 warnings.

Adds the warning to per-file ignores for init files where we expect and allow
using star imports (unfortunately some init files contain actual code suffers
from these warning being disabled, but that's a different issue).
  • Loading branch information
a0x8o committed Aug 23, 2024
1 parent 00d1c55 commit 9e2a78d
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 0 deletions.
70 changes: 70 additions & 0 deletions python/grass/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
[flake8]
ignore =
E203, # whitespace before ':' (Black)
W503, # line break before binary operator (Black)
E722, # do not use bare 'except'

per-file-ignores =
# C wrappers call libgis.G_gisinit before importing other modules.
# TODO: Is this really needed?
pygrass/vector/__init__.py: E402,
pygrass/raster/__init__.py: E402,
# Files and directories which need fixes or specific exceptions
# E501 line too long
# W605 invalid escape sequence
# E741 ambiguous variable name 'l'
gunittest/*.py: E501 # These are mainly just todo comments
gunittest/gmodules.py: E501, W605
pygrass/vector/geometry.py: W605
pygrass/vector/table.py: E501
pygrass/vector/__init__.py: E501, E402
pygrass/modules/interface/*.py: E501, F401
pygrass/modules/interface/module.py: E501, W605
pygrass/modules/interface/parameter.py: E501, W605
pygrass/modules/grid/*.py: E501, F401
pygrass/raster/*.py: E501
pygrass/raster/rowio.py: E741
pygrass/rpc/__init__.py: E501, F401, F403
pygrass/utils.py: E402, E501
script/db.py: E501
script/task.py: W605
script/vector.py: E501 # Long doctest lines which need review anyway
temporal/*.py: E501, F841
temporal/abstract_space_time_dataset.py: W605, E501, F841
temporal/temporal_algebra.py: E741, E501, F841
temporal/temporal_raster_algebra.py: E741
temporal/temporal_raster3d_algebra.py: E741
temporal/temporal_vector_algebra.py: E741, E501, F841
# Current benchmarks/tests are changing sys.path before import.
# Possibly, a different approach should be taken there anyway.
pygrass/tests/benchmark.py: E501, E402, F401, F821
# Configuration file for Sphinx:
# Ignoring import/code mix and line length.
docs/conf.py: E402, E501,
# Files not managed by Black
imaging/images2gif.py: E226, E501
# Unused imports in init files
# F401 imported but unused
# F403 star import used; unable to detect undefined names
*/__init__.py: F401, F403
*/*/__init__.py: F401, F403
*/*/*/__init__.py: F401, F403


max-line-length = 88
exclude =
.git,
__pycache__,
.env,
.venv,
env,
venv,
ENV,
env.bak,
venv.bak,
ctypes,
pydispatch,
testsuite,

builtins =
_
4 changes: 4 additions & 0 deletions python/grass/exceptions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ def __init__(self, module, code, returncode, errors=None):
:param errors: errors provided by the module (e.g., stderr)
"""
# CalledProcessError has undocumented constructor
<<<<<<< HEAD
super().__init__(returncode, module)
=======
super(CalledModuleError, self).__init__(returncode, module)
>>>>>>> 3fce2ed438 (pythonlib: Remove star imports (#1546))
if not module or module in code:
# No need to include module name if it is directly in code
# of if it is not set.
Expand Down
11 changes: 11 additions & 0 deletions python/grass/gunittest/invoker.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def _run_test_module(self, module, results_dir, gisdbase, location, timeout):
timeout=timeout,
check=False,
)
<<<<<<< HEAD
stdout = p.stdout
stderr = p.stderr
returncode = p.returncode
Expand All @@ -224,6 +225,12 @@ def _run_test_module(self, module, results_dir, gisdbase, location, timeout):

encodings = [_get_encoding(), "utf8", "latin-1", "ascii"]

=======
stdout, stderr = p.communicate()
returncode = p.returncode
encodings = [_get_encoding(), "utf8", "latin-1", "ascii"]

>>>>>>> 3fce2ed438 (pythonlib: Remove star imports (#1546))
def try_decode(data, encodings):
"""Try to decode data (bytes) using one of encodings
Expand All @@ -232,7 +239,11 @@ def try_decode(data, encodings):
"""
for encoding in encodings:
try:
<<<<<<< HEAD
return decode(data, encoding=encoding)
=======
return decode(stdout, encoding=encoding)
>>>>>>> 3fce2ed438 (pythonlib: Remove star imports (#1546))
except UnicodeError:
pass
if isinstance(data, bytes):
Expand Down
8 changes: 8 additions & 0 deletions python/grass/pygrass/vector/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
import numpy as np
from sqlite3 import OperationalError

<<<<<<< HEAD
=======
try:
from collections import OrderedDict
except ImportError:
from grass.pygrass.orderdict import OrderedDict

>>>>>>> 3fce2ed438 (pythonlib: Remove star imports (#1546))
import grass.lib.vector as libvect
from grass.pygrass.gis import Mapset
from grass.exceptions import DBError
Expand Down
4 changes: 4 additions & 0 deletions python/grass/script/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
.. sectionauthor:: Glynn Clements
.. sectionauthor:: Martin Landa <landa.martin gmail.com>
"""
<<<<<<< HEAD
=======
from __future__ import absolute_import
>>>>>>> 3fce2ed438 (pythonlib: Remove star imports (#1546))

import os
from .core import (
Expand Down
26 changes: 26 additions & 0 deletions python/grass/script/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
.. sectionauthor:: Martin Landa <landa.martin gmail.com>
"""
<<<<<<< HEAD

=======
>>>>>>> 3fce2ed438 (pythonlib: Remove star imports (#1546))
import os
import re
import sys
Expand All @@ -28,7 +31,30 @@
from .utils import decode, split
from .core import Popen, PIPE, get_real_command

<<<<<<< HEAD
ETREE_EXCEPTIONS = (ET.ParseError, expat.ExpatError)
=======
<<<<<<< HEAD
ETREE_EXCEPTIONS = (etree.ParseError, expat.ExpatError)
=======
try:
import xml.etree.ElementTree as etree
except ImportError:
import elementtree.ElementTree as etree # Python <= 2.4
from xml.parsers import expat # TODO: works for any Python?

# Get the XML parsing exceptions to catch. The behavior chnaged with Python 2.7
# and ElementTree 1.3.
if hasattr(etree, "ParseError"):
ETREE_EXCEPTIONS = (etree.ParseError, expat.ExpatError)
else:
ETREE_EXCEPTIONS = expat.ExpatError


if sys.version_info.major >= 3:
unicode = str
>>>>>>> 3fce2ed438 (pythonlib: Remove star imports (#1546))
>>>>>>> 4bd0126b6c (pythonlib: Remove star imports (#1546))


class grassTask:
Expand Down
6 changes: 6 additions & 0 deletions python/grass/script/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""

import os
import sys

from .utils import parse_key_val
from .core import (
Expand All @@ -27,6 +28,11 @@
fatal,
debug,
)
<<<<<<< HEAD
=======

from grass.exceptions import CalledModuleError, ScriptError
>>>>>>> 3fce2ed438 (pythonlib: Remove star imports (#1546))

from grass.exceptions import CalledModuleError, ScriptError

Expand Down
8 changes: 8 additions & 0 deletions python/grass/temporal/c_libraries_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@
"""

import logging
<<<<<<< HEAD
import sys
from ctypes import CFUNCTYPE, POINTER, byref, c_int, c_void_p, cast
=======
<<<<<<< HEAD
from ctypes import byref, cast, c_int, c_void_p, CFUNCTYPE, POINTER
=======
from ctypes import byref, cast, c_char_p, c_int, c_void_p, CFUNCTYPE, POINTER
>>>>>>> 3fce2ed438 (pythonlib: Remove star imports (#1546))
>>>>>>> bdc1a9eff8 (pythonlib: Remove star imports (#1546))
from datetime import datetime
from multiprocessing import Lock, Pipe, Process

Expand Down
12 changes: 12 additions & 0 deletions python/grass/temporal/temporal_granularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@
:authors: Soeren Gebbert
"""
<<<<<<< HEAD

<<<<<<< HEAD
=======
from .datetime_math import compute_datetime_delta
from .abstract_map_dataset import AbstractMapDataset
=======
from __future__ import print_function
from .datetime_math import compute_datetime_delta
>>>>>>> 3fce2ed438 (pythonlib: Remove star imports (#1546))
from functools import reduce
from collections import OrderedDict
>>>>>>> bdc1a9eff8 (pythonlib: Remove star imports (#1546))
import ast
from collections import OrderedDict
from functools import reduce
Expand Down

0 comments on commit 9e2a78d

Please sign in to comment.