diff options
Diffstat (limited to 'logilab')
29 files changed, 138 insertions, 179 deletions
diff --git a/logilab/common/cache.py b/logilab/common/cache.py index 6b673a2..1562111 100644 --- a/logilab/common/cache.py +++ b/logilab/common/cache.py @@ -27,7 +27,7 @@ __docformat__ = "restructuredtext en" from threading import Lock from logilab.common.decorators import locked -from typing import Union, TypeVar, List +from typing import TypeVar, List _marker = object() diff --git a/logilab/common/clcommands.py b/logilab/common/clcommands.py index f89a4b4..b73791c 100644 --- a/logilab/common/clcommands.py +++ b/logilab/common/clcommands.py @@ -103,7 +103,7 @@ class CommandLine(dict): def register(self, cls, force=False): """register the given :class:`Command` subclass""" - assert not self.check_duplicated_command or force or not cls.name in self, ( + assert not self.check_duplicated_command or force or cls.name not in self, ( "a command %s is already defined" % cls.name ) self[cls.name] = cls diff --git a/logilab/common/compat.py b/logilab/common/compat.py index e601b26..c0d3d41 100644 --- a/logilab/common/compat.py +++ b/logilab/common/compat.py @@ -29,14 +29,12 @@ See another compatibility snippets from other projects: __docformat__ = "restructuredtext en" -import os import sys import types -from warnings import warn from typing import Union # not used here, but imported to preserve API -import builtins +import builtins # noqa def str_to_bytes(string): @@ -65,14 +63,14 @@ else: if sys.version_info < (3, 0): from cStringIO import StringIO - FileIO = file + FileIO = file # noqa BytesIO = StringIO - reload = reload + reload = reload # noqa else: - from io import FileIO, BytesIO, StringIO - from imp import reload + from io import StringIO, FileIO # noqa + from imp import reload # noqa -from logilab.common.deprecation import deprecated +from logilab.common.deprecation import deprecated # noqa # Other projects import these from here, keep providing them for # backwards compat diff --git a/logilab/common/configuration.py b/logilab/common/configuration.py index 4c83030..fac0121 100644 --- a/logilab/common/configuration.py +++ b/logilab/common/configuration.py @@ -126,8 +126,7 @@ from os.path import exists, expanduser from optparse import OptionGroup from copy import copy from _io import StringIO, TextIOWrapper -from mypy_extensions import NoReturn -from typing import Any, Optional, Union, Dict, List, Tuple, Iterator, Callable, Sequence +from typing import Any, Optional, Union, Dict, List, Tuple, Iterator, Callable from warnings import warn import configparser as cp @@ -168,7 +167,7 @@ _ValueType = Union[List[str], Tuple[str, ...], str] def choice_validator(optdict: Dict[str, Any], name: str, value: str) -> str: """validate and return a converted value for option of type 'choice' """ - if not value in optdict["choices"]: + if value not in optdict["choices"]: msg = "option %s: invalid value: %r, should be in %s" raise optik_ext.OptionValueError(msg % (name, value, optdict["choices"])) return value @@ -180,7 +179,7 @@ def multiple_choice_validator(optdict: Dict[str, Any], name: str, value: _ValueT choices = optdict["choices"] values = optik_ext.check_csv(None, name, value) for value in values: - if not value in choices: + if value not in choices: msg = "option %s: invalid value: %r, should be in %s" raise optik_ext.OptionValueError(msg % (name, value, choices)) return values @@ -269,7 +268,7 @@ def _call_validator( return VALIDATORS[opttype](value) except optik_ext.OptionValueError: raise - except: + except Exception: raise optik_ext.OptionValueError( "%s value (%r) should be of type %s" % (option, value, opttype) ) @@ -638,7 +637,7 @@ class OptionsManagerMixIn(object): del optdict["short"] # cleanup option definition dict before giving it to optik for key in list(optdict.keys()): - if not key in self._optik_option_attrs: + if key not in self._optik_option_attrs: optdict.pop(key) return args, optdict @@ -682,7 +681,7 @@ class OptionsManagerMixIn(object): options = [(n, d, v) for (n, d, v) in options if d.get("type") is not None] if not options: continue - if not section in sections: + if section not in sections: sections.append(section) alloptions = options_by_section.setdefault(section, []) alloptions += options @@ -773,7 +772,7 @@ class OptionsManagerMixIn(object): for section, option, optdict in provider.all_options(): if onlysection is not None and section != onlysection: continue - if not "type" in optdict: + if "type" not in optdict: # ignore action without type (callback, store_true...) continue provider.input_option(option, optdict, inputlevel) @@ -1079,7 +1078,7 @@ class ConfigurationMixIn(OptionsManagerMixIn, OptionsProviderMixIn): gdef = (optdict["group"].upper(), "") except KeyError: continue - if not gdef in self.option_groups: + if gdef not in self.option_groups: self.option_groups.append(gdef) self.register_options_provider(self, own_group=False) @@ -1225,7 +1224,7 @@ def read_old_config(newconfig, changes, configfile): newconfig[optname] = newvalue done.add(optname) for optname, optdef in newconfig.options: - if optdef.get("type") and not optname in done: + if optdef.get("type") and optname not in done: newconfig.set_option(optname, oldconfig[optname], optdict=optdef) diff --git a/logilab/common/daemon.py b/logilab/common/daemon.py index dd4496b..7b4994a 100644 --- a/logilab/common/daemon.py +++ b/logilab/common/daemon.py @@ -21,9 +21,6 @@ __docformat__ = "restructuredtext en" import os import errno -import signal -import sys -import time import warnings diff --git a/logilab/common/date.py b/logilab/common/date.py index 5f43d3e..e182193 100644 --- a/logilab/common/date.py +++ b/logilab/common/date.py @@ -27,7 +27,7 @@ from locale import getlocale, LC_TIME from datetime import date, time, datetime, timedelta from time import strptime as time_strptime from calendar import monthrange, timegm -from typing import Union, List, Any, Iterator, Optional, Generator +from typing import Union, List, Any, Optional, Generator try: @@ -213,14 +213,6 @@ def date_range( ONEDAY: timedelta = timedelta(days=1) ONEWEEK: timedelta = timedelta(days=7) -try: - strptime = datetime.strptime -except AttributeError: # py < 2.5 - from time import strptime as time_strptime - - def strptime(value, format): - return datetime(*time_strptime(value, format)[:6]) - def strptime_time(value, format="%H:%M"): return time(*time_strptime(value, format)[3:6]) @@ -315,7 +307,7 @@ def ustrftime(somedate: datetime, fmt: str = "%Y-%m-%d") -> str: try: if sys.version_info < (3, 0): encoding = getlocale(LC_TIME)[1] or "ascii" - return unicode(somedate.strftime(str(fmt)), encoding) + return str(somedate.strftime(str(fmt)), encoding) else: return somedate.strftime(fmt) except ValueError: @@ -332,7 +324,7 @@ def ustrftime(somedate: datetime, fmt: str = "%Y-%m-%d") -> str: if isinstance(somedate, datetime): fields.update({"H": somedate.hour, "M": somedate.minute, "S": somedate.second}) fmt = re.sub("%([YmdHMS])", r"%(\1)02d", fmt) - return unicode(fmt) % fields + return str(fmt) % fields def utcdatetime(dt: datetime) -> datetime: diff --git a/logilab/common/debugger.py b/logilab/common/debugger.py index 6553557..c61b9d5 100644 --- a/logilab/common/debugger.py +++ b/logilab/common/debugger.py @@ -39,7 +39,6 @@ except ImportError: # conditional import readline = None # type: ignore import os -import os.path as osp import sys from pdb import Pdb import inspect @@ -186,7 +185,7 @@ class Debugger(Pdb): ret = ret + self.get_class_members(base) return ret - ## specific / overridden commands + # specific / overridden commands def do_list(self, arg): """overrides default list command to display the surrounding block instead of 5 lines of context diff --git a/logilab/common/decorators.py b/logilab/common/decorators.py index a471353..b7228e9 100644 --- a/logilab/common/decorators.py +++ b/logilab/common/decorators.py @@ -21,11 +21,8 @@ from __future__ import print_function __docformat__ = "restructuredtext en" -import sys -import types from time import process_time, time from inspect import isgeneratorfunction -from mypy_extensions import NoReturn from typing import Any, Optional, Callable, Union from inspect import getfullargspec @@ -81,7 +78,7 @@ class _SingleValueCache(object): try: wrapped.__doc__ = self.callable.__doc__ wrapped.__name__ = self.callable.__name__ - except: + except Exception: pass return wrapped diff --git a/logilab/common/fileutils.py b/logilab/common/fileutils.py index ffe98ff..f89ae1d 100644 --- a/logilab/common/fileutils.py +++ b/logilab/common/fileutils.py @@ -40,7 +40,6 @@ from typing import Optional, List, Tuple from _io import TextIOWrapper from logilab.common import STD_BLACKLIST as BASE_BLACKLIST, IGNORED_EXTENSIONS -from logilab.common.shellutils import find from logilab.common.deprecation import deprecated from logilab.common.compat import FileIO diff --git a/logilab/common/graph.py b/logilab/common/graph.py index 82c3a32..d460692 100644 --- a/logilab/common/graph.py +++ b/logilab/common/graph.py @@ -30,7 +30,7 @@ import sys import tempfile import codecs import errno -from typing import Dict, List, Tuple, Union, Any, Optional, Set, TypeVar, Iterable +from typing import Dict, List, Tuple, Optional, Set, TypeVar, Iterable def escape(value): diff --git a/logilab/common/modutils.py b/logilab/common/modutils.py index 9ca4c81..4c1aee6 100644 --- a/logilab/common/modutils.py +++ b/logilab/common/modutils.py @@ -39,18 +39,20 @@ from os.path import ( isdir, dirname, exists, - basename, expanduser, normcase, realpath, ) from imp import find_module, load_module, C_BUILTIN, PY_COMPILED, PKG_DIRECTORY -from distutils.sysconfig import get_config_var, get_python_lib +from distutils.sysconfig import get_python_lib from distutils.errors import DistutilsPlatformError from typing import Dict, List, Optional, Any, Tuple, Union, Sequence from types import ModuleType from _frozen_importlib_external import FileFinder +from logilab.common import STD_BLACKLIST, _handle_blacklist +from logilab.common.deprecation import deprecated + try: import zipimport except ImportError: @@ -60,9 +62,6 @@ except ImportError: ZIPFILE = object() -from logilab.common import STD_BLACKLIST, _handle_blacklist -from logilab.common.deprecation import deprecated - # Notes about STD_LIB_DIR # Consider arch-specific installation for STD_LIB_DIR definition # :mod:`distutils.sysconfig` contains to much hardcoded values to rely on @@ -108,7 +107,7 @@ class LazyObject(object): def __getattribute__(self, attr): try: return super(LazyObject, self).__getattribute__(attr) - except AttributeError as ex: + except AttributeError: return getattr(self._getobj(), attr) def __call__(self, *args, **kwargs): @@ -422,7 +421,7 @@ def get_modules( for directory, dirnames, filenames in os.walk(src_directory): _handle_blacklist(blacklist, dirnames, filenames) # check for __init__.py - if not "__init__.py" in filenames: + if "__init__.py" not in filenames: dirnames[:] = () continue if directory != src_directory: @@ -458,7 +457,7 @@ def get_module_files(src_directory: str, blacklist: Sequence[str] = STD_BLACKLIS for directory, dirnames, filenames in os.walk(src_directory): _handle_blacklist(blacklist, dirnames, filenames) # check for __init__.py - if not "__init__.py" in filenames: + if "__init__.py" not in filenames: dirnames[:] = () continue for filename in filenames: @@ -551,7 +550,7 @@ def is_standard_module( modname = modname.split(".")[0] try: filename = file_from_modpath([modname]) - except ImportError as ex: + except ImportError: # import failed, i'm probably not so wrong by supposing it's # not standard... return False @@ -656,8 +655,8 @@ def _is_namespace(modname: str) -> bool: # mypy: Module has no attribute "_namespace_packages"; maybe "fixup_namespace_packages"?" # but is still has? or is it a failure from python3 port? return ( - pkg_resources is not None and modname in pkg_resources._namespace_packages - ) # type: ignore + pkg_resources is not None and modname in pkg_resources._namespace_packages # type: ignore + ) def _module_file( @@ -684,7 +683,7 @@ def _module_file( pic = sys.path_importer_cache _path = path if path is not None else sys.path for __path in _path: - if not __path in pic: + if __path not in pic: try: pic[__path] = zipimport.zipimporter(__path) except zipimport.ZipImportError: diff --git a/logilab/common/optik_ext.py b/logilab/common/optik_ext.py index 3f321b5..222f48b 100644 --- a/logilab/common/optik_ext.py +++ b/logilab/common/optik_ext.py @@ -58,11 +58,10 @@ from os.path import exists from logilab.common import attrdict from typing import Any, Union, List, Optional, Tuple, Dict -from optparse import Values, IndentedHelpFormatter, OptionGroup from _io import StringIO # python >= 2.3 -from optparse import ( +from optparse import ( # noqa OptionParser as BaseParser, Option as BaseOption, OptionGroup, @@ -71,8 +70,8 @@ from optparse import ( OptionError, Values, HelpFormatter, - NO_DEFAULT, SUPPRESS_HELP, + NO_DEFAULT, ) try: @@ -179,7 +178,7 @@ def check_date(option, opt, value): def check_color(option, opt, value): """check a color value and returns it - /!\ does *not* check color labels (like 'red', 'green'), only + /!\\ does *not* check color labels (like 'red', 'green'), only checks hexadecimal forms """ # Case (1) : color label, we trust the end-user @@ -257,9 +256,9 @@ class Option(BaseOption): elif not isinstance(self.choices, (tuple, list)): # type: ignore raise OptionError( "choices must be a list of strings ('%s' supplied)" - % str(type(self.choices)).split("'")[1], + % str(type(self.choices)).split("'")[1], # type: ignore self, - ) # type: ignore + ) elif self.choices is not None: # type: ignore raise OptionError("must not supply choices for type %r" % self.type, self) @@ -331,8 +330,8 @@ def level_options(group: BaseParser, outputlevel: int) -> List[BaseOption]: return [ option for option in group.option_list - if (getattr(option, "level", 0) or 0) <= outputlevel and not option.help is SUPPRESS_HELP - ] # type: ignore + if (getattr(option, "level", 0) or 0) <= outputlevel and option.help is not SUPPRESS_HELP # type: ignore # noqa + ] def format_option_help(self, formatter): @@ -408,7 +407,7 @@ class ManHelpFormatter(HelpFormatter): return '.TH %s %s "%s" %s' % (pgm, section, date, pgm) def format_short_description(self, pgm: str, short_desc: str) -> str: - return """.SH NAME + return r""".SH NAME .B %s \- %s """ % ( diff --git a/logilab/common/optparser.py b/logilab/common/optparser.py index 8dd6b36..e048de3 100644 --- a/logilab/common/optparser.py +++ b/logilab/common/optparser.py @@ -35,15 +35,15 @@ __docformat__ = "restructuredtext en" from warnings import warn +import sys +import optparse + warn( "lgc.optparser module is deprecated, use lgc.clcommands instead", DeprecationWarning, stacklevel=2, ) -import sys -import optparse - class OptionParser(optparse.OptionParser): def __init__(self, *args, **kwargs): diff --git a/logilab/common/pytest.py b/logilab/common/pytest.py index 0f89ddf..5c8a69b 100644 --- a/logilab/common/pytest.py +++ b/logilab/common/pytest.py @@ -95,25 +95,9 @@ you can filter the function with a simple python expression from __future__ import print_function -__docformat__ = "restructuredtext en" - -PYTEST_DOC = """%prog [OPTIONS] [testfile [testpattern]] - -examples: - -logilab-pytest path/to/mytests.py -logilab-pytest path/to/mytests.py TheseTests -logilab-pytest path/to/mytests.py TheseTests.test_thisone -logilab-pytest path/to/mytests.py -m '(not long and database) or regr' - -logilab-pytest one (will run both test_thisone and test_thatone) -logilab-pytest path/to/mytests.py -s not (will skip test_notthisone) -""" - -ENABLE_DBC = False -FILE_RESTART = ".pytest.restart" - -import os, sys, re +import os +import re +import sys import os.path as osp from time import process_time, time from re import Match @@ -130,7 +114,7 @@ from itertools import dropwhile from unittest.runner import _WritelnDecorator # type: ignore from unittest.suite import TestSuite -from typing import Callable, Any, Optional, List, Tuple, Generator, Union, Dict +from typing import Callable, Any, Optional, List, Tuple, Generator, Dict from logilab.common.deprecation import deprecated from logilab.common.fileutils import abspath_listdir @@ -139,12 +123,36 @@ from logilab.common import testlib, STD_BLACKLIST # use the same unittest module as testlib from logilab.common.testlib import unittest, start_interactive_mode -from logilab.common.testlib import nocoverage, pause_trace, replace_trace # bwcompat +from logilab.common.testlib import ( # noqa + nocoverage, + pause_trace, + replace_trace, +) # bwcompat from logilab.common.debugger import Debugger, colorize_source import doctest import unittest as unittest_legacy +from .decorators import monkeypatch + +__docformat__ = "restructuredtext en" + +PYTEST_DOC = """%prog [OPTIONS] [testfile [testpattern]] + +examples: + +logilab-pytest path/to/mytests.py +logilab-pytest path/to/mytests.py TheseTests +logilab-pytest path/to/mytests.py TheseTests.test_thisone +logilab-pytest path/to/mytests.py -m '(not long and database) or regr' + +logilab-pytest one (will run both test_thisone and test_thatone) +logilab-pytest path/to/mytests.py -s not (will skip test_notthisone) +""" + +ENABLE_DBC = False +FILE_RESTART = ".pytest.restart" + if not getattr(unittest_legacy, "__package__", None): try: import unittest2.suite as unittest_suite @@ -155,7 +163,7 @@ else: import unittest.suite as unittest_suite # type: ignore try: - import django + import django # noqa from logilab.common.modutils import modpath_from_file, load_module_from_modpath DJANGO_FOUND = True @@ -164,7 +172,7 @@ except ImportError: CONF_FILE = "pytestconf.py" -TESTFILE_RE = re.compile("^((unit)?test.*|smoketest)\.py$") +TESTFILE_RE = re.compile(r"^((unit)?test.*|smoketest)\.py$") def this_is_a_testfile(filename: str) -> Optional[Match]: @@ -442,14 +450,12 @@ class DjangoTester(PyTester): def load_django_settings(self, dirname): """try to find project's setting and load it""" curdir = osp.abspath(dirname) - previousdir = curdir while not osp.isfile(osp.join(curdir, "settings.py")) and osp.isfile( osp.join(curdir, "__init__.py") ): newdir = osp.normpath(osp.join(curdir, os.pardir)) if newdir == curdir: raise AssertionError("could not find settings.py") - previousdir = curdir curdir = newdir # late django initialization settings = load_module_from_modpath(modpath_from_file(osp.join(curdir, "settings.py"))) @@ -704,7 +710,7 @@ def run(): # mock a new command line sys.argv[1:] = parser.newargs cvg = None - if not "" in sys.path: + if "" not in sys.path: sys.path.insert(0, "") if DJANGO_FOUND and options.django: tester = DjangoTester(cvg, options) @@ -729,7 +735,7 @@ def run(): cmd(*args) except SystemExit: raise - except: + except Exception: import traceback traceback.print_exc() @@ -1068,9 +1074,13 @@ class SkipAwareTestResult(unittest._TextTestResult): def _iter_valid_frames(self, frames: List[FrameInfo]) -> Generator[FrameInfo, Any, None]: """only consider non-testlib frames when formatting traceback""" + + def invalid(fi): + return osp.abspath(fi[1]) in (lgc_testlib, std_testlib) + lgc_testlib = osp.abspath(__file__) std_testlib = osp.abspath(unittest.__file__) - invalid = lambda fi: osp.abspath(fi[1]) in (lgc_testlib, std_testlib) + for frameinfo in dropwhile(invalid, frames): yield frameinfo @@ -1115,7 +1125,7 @@ class SkipAwareTestResult(unittest._TextTestResult): """err -> (exc_type, exc, tcbk)""" exc_type, exc, _ = err if isinstance(exc, testlib.SkipTest): - assert exc_type == SkipTest + assert exc_type == testlib.SkipTest self.addSkip(test, exc) else: if self.exitfirst: @@ -1160,8 +1170,6 @@ class SkipAwareTestResult(unittest._TextTestResult): self.stream.writeln("no stderr".center(len(self.separator2))) -from .decorators import monkeypatch - orig_call = testlib.TestCase.__call__ @@ -1387,7 +1395,7 @@ if sys.version_info >= (2, 7): # The function below implements a modified version of the # TestSuite.run method that is provided with python 2.7, in # unittest/suite.py - def _ts_run( + def _ts_run( # noqa self: Any, result: SkipAwareTestResult, debug: bool = False, diff --git a/logilab/common/registry.py b/logilab/common/registry.py index 83f4703..b0c07d2 100644 --- a/logilab/common/registry.py +++ b/logilab/common/registry.py @@ -94,8 +94,6 @@ from warnings import warn from typing import List, Tuple, Any, Iterable, Callable from types import ModuleType from typing_extensions import TypedDict -from _frozen_importlib import ModuleSpec -from _frozen_importlib_external import SourceFileLoader from logilab.common.modutils import modpath_from_file from logilab.common.logging_ext import set_log_methods @@ -134,7 +132,7 @@ _PREDICATES: Dict[int, Type] = {} def wrap_predicates(decorator: Callable) -> None: for predicate in _PREDICATES.values(): - if not "_decorators" in predicate.__dict__: + if "_decorators" not in predicate.__dict__: predicate._decorators = set() if decorator in predicate._decorators: continue @@ -568,7 +566,7 @@ class Registry(dict): def register(self, obj: Any, oid: Optional[Any] = None, clear: bool = False) -> None: """base method to add an object in the registry""" - assert not "__abstract__" in obj.__dict__, obj + assert "__abstract__" not in obj.__dict__, obj assert obj.__select__, obj oid = oid or obj.__regid__ assert oid, ( @@ -578,7 +576,7 @@ class Registry(dict): objects = self[oid] = [] else: objects = self.setdefault(oid, []) - assert not obj in objects, "object %s is already registered" % obj + assert obj not in objects, "object %s is already registered" % obj objects.append(obj) def register_and_replace(self, obj, replaced): @@ -943,7 +941,7 @@ class RegistryStore(dict): "modname expected to be a module name (ie string), got %r" % modname ) for obj in objects: - if self.is_registrable(obj) and obj.__module__ == modname and not obj in butclasses: + if self.is_registrable(obj) and obj.__module__ == modname and obj not in butclasses: if isinstance(obj, type): self._load_ancestors_then_object(modname, obj, butclasses) else: diff --git a/logilab/common/shellutils.py b/logilab/common/shellutils.py index 557e45d..f8914da 100644 --- a/logilab/common/shellutils.py +++ b/logilab/common/shellutils.py @@ -26,12 +26,9 @@ __docformat__ = "restructuredtext en" import os import glob import shutil -import stat import sys import tempfile -import time import fnmatch -import errno import string import random import subprocess @@ -41,7 +38,6 @@ from _io import StringIO from typing import Any, Callable, Optional, List, Union, Iterator, Tuple from logilab.common import STD_BLACKLIST, _handle_blacklist -from logilab.common.compat import str_to_bytes from logilab.common.deprecation import deprecated diff --git a/logilab/common/sphinx_ext.py b/logilab/common/sphinx_ext.py index 4ca30f7..0f4b446 100644 --- a/logilab/common/sphinx_ext.py +++ b/logilab/common/sphinx_ext.py @@ -18,6 +18,14 @@ from logilab.common.decorators import monkeypatch from sphinx.ext import autodoc +from sphinx.ext.autodoc import ( + ViewList, + Options, + AutodocReporter, + nodes, + assemble_option_dict, + nested_parse_with_titles, +) class DocstringOnlyModuleDocumenter(autodoc.ModuleDocumenter): @@ -45,16 +53,6 @@ def setup(app): app.add_autodocumenter(DocstringOnlyModuleDocumenter) -from sphinx.ext.autodoc import ( - ViewList, - Options, - AutodocReporter, - nodes, - assemble_option_dict, - nested_parse_with_titles, -) - - @monkeypatch(autodoc.AutoDirective) def run(self): self.filename_set = set() # a set of dependent filenames diff --git a/logilab/common/sphinxutils.py b/logilab/common/sphinxutils.py index 350188d..724ea39 100644 --- a/logilab/common/sphinxutils.py +++ b/logilab/common/sphinxutils.py @@ -29,7 +29,7 @@ Typical usage: >>> mgen.generate('api_logilab_common.rst', exclude_dirs=('test',)) """ -import os, sys +import sys import os.path as osp import inspect @@ -113,7 +113,7 @@ class ModuleGenerator: continue try: module = load_module_from_file(filepath) - except: # module might be broken or magic + except Exception: # module might be broken or magic dotted_path = modpath_from_file(filepath) module = type(".".join(dotted_path), (), {}) # mock it yield module diff --git a/logilab/common/table.py b/logilab/common/table.py index 983708b..46216d9 100644 --- a/logilab/common/table.py +++ b/logilab/common/table.py @@ -21,7 +21,8 @@ from __future__ import print_function from types import CodeType from typing import Any, List, Optional, Tuple, Union, Dict, Iterator from _io import StringIO -from mypy_extensions import NoReturn + +import re __docformat__ = "restructuredtext en" @@ -82,7 +83,7 @@ class Table(object): def __len__(self) -> int: return len(self.row_names) - ## Rows / Columns creation ################################################# + # Rows / Columns creation ################################################# def create_rows(self, row_names: List[str]) -> None: """Appends row_names to the list of existing rows """ @@ -110,7 +111,7 @@ class Table(object): for row in self.data: row.append(self.default_value) - ## Sort by column ########################################################## + # Sort by column ########################################################## def sort_by_column_id(self, col_id: str, method: str = "asc") -> None: """Sorts the table (in-place) according to data stored in col_id """ @@ -176,7 +177,7 @@ class Table(object): if row[col_index] == value: self.data.remove(row) - ## The 'setter' part ####################################################### + # The 'setter' part ####################################################### def set_cell(self, row_index: int, col_index: int, data: int) -> None: """sets value of cell 'row_indew', 'col_index' to data """ @@ -332,7 +333,7 @@ class Table(object): except ValueError: raise KeyError("Column (%s) not found in table" % (col_id)) - ## The 'getter' part ####################################################### + # The 'getter' part ####################################################### def get_shape(self) -> Tuple[int, int]: """Returns a tuple which represents the table's shape @@ -649,8 +650,6 @@ class TableStyle: return self.units[col_id] -import re - CELL_PROG = re.compile("([0-9]+)_([0-9]+)") diff --git a/logilab/common/testlib.py b/logilab/common/testlib.py index f8401c4..fe5e3c4 100644 --- a/logilab/common/testlib.py +++ b/logilab/common/testlib.py @@ -46,25 +46,26 @@ __docformat__ = "restructuredtext en" from contextlib import contextmanager import sys -import os, os.path as osp -import re -import difflib +import os +import os.path as osp import tempfile -import math import warnings from shutil import rmtree -from operator import itemgetter from inspect import isgeneratorfunction from typing import Any, Iterator, Union, Optional, Callable, Dict, List, Tuple from mypy_extensions import NoReturn import builtins -import configparser +import doctest from logilab.common.deprecation import class_deprecated, deprecated import unittest as unittest_legacy +from functools import wraps + +from logilab.common.decorators import cached, classproperty + if not getattr(unittest_legacy, "__package__", None): try: import unittest2 as unittest @@ -77,12 +78,6 @@ else: import unittest as unittest # type: ignore from unittest import SkipTest -from functools import wraps - -from logilab.common.debugger import Debugger -from logilab.common.decorators import cached, classproperty -from logilab.common import textutils - __all__ = ["unittest_main", "find_tests", "nocoverage", "pause_trace"] @@ -176,7 +171,7 @@ def find_tests(testdir, prefixes=DEFAULT_PREFIXES, suffix=".py", excludes=(), re return tests -## PostMortem Debug facilities ##### +# PostMortem Debug facilities ##### def start_interactive_mode(result): """starts an interactive shell so that the user can inspect errors """ @@ -283,8 +278,6 @@ unittest_main = unittest.main class InnerTestSkipped(SkipTest): """raised when a test is skipped""" - pass - def parse_generative_args(params: Tuple[int, ...]) -> Tuple[Union[List[bool], List[int]], Dict]: args = [] @@ -416,7 +409,7 @@ class TestCase(unittest.TestCase): ) result.addSuccess(self) return False - except: + except Exception: result.addError(self, self.__exc_info()) return False return True @@ -513,7 +506,7 @@ class TestCase(unittest.TestCase): success = False except SkipTest as e: result.addSkip(self, e) - except: + except Exception: # if an error occurs between two yield result.addError(self, self.__exc_info()) success = False @@ -547,7 +540,7 @@ class TestCase(unittest.TestCase): except SkipTest as e: result.addSkip(self, e) return 0 - except: + except Exception: result.addError(self, self.__exc_info()) return 2 return 0 @@ -567,8 +560,6 @@ TestCase.assertItemsEqual = deprecated("assertItemsEqual is deprecated, use asse TestCase.assertItemsEqual ) -import doctest - class SkippedSuite(unittest.TestSuite): def test(self): @@ -658,12 +649,11 @@ class MockConnection: def close(self): """Mock close method""" - pass # mypy error: Name 'Mock' is not defined # dynamic class created by this class -def mock_object(**params: Any) -> "Mock": # type: ignore +def mock_object(**params: Any) -> "Mock": # type: ignore # noqa """creates an object using params to set attributes >>> option = mock_object(verbose=False, index=range(5)) >>> option.verbose diff --git a/logilab/common/textutils.py b/logilab/common/textutils.py index b988c7a..b0f59d5 100644 --- a/logilab/common/textutils.py +++ b/logilab/common/textutils.py @@ -136,7 +136,7 @@ def unquote(string: str) -> str: _BLANKLINES_RGX = re.compile("\r?\n\r?\n") -_NORM_SPACES_RGX = re.compile("\s+") +_NORM_SPACES_RGX = re.compile(r"\s+") def normalize_text(text: str, line_len: int = 80, indent: str = "", rest: bool = False) -> str: diff --git a/logilab/common/tree.py b/logilab/common/tree.py index dbde2eb..33fb7e2 100644 --- a/logilab/common/tree.py +++ b/logilab/common/tree.py @@ -25,12 +25,10 @@ __docformat__ = "restructuredtext en" import sys -# from logilab.common import flatten from logilab.common.visitor import VisitedMixIn, FilteredIterator, no_filter -from logilab.common.types import Paragraph, Title -from typing import Optional, Any, Union, List, Callable, TypeVar +from typing import Optional, Any, List, Callable -## Exceptions ################################################################# +# Exceptions ################################################################# class NodeNotFound(Exception): @@ -244,7 +242,6 @@ class VNode(Node, VisitedMixIn): # we should probably merge this VisitedMixIn here because it's only used here """a visitable node """ - pass class BinaryNode(VNode): diff --git a/logilab/common/umessage.py b/logilab/common/umessage.py index a759003..2117062 100644 --- a/logilab/common/umessage.py +++ b/logilab/common/umessage.py @@ -113,7 +113,8 @@ class UMessage: message = self.message if index is None: - # mypy: Argument 1 to "get_payload" of "Message" has incompatible type "None"; expected "int" + # mypy: Argument 1 to "get_payload" of "Message" has incompatible type "None"; + # mypy: expected "int" # email.message.Message.get_payload has type signature: # Message.get_payload(self, i=None, decode=False) # so None seems to be totally acceptable, I don't understand mypy here diff --git a/logilab/common/ureports/__init__.py b/logilab/common/ureports/__init__.py index a539150..cf6e098 100644 --- a/logilab/common/ureports/__init__.py +++ b/logilab/common/ureports/__init__.py @@ -24,11 +24,12 @@ __docformat__ = "restructuredtext en" import sys +from typing import Any, Optional, Union, List, Generator, Tuple, Callable, TextIO + from logilab.common.compat import StringIO from logilab.common.textutils import linesep from logilab.common.tree import VNode -from logilab.common.ureports.nodes import Table, List as NodeList -from typing import Any, Optional, Union, List, Generator, Tuple, Callable, TextIO +from logilab.common.ureports.nodes import Table, Section, Link, Paragraph, Title, Text def get_nodes(node, klass): @@ -208,10 +209,5 @@ class BaseWriter(object): del self.writeln -# mypy error: Incompatible import of "Table" (imported name has type -# mypy error: "Type[logilab.common.ureports.nodes.Table]", local name has type -# mypy error: "Type[logilab.common.table.Table]") -# this will be cleaned when the "*" will be removed -from logilab.common.ureports.nodes import * # type: ignore -from logilab.common.ureports.text_writer import TextWriter -from logilab.common.ureports.html_writer import HTMLWriter +from logilab.common.ureports.text_writer import TextWriter # noqa +from logilab.common.ureports.html_writer import HTMLWriter # noqa diff --git a/logilab/common/ureports/docbook_writer.py b/logilab/common/ureports/docbook_writer.py index 7e7564f..cdf17cb 100644 --- a/logilab/common/ureports/docbook_writer.py +++ b/logilab/common/ureports/docbook_writer.py @@ -18,7 +18,7 @@ """HTML formatting drivers for ureports""" __docformat__ = "restructuredtext en" -from logilab.common.ureports import HTMLWriter +from logilab.common.ureports.html_writer import HTMLWriter class DocbookWriter(HTMLWriter): diff --git a/logilab/common/urllib2ext.py b/logilab/common/urllib2ext.py index dfbafc1..40cac40 100644 --- a/logilab/common/urllib2ext.py +++ b/logilab/common/urllib2ext.py @@ -6,13 +6,13 @@ import urllib2 import kerberos as krb -class GssapiAuthError(Exception): - """raised on error during authentication process""" +import re +RGX = re.compile(r"(?:.*,)*\s*Negotiate\s*([^,]*),?", re.I) -import re -RGX = re.compile("(?:.*,)*\s*Negotiate\s*([^,]*),?", re.I) +class GssapiAuthError(Exception): + """raised on error during authentication process""" def get_negociate_value(headers): @@ -85,8 +85,6 @@ if __name__ == "__main__": httplib.HTTPConnection.debuglevel = 1 httplib.HTTPSConnection.debuglevel = 1 - # debug - import logging logging.basicConfig(level=logging.DEBUG) # handle cookies diff --git a/logilab/common/vcgutils.py b/logilab/common/vcgutils.py index cd2b73a..c2a4e4e 100644 --- a/logilab/common/vcgutils.py +++ b/logilab/common/vcgutils.py @@ -177,7 +177,7 @@ def latin_to_vcg(st): num = ord(char) if num >= 192: st = st.replace(char, r"\fi%d" % ord(char)) - except: + except Exception: pass return st diff --git a/logilab/common/visitor.py b/logilab/common/visitor.py index 8d80d54..30a0729 100644 --- a/logilab/common/visitor.py +++ b/logilab/common/visitor.py @@ -42,7 +42,7 @@ class FilteredIterator(object): def __next__(self) -> Optional[Node]: try: return self._list.pop(0) - except: + except Exception: return None next = __next__ @@ -80,7 +80,6 @@ class Visitor(object): """ method called at the beginning of the visit """ - pass def close_visit(self, result): """ @@ -105,7 +104,7 @@ class VisitedMixIn(object): # mypy: "VisitedMixIn" has no attribute "TYPE" # dynamic attribute return self.TYPE.replace("-", "_") # type: ignore - except: + except Exception: return self.__class__.__name__.lower() def accept( diff --git a/logilab/common/xmlutils.py b/logilab/common/xmlutils.py index 14e3762..93224fb 100644 --- a/logilab/common/xmlutils.py +++ b/logilab/common/xmlutils.py @@ -29,10 +29,10 @@ instruction and return a Python dictionary. __docformat__ = "restructuredtext en" import re -from typing import Dict, Optional, Union +from typing import Dict, Optional -RE_DOUBLE_QUOTE = re.compile('([\w\-\.]+)="([^"]+)"') -RE_SIMPLE_QUOTE = re.compile("([\w\-\.]+)='([^']+)'") +RE_DOUBLE_QUOTE = re.compile(r'([\w\-\.]+)="([^"]+)"') +RE_SIMPLE_QUOTE = re.compile(r"([\w\-\.]+)='([^']+)'") def parse_pi_data(pi_data: str) -> Dict[str, Optional[str]]: |