Commit 05ab3833 authored by Laurent Peuch's avatar Laurent Peuch
Browse files

Please the flake8 god

parent 4ecf013bb610
......@@ -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()
......
......@@ -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
......
......@@ -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
......
......@@ -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)
......
......@@ -21,9 +21,6 @@ __docformat__ = "restructuredtext en"
import os
import errno
import signal
import sys
import time
import warnings
......
......@@ -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:
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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):
......
......@@ -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:
......
......@@ -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
""" % (
......
......@@ -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):
......
......@@ -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,
......
......@@ -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)