Commit 7277245e authored by Laurent Peuch's avatar Laurent Peuch
Browse files

black && flake8

parent 40e18f2e05f4
......@@ -29,7 +29,7 @@ subpackage_of = "logilab"
subpackage_master = True
numversion = (1, 6, 1)
version = '.'.join([str(num) for num in numversion])
version = ".".join([str(num) for num in numversion])
license = "LGPL" # 2.1 or later
description = "collection of low-level Python packages and modules" " used by Logilab projects"
......
......@@ -28,6 +28,7 @@ from functools import wraps
class DeprecationWrapper(object):
"""proxy to print a warning on access to any attribute of the wrapped object
"""
def __init__(self, proxied, msg=None, version=None):
self._proxied = proxied
self._msg = msg
......@@ -77,11 +78,11 @@ def send_warning(reason, version=None, stacklevel=2, module_name=None):
compatible version.
"""
if module_name and version:
reason = '[%s %s] %s' % (module_name, version, reason)
reason = "[%s %s] %s" % (module_name, version, reason)
elif module_name:
reason = '[%s] %s' % (module_name, reason)
reason = "[%s] %s" % (module_name, reason)
elif version:
reason = '[%s] %s' % (version, reason)
reason = "[%s] %s" % (version, reason)
warn(reason, DeprecationWarning, stacklevel=stacklevel)
......@@ -94,10 +95,11 @@ def callable_renamed(old_name, new_function, version=None):
>>> old_function = renamed('old_function', new_function)
>>> old_function()
sample.py:57: DeprecationWarning: old_function has been renamed and is deprecated, uses new_function instead
old_function()
sample.py:57: DeprecationWarning: old_function has been renamed and is deprecated, uses
new_function instead old_function()
>>>
"""
@wraps(new_function)
def wrapped(*args, **kwargs):
send_warning(
......@@ -110,6 +112,7 @@ def callable_renamed(old_name, new_function, version=None):
module_name=new_function.__module__,
)
return new_function(*args, **kwargs)
return wrapped
......@@ -124,10 +127,11 @@ def argument_removed(old_argument_name, version=None):
... def some_function(new):
... return new
>>> some_function(old=42)
sample.py:15: DeprecationWarning: argument old of callable some_function has been renamed and is deprecated, use keyword argument new instead
some_function(old=42)
sample.py:15: DeprecationWarning: argument old of callable some_function has been renamed and
is deprecated, use keyword argument new instead some_function(old=42)
42
"""
def _wrap(func):
@wraps(func)
def check_kwargs(*args, **kwargs):
......@@ -154,15 +158,17 @@ def callable_deprecated(reason=None, version=None, stacklevel=2):
"""Display a deprecation message only if the version is older than the
compatible version.
"""
def decorator(func):
message = reason or 'The function "%s" is deprecated'
if '%s' in message:
if "%s" in message:
message %= func.__name__
@wraps(func)
def wrapped(*args, **kwargs):
send_warning(message, version, stacklevel + 1, module_name=func.__module__)
return func(*args, **kwargs)
return wrapped
return decorator
......@@ -198,15 +204,18 @@ def attribute_renamed(old_name, new_name, version=None):
>>> some_class = SomeClass()
>>> print(some_class.old)
sample.py:15: DeprecationWarning: SomeClass.old has been renamed and is deprecated, use SomeClass.new instead
sample.py:15: DeprecationWarning: SomeClass.old has been renamed and is deprecated, use
SomeClass.new instead
print(some_class.old)
42
>>> some_class.old = 43
sample.py:16: DeprecationWarning: SomeClass.old has been renamed and is deprecated, use SomeClass.new instead
sample.py:16: DeprecationWarning: SomeClass.old has been renamed and is deprecated, use
SomeClass.new instead
some_class.old = 43
>>> some_class.old == some_class.new
True
"""
def _class_wrap(klass):
reason = (
f"{klass.__name__}.{old_name} has been renamed and is deprecated, use "
......@@ -240,17 +249,21 @@ def argument_renamed(old_name, new_name, version=None):
... def some_function(new):
... return new
>>> some_function(old=42)
sample.py:15: DeprecationWarning: argument old of callable some_function has been renamed and is deprecated, use keyword argument new instead
sample.py:15: DeprecationWarning: argument old of callable some_function has been renamed and
is deprecated, use keyword argument new instead
some_function(old=42)
42
"""
def _wrap(func):
@wraps(func)
def check_kwargs(*args, **kwargs):
if old_name in kwargs and new_name in kwargs:
raise ValueError(f"argument {old_name} of callable {func.__name__} has been "
f"renamed to {new_name} but you are both using {old_name} and "
f"{new_name} has keyword arguments, only uses {new_name}")
raise ValueError(
f"argument {old_name} of callable {func.__name__} has been "
f"renamed to {new_name} but you are both using {old_name} and "
f"{new_name} has keyword arguments, only uses {new_name}"
)
if old_name in kwargs:
send_warning(
......
......@@ -32,7 +32,7 @@ with open(path.join(here, "__pkginfo__.py")) as f:
exec(f.read(), pkginfo)
# Get the long description from the relevant file
with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
with open(path.join(here, "README.rst"), encoding="utf-8") as f:
long_description = f.read()
setup(
......
......@@ -42,11 +42,13 @@ class RawInputTC(TestCase):
def mk_func(self):
def any_func():
pass
return any_func
def test_class_deprecated(self):
class AnyClass(object, metaclass=deprecation.class_deprecated):
pass
AnyClass()
self.assertEqual(self.messages, ["[test_deprecation] AnyClass is deprecated"])
......
......@@ -22,6 +22,7 @@ unit tests for module modutils (module manipulation utilities)
import doctest
import sys
import warnings
try:
__file__
except NameError:
......@@ -34,9 +35,9 @@ from os import path, getcwd, sep
from logilab import common
from logilab.common import tree
warnings.simplefilter('default', DeprecationWarning)
warnings.simplefilter("default", DeprecationWarning)
sys.path.insert(0, path.dirname(__file__))
DATADIR = path.join(path.dirname(__file__), 'data')
DATADIR = path.join(path.dirname(__file__), "data")
class ModutilsTestCase(TestCase):
......@@ -58,32 +59,37 @@ class ModuleFileTC(ModutilsTestCase):
def setUp(self):
super(ModuleFileTC, self).setUp()
for k in list(sys.path_importer_cache.keys()):
if 'MyPyPa' in k:
if "MyPyPa" in k:
del sys.path_importer_cache[k]
def test_find_zipped_module(self):
mtype, mfile = modutils._module_file([self.package], [path.join(DATADIR, 'MyPyPa-0.1.0.zip')])
mtype, mfile = modutils._module_file(
[self.package], [path.join(DATADIR, "MyPyPa-0.1.0.zip")]
)
self.assertEqual(mtype, modutils.ZIPFILE)
self.assertEqual(mfile.split(sep)[-4:], ["test", "data", "MyPyPa-0.1.0.zip", self.package])
def test_find_egg_module(self):
mtype, mfile = modutils._module_file([self.package], [path.join(DATADIR, 'MyPyPa-0.1.0-py2.5.egg')])
mtype, mfile = modutils._module_file(
[self.package], [path.join(DATADIR, "MyPyPa-0.1.0-py2.5.egg")]
)
self.assertEqual(mtype, modutils.ZIPFILE)
self.assertEqual(mfile.split(sep)[-4:], ["test", "data", "MyPyPa-0.1.0-py2.5.egg", self.package])
self.assertEqual(
mfile.split(sep)[-4:], ["test", "data", "MyPyPa-0.1.0-py2.5.egg", self.package]
)
class load_module_from_name_tc(ModutilsTestCase):
""" load a python module from it's name """
def test_knownValues_load_module_from_name_1(self):
self.assertEqual(modutils.load_module_from_name('sys'), sys)
self.assertEqual(modutils.load_module_from_name("sys"), sys)
def test_knownValues_load_module_from_name_2(self):
self.assertEqual(modutils.load_module_from_name('os.path'), path)
self.assertEqual(modutils.load_module_from_name("os.path"), path)
def test_raise_load_module_from_name_1(self):
self.assertRaises(ImportError,
modutils.load_module_from_name, 'os.path', use_sys=0)
self.assertRaises(ImportError, modutils.load_module_from_name, "os.path", use_sys=0)
class get_module_part_tc(ModutilsTestCase):
......@@ -102,20 +108,22 @@ class get_module_part_tc(ModutilsTestCase):
def test_knownValues_get_module_part_3(self):
"""relative import from given file"""
self.assertEqual(modutils.get_module_part('interface.Interface',
modutils.__file__), 'interface')
self.assertEqual(
modutils.get_module_part("interface.Interface", modutils.__file__), "interface"
)
def test_knownValues_get_compiled_module_part(self):
self.assertEqual(modutils.get_module_part('math.log10'), 'math')
self.assertEqual(modutils.get_module_part('math.log10', __file__), 'math')
self.assertEqual(modutils.get_module_part("math.log10"), "math")
self.assertEqual(modutils.get_module_part("math.log10", __file__), "math")
def test_knownValues_get_builtin_module_part(self):
self.assertEqual(modutils.get_module_part('sys.path'), 'sys')
self.assertEqual(modutils.get_module_part('sys.path', '__file__'), 'sys')
self.assertEqual(modutils.get_module_part("sys.path"), "sys")
self.assertEqual(modutils.get_module_part("sys.path", "__file__"), "sys")
def test_get_module_part_exception(self):
self.assertRaises(ImportError, modutils.get_module_part, 'unknown.module',
modutils.__file__)
self.assertRaises(
ImportError, modutils.get_module_part, "unknown.module", modutils.__file__
)
class modpath_from_file_tc(ModutilsTestCase):
......@@ -132,24 +140,25 @@ class modpath_from_file_tc(ModutilsTestCase):
)
def test_knownValues_modpath_from_file_2(self):
self.assertEqual(modutils.modpath_from_file('unittest_modutils.py',
{getcwd(): 'arbitrary.pkg'}),
['arbitrary', 'pkg', 'unittest_modutils'])
self.assertEqual(
modutils.modpath_from_file("unittest_modutils.py", {getcwd(): "arbitrary.pkg"}),
["arbitrary", "pkg", "unittest_modutils"],
)
def test_raise_modpath_from_file_Exception(self):
self.assertRaises(Exception, modutils.modpath_from_file, '/turlututu')
self.assertRaises(Exception, modutils.modpath_from_file, "/turlututu")
class load_module_from_path_tc(ModutilsTestCase):
def test_do_not_load_twice(self):
sys.path.insert(0, self.datadir)
foo = modutils.load_module_from_modpath(['lmfp', 'foo'])
lmfp = modutils.load_module_from_modpath(['lmfp'])
modutils.load_module_from_modpath(["lmfp", "foo"])
modutils.load_module_from_modpath(["lmfp"])
self.assertEqual(len(sys.just_once), 1)
sys.path.pop(0)
del sys.just_once
class file_from_modpath_tc(ModutilsTestCase):
"""given a mod path (i.e. splited module / package name), return the
corresponding file, giving priority to source file over precompiled file
......@@ -157,13 +166,19 @@ class file_from_modpath_tc(ModutilsTestCase):
def test_site_packages(self):
from pytz import tzinfo
self.assertEqual(path.realpath(modutils.file_from_modpath(['pytz', 'tzinfo'])),
path.realpath(tzinfo.__file__.replace('.pyc', '.py')))
self.assertEqual(
path.realpath(modutils.file_from_modpath(["pytz", "tzinfo"])),
path.realpath(tzinfo.__file__.replace(".pyc", ".py")),
)
def test_std_lib(self):
from os import path
self.assertEqual(path.realpath(modutils.file_from_modpath(['os', 'path']).replace('.pyc', '.py')),
path.realpath(path.__file__.replace('.pyc', '.py')))
self.assertEqual(
path.realpath(modutils.file_from_modpath(["os", "path"]).replace(".pyc", ".py")),
path.realpath(path.__file__.replace(".pyc", ".py")),
)
def test_xmlplus(self):
try:
......@@ -172,27 +187,31 @@ class file_from_modpath_tc(ModutilsTestCase):
except ImportError:
pass
else:
self.assertEqual(path.realpath(modutils.file_from_modpath(['xml', 'dom', 'ext']).replace('.pyc', '.py')),
path.realpath(ext.__file__.replace('.pyc', '.py')))
self.assertEqual(
path.realpath(
modutils.file_from_modpath(["xml", "dom", "ext"]).replace(".pyc", ".py")
),
path.realpath(ext.__file__.replace(".pyc", ".py")),
)
def test_builtin(self):
self.assertEqual(modutils.file_from_modpath(['sys']),
None)
self.assertEqual(modutils.file_from_modpath(["sys"]), None)
def test_unexisting(self):
self.assertRaises(ImportError, modutils.file_from_modpath, ['turlututu'])
self.assertRaises(ImportError, modutils.file_from_modpath, ["turlututu"])
class get_source_file_tc(ModutilsTestCase):
def test(self):
from os import path
self.assertEqual(modutils.get_source_file(path.__file__),
path.__file__.replace('.pyc', '.py'))
self.assertEqual(
modutils.get_source_file(path.__file__), path.__file__.replace(".pyc", ".py")
)
def test_raise(self):
self.assertRaises(modutils.NoSourceFile, modutils.get_source_file, 'whatever')
self.assertRaises(modutils.NoSourceFile, modutils.get_source_file, "whatever")
class is_standard_module_tc(ModutilsTestCase):
"""
......@@ -202,102 +221,119 @@ class is_standard_module_tc(ModutilsTestCase):
def test_builtins(self):
if sys.version_info < (3, 0):
self.assertEqual(modutils.is_standard_module('__builtin__'), True)
self.assertEqual(modutils.is_standard_module('builtins'), False)
self.assertEqual(modutils.is_standard_module("__builtin__"), True)
self.assertEqual(modutils.is_standard_module("builtins"), False)
else:
self.assertEqual(modutils.is_standard_module('__builtin__'), False)
self.assertEqual(modutils.is_standard_module('builtins'), True)
self.assertEqual(modutils.is_standard_module("__builtin__"), False)
self.assertEqual(modutils.is_standard_module("builtins"), True)
def test_builtin(self):
self.assertEqual(modutils.is_standard_module('sys'), True)
self.assertEqual(modutils.is_standard_module("sys"), True)
def test_nonstandard(self):
self.assertEqual(modutils.is_standard_module('logilab'), False)
self.assertEqual(modutils.is_standard_module("logilab"), False)
def test_unknown(self):
self.assertEqual(modutils.is_standard_module('unknown'), False)
self.assertEqual(modutils.is_standard_module("unknown"), False)
def test_4(self):
self.assertEqual(modutils.is_standard_module('marshal'), True)
self.assertEqual(modutils.is_standard_module('pickle'), True)
self.assertEqual(modutils.is_standard_module('email'), True)
self.assertEqual(modutils.is_standard_module('StringIO'), sys.version_info < (3, 0))
venv_py3 = sys.version_info[0] >= 3 and hasattr(sys, 'real_prefix')
self.assertEqual(modutils.is_standard_module("marshal"), True)
self.assertEqual(modutils.is_standard_module("pickle"), True)
self.assertEqual(modutils.is_standard_module("email"), True)
self.assertEqual(modutils.is_standard_module("StringIO"), sys.version_info < (3, 0))
venv_py3 = sys.version_info[0] >= 3 and hasattr(sys, "real_prefix")
if not venv_py3:
# those modules are symlinked by virtualenv (but not by python's venv)
self.assertEqual(modutils.is_standard_module('hashlib'), True)
self.assertEqual(modutils.is_standard_module('io'), True)
self.assertEqual(modutils.is_standard_module("hashlib"), True)
self.assertEqual(modutils.is_standard_module("io"), True)
def test_custom_path(self):
self.assertEqual(modutils.is_standard_module('data.module', (DATADIR,)), True)
self.assertEqual(modutils.is_standard_module('data.module', (path.abspath(DATADIR),)), True)
self.assertEqual(modutils.is_standard_module("data.module", (DATADIR,)), True)
self.assertEqual(modutils.is_standard_module("data.module", (path.abspath(DATADIR),)), True)
def test_failing_border_cases(self):
# using a subpackage/submodule path as std_path argument
self.assertEqual(modutils.is_standard_module('logilab.common', common.__path__), False)
self.assertEqual(modutils.is_standard_module("logilab.common", common.__path__), False)
# using a module + object name as modname argument
self.assertEqual(modutils.is_standard_module('sys.path'), True)
self.assertEqual(modutils.is_standard_module("sys.path"), True)
# this is because only the first package/module is considered
self.assertEqual(modutils.is_standard_module('sys.whatever'), True)
self.assertEqual(modutils.is_standard_module('logilab.whatever', common.__path__), False)
self.assertEqual(modutils.is_standard_module("sys.whatever"), True)
self.assertEqual(modutils.is_standard_module("logilab.whatever", common.__path__), False)
class is_relative_tc(ModutilsTestCase):
def test_knownValues_is_relative_1(self):
self.assertEqual(modutils.is_relative('modutils', common.__path__[0]), True)
self.assertEqual(modutils.is_relative("modutils", common.__path__[0]), True)
def test_knownValues_is_relative_2(self):
self.assertEqual(modutils.is_relative('modutils', tree.__file__), True)
self.assertEqual(modutils.is_relative("modutils", tree.__file__), True)
def test_knownValues_is_relative_3(self):
self.assertEqual(modutils.is_relative('logilab.common.modutils',
common.__path__[0]), False)
self.assertEqual(modutils.is_relative("logilab.common.modutils", common.__path__[0]), False)
class get_modules_tc(ModutilsTestCase):
def test_knownValues_get_modules_1(self): # XXXFIXME: TOWRITE
class get_modules_tc(ModutilsTestCase):
def test_knownValues_get_modules_1(self): # XXXFIXME: TOWRITE
"""given a directory return a list of all available python modules, even
in subdirectories
"""
import data.find_test as data
mod_path = ("data", 'find_test')
modules = sorted(modutils.get_modules(path.join(*mod_path),
data.__path__[0]))
self.assertSetEqual(set(modules),
set([ '.'.join(mod_path + (mod, )) for mod in ('module', 'module2',
'noendingnewline', 'nonregr')]))
mod_path = ("data", "find_test")
modules = sorted(modutils.get_modules(path.join(*mod_path), data.__path__[0]))
self.assertSetEqual(
set(modules),
set(
[
".".join(mod_path + (mod,))
for mod in ("module", "module2", "noendingnewline", "nonregr")
]
),
)
class get_modules_files_tc(ModutilsTestCase):
def test_knownValues_get_module_files_1(self): # XXXFIXME: TOWRITE
class get_modules_files_tc(ModutilsTestCase):
def test_knownValues_get_module_files_1(self): # XXXFIXME: TOWRITE
"""given a directory return a list of all available python module's files, even
in subdirectories
"""
import data
modules = sorted(modutils.get_module_files(path.join(DATADIR, 'find_test'),
data.__path__[0]))
self.assertEqual(modules,
[path.join(DATADIR, 'find_test', x) for x in ['__init__.py', 'module.py', 'module2.py', 'noendingnewline.py', 'nonregr.py']])
modules = sorted(
modutils.get_module_files(path.join(DATADIR, "find_test"), data.__path__[0])
)
self.assertEqual(
modules,
[
path.join(DATADIR, "find_test", x)
for x in [
"__init__.py",
"module.py",
"module2.py",
"noendingnewline.py",
"nonregr.py",
]
],
)
def test_load_module_set_attribute(self):
import logilab.common.fileutils
import logilab
del logilab.common.fileutils
del sys.modules['logilab.common.fileutils']
m = modutils.load_module_from_modpath(['logilab', 'common', 'fileutils'])
self.assertTrue( hasattr(logilab, 'common') )
self.assertTrue( hasattr(logilab.common, 'fileutils') )
self.assertTrue( m is logilab.common.fileutils )
del sys.modules["logilab.common.fileutils"]
m = modutils.load_module_from_modpath(["logilab", "common", "fileutils"])
self.assertTrue(hasattr(logilab, "common"))
self.assertTrue(hasattr(logilab.common, "fileutils"))
self.assertTrue(m is logilab.common.fileutils)
def load_tests(loader, tests, ignore):
from logilab.common import modutils
tests.addTests(doctest.DocTestSuite(modutils))
return tests
if __name__ == '__main__':
if __name__ == "__main__":
unittest_main()
......@@ -44,10 +44,12 @@ class _1_(Predicate):
def __call__(self, *args, **kwargs):
return 1
class _0_(Predicate):
def __call__(self, *args, **kwargs):
return 0
def _2_(*args, **kwargs):
return 2
......@@ -114,7 +116,7 @@ class SelectorsTC(TestCase):
self.assertIs(csel.search_selector(_1_), sel)
self.assertIs(csel.search_selector((AndPredicate, OrPredicate)), csel)
self.assertIs(csel.search_selector((OrPredicate, AndPredicate)), csel)
self.assertIs(csel.search_selector((_1_, _0_)), sel)
self.assertIs(csel.search_selector((_1_, _0_)), sel)
self.assertIs(csel.search_selector((_0_, _1_)), sel)
def test_inplace_and(self):
......@@ -157,74 +159,80 @@ class SelectorsTC(TestCase):
class _temp_(Predicate):
def __call__(self, *args, **kwargs):
return 0
del _temp_ # test weakref
del _temp_ # test weakref
s1 = _1_() & _1_()
s2 = _1_() & _0_()
s3 = _0_() & _1_()
gc.collect()
self.count = 0
def decorate(f, self=self):
def wrapper(*args, **kwargs):
self.count += 1
return f(*args, **kwargs)
return wrapper
wrap_predicates(decorate)
self.assertEqual(s1(None), 2)
self.assertEqual(s2(None), 0)
self.assertEqual(s3(None), 0)
self.assertEqual(self.count, 8)
@contextmanager
def prepended_syspath(path):
sys.path.insert(0, path)
yield
sys.path = sys.path[1:]
class RegistryStoreTC(TestCase):
class RegistryStoreTC(TestCase):
def test_autoload(self):
store = RegistryStore()
store.setdefault('zereg')
store.setdefault("zereg")
with prepended_syspath(self.datadir):
with warnings.catch_warnings(record=True) as warns:
store.register_objects([self.datapath('regobjects.py'),
self.datapath('regobjects2.py')])
self.assertEqual(['zereg'], list(store.keys()))
self.assertEqual(set(('appobject1', 'appobject2', 'appobject3')),
set(store['zereg']))
store.register_objects(
[self.datapath("regobjects.py"), self.datapath("regobjects2.py")]
)
self.assertIn(
"[logilab.common.registry] use register_modnames() instead",
[str(w.message) for w in warns],
)
self.assertEqual(["zereg"], list(store.keys()))
self.assertEqual(set(("appobject1", "appobject2", "appobject3")), set(store["zereg"]))
def test_autoload_modnames(self):
store = RegistryStore()
store.setdefault('zereg')
store.setdefault("zereg")
with prepended_syspath(self.datadir):
store.register_modnames(['regobjects', 'regobjects2'])
self.assertEqual(['zereg'], list(store.keys()))
self.assertEqual(set(('appobject1', 'appobject2', 'appobject3')),
set(store['zereg']))