diff --git a/src/lazy_object_proxy/__init__.py b/src/lazy_object_proxy/__init__.py index 5799885..c431e17 100644 --- a/src/lazy_object_proxy/__init__.py +++ b/src/lazy_object_proxy/__init__.py @@ -20,4 +20,4 @@ except ImportError: __version__ = '1.9.0' -__all__ = ("Proxy",) +__all__ = ('Proxy',) diff --git a/src/lazy_object_proxy/compat.py b/src/lazy_object_proxy/compat.py index 770f2da..90c1cde 100644 --- a/src/lazy_object_proxy/compat.py +++ b/src/lazy_object_proxy/compat.py @@ -3,4 +3,4 @@ def with_metaclass(meta, *bases): """Create a base class with a metaclass.""" - return meta("NewBase", bases, {}) + return meta('NewBase', bases, {}) diff --git a/src/lazy_object_proxy/simple.py b/src/lazy_object_proxy/simple.py index 283894b..abf0587 100644 --- a/src/lazy_object_proxy/simple.py +++ b/src/lazy_object_proxy/simple.py @@ -14,7 +14,7 @@ def proxy_wrapper(self, *args): return proxy_wrapper -class _ProxyMethods(object): +class _ProxyMethods: # We use properties to override the values of __module__ and # __doc__. If we add these in ObjectProxy, the derived class # __dict__ will still be setup to have string variants of these @@ -95,7 +95,7 @@ def __repr__(self, __getattr__=object.__getattribute__): type(self).__name__, id(self), self.__wrapped__, id(self.__wrapped__), self.__factory__ ) else: - return '<{} at 0x{:x} with factory {!r}>'.format(type(self).__name__, id(self), self.__factory__) + return f'<{type(self).__name__} at 0x{id(self):x} with factory {self.__factory__!r}>' def __fspath__(self): wrapped = self.__wrapped__ diff --git a/src/lazy_object_proxy/slots.py b/src/lazy_object_proxy/slots.py index 4b62859..ab20a04 100644 --- a/src/lazy_object_proxy/slots.py +++ b/src/lazy_object_proxy/slots.py @@ -6,7 +6,7 @@ from .utils import identity -class _ProxyMethods(object): +class _ProxyMethods: # We use properties to override the values of __module__ and # __doc__. If we add these in ObjectProxy, the derived class # __dict__ will still be setup to have string variants of these @@ -124,8 +124,8 @@ def __name__(self, value): def __class__(self): return self.__wrapped__.__class__ - @__class__.setter # noqa: F811 - def __class__(self, value): # noqa: F811 + @__class__.setter + def __class__(self, value): self.__wrapped__.__class__ = value @property @@ -149,11 +149,9 @@ def __repr__(self, __getattr__=object.__getattribute__): try: target = __getattr__(self, '__target__') except AttributeError: - return '<{} at 0x{:x} with factory {!r}>'.format(type(self).__name__, id(self), self.__factory__) + return f'<{type(self).__name__} at 0x{id(self):x} with factory {self.__factory__!r}>' else: - return '<{} at 0x{:x} wrapping {!r} at 0x{:x} with factory {!r}>'.format( - type(self).__name__, id(self), target, id(target), self.__factory__ - ) + return f'<{type(self).__name__} at 0x{id(self):x} wrapping {target!r} at 0x{id(target):x} with factory {self.__factory__!r}>' def __fspath__(self): wrapped = self.__wrapped__ diff --git a/src/lazy_object_proxy/utils.py b/src/lazy_object_proxy/utils.py index 99945f4..8e69787 100644 --- a/src/lazy_object_proxy/utils.py +++ b/src/lazy_object_proxy/utils.py @@ -49,7 +49,7 @@ def identity(obj): return obj -class cached_property(object): +class cached_property: def __init__(self, func): self.func = func diff --git a/tests/conftest.py b/tests/conftest.py index 3f7530a..047d72e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,30 +5,30 @@ PYPY = '__pypy__' in sys.builtin_module_names -@pytest.fixture(scope="session") +@pytest.fixture(scope='session') def lop_loader(): def load_implementation(name): class FakeModule: subclass = False kind = name - if name == "slots": + if name == 'slots': from lazy_object_proxy.slots import Proxy - elif name == "simple": + elif name == 'simple': from lazy_object_proxy.simple import Proxy - elif name == "cext": + elif name == 'cext': try: from lazy_object_proxy.cext import Proxy except ImportError: if PYPY: - pytest.skip(reason="C Extension not available.") + pytest.skip(reason='C Extension not available.') else: raise - elif name == "objproxies": - Proxy = pytest.importorskip("objproxies").LazyProxy - elif name == "django": - Proxy = pytest.importorskip("django.utils.functional").SimpleLazyObject + elif name == 'objproxies': + Proxy = pytest.importorskip('objproxies').LazyProxy + elif name == 'django': + Proxy = pytest.importorskip('django.utils.functional').SimpleLazyObject else: - raise RuntimeError("Unsupported param: %r." % name) + raise RuntimeError('Unsupported param: %r.' % name) Proxy @@ -38,11 +38,11 @@ class FakeModule: @pytest.fixture( - scope="session", + scope='session', params=[ - "slots", - "cext", - "simple", + 'slots', + 'cext', + 'simple', # "external-django", "external-objproxies" ], ) @@ -50,20 +50,20 @@ def lop_implementation(request, lop_loader): return lop_loader(request.param) -@pytest.fixture(scope="session", params=[True, False], ids=['subclassed', 'normal']) +@pytest.fixture(scope='session', params=[True, False], ids=['subclassed', 'normal']) def lop_subclass(request, lop_implementation): if request.param: class submod(lop_implementation): subclass = True - Proxy = type("SubclassOf_" + lop_implementation.Proxy.__name__, (lop_implementation.Proxy,), {}) + Proxy = type('SubclassOf_' + lop_implementation.Proxy.__name__, (lop_implementation.Proxy,), {}) return submod else: return lop_implementation -@pytest.fixture(scope="function") +@pytest.fixture(scope='function') def lop(request, lop_subclass): if request.node.get_closest_marker('xfail_subclass'): request.applymarker( @@ -72,6 +72,6 @@ def lop(request, lop_subclass): ) ) if request.node.get_closest_marker('xfail_simple'): - request.applymarker(pytest.mark.xfail(reason="The lazy_object_proxy.simple.Proxy has some limitations.")) + request.applymarker(pytest.mark.xfail(reason='The lazy_object_proxy.simple.Proxy has some limitations.')) return lop_subclass diff --git a/tests/test_lazy_object_proxy.py b/tests/test_lazy_object_proxy.py index fd8274b..e11000b 100644 --- a/tests/test_lazy_object_proxy.py +++ b/tests/test_lazy_object_proxy.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import gc import os import pickle @@ -366,7 +364,7 @@ def function(*args, **kwargs): def test_function_kwargs(lop): _args = () - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} def function(*args, **kwargs): return args, kwargs @@ -380,7 +378,7 @@ def function(*args, **kwargs): def test_function_args_plus_kwargs(lop): _args = (1, 2) - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} def function(*args, **kwargs): return args, kwargs @@ -396,7 +394,7 @@ def test_instancemethod_no_args(lop): _args = () _kwargs = {} - class Class(object): + class Class: def function(self, *args, **kwargs): return args, kwargs @@ -411,7 +409,7 @@ def test_instancemethod_args(lop): _args = (1, 2) _kwargs = {} - class Class(object): + class Class: def function(self, *args, **kwargs): return args, kwargs @@ -424,9 +422,9 @@ def function(self, *args, **kwargs): def test_instancemethod_kwargs(lop): _args = () - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} - class Class(object): + class Class: def function(self, *args, **kwargs): return args, kwargs @@ -439,9 +437,9 @@ def function(self, *args, **kwargs): def test_instancemethod_args_plus_kwargs(lop): _args = (1, 2) - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} - class Class(object): + class Class: def function(self, *args, **kwargs): return args, kwargs @@ -456,7 +454,7 @@ def test_instancemethod_via_class_no_args(lop): _args = () _kwargs = {} - class Class(object): + class Class: def function(self, *args, **kwargs): return args, kwargs @@ -471,7 +469,7 @@ def test_instancemethod_via_class_args(lop): _args = (1, 2) _kwargs = {} - class Class(object): + class Class: def function(self, *args, **kwargs): return args, kwargs @@ -484,9 +482,9 @@ def function(self, *args, **kwargs): def test_instancemethod_via_class_kwargs(lop): _args = () - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} - class Class(object): + class Class: def function(self, *args, **kwargs): return args, kwargs @@ -499,9 +497,9 @@ def function(self, *args, **kwargs): def test_instancemethod_via_class_args_plus_kwargs(lop): _args = (1, 2) - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} - class Class(object): + class Class: def function(self, *args, **kwargs): return args, kwargs @@ -516,7 +514,7 @@ def test_classmethod_no_args(lop): _args = () _kwargs = {} - class Class(object): + class Class: @classmethod def function(cls, *args, **kwargs): return args, kwargs @@ -532,7 +530,7 @@ def test_classmethod_args(lop): _args = (1, 2) _kwargs = {} - class Class(object): + class Class: @classmethod def function(cls, *args, **kwargs): return args, kwargs @@ -546,9 +544,9 @@ def function(cls, *args, **kwargs): def test_classmethod_kwargs(lop): _args = () - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} - class Class(object): + class Class: @classmethod def function(cls, *args, **kwargs): return args, kwargs @@ -562,9 +560,9 @@ def function(cls, *args, **kwargs): def test_classmethod_args_plus_kwargs(lop): _args = (1, 2) - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} - class Class(object): + class Class: @classmethod def function(cls, *args, **kwargs): return args, kwargs @@ -580,7 +578,7 @@ def test_classmethod_via_class_no_args(lop): _args = () _kwargs = {} - class Class(object): + class Class: @classmethod def function(cls, *args, **kwargs): return args, kwargs @@ -596,7 +594,7 @@ def test_classmethod_via_class_args(lop): _args = (1, 2) _kwargs = {} - class Class(object): + class Class: @classmethod def function(cls, *args, **kwargs): return args, kwargs @@ -610,9 +608,9 @@ def function(cls, *args, **kwargs): def test_classmethod_via_class_kwargs(lop): _args = () - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} - class Class(object): + class Class: @classmethod def function(cls, *args, **kwargs): return args, kwargs @@ -626,9 +624,9 @@ def function(cls, *args, **kwargs): def test_classmethod_via_class_args_plus_kwargs(lop): _args = (1, 2) - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} - class Class(object): + class Class: @classmethod def function(cls, *args, **kwargs): return args, kwargs @@ -644,7 +642,7 @@ def test_staticmethod_no_args(lop): _args = () _kwargs = {} - class Class(object): + class Class: @staticmethod def function(*args, **kwargs): return args, kwargs @@ -660,7 +658,7 @@ def test_staticmethod_args(lop): _args = (1, 2) _kwargs = {} - class Class(object): + class Class: @staticmethod def function(*args, **kwargs): return args, kwargs @@ -674,9 +672,9 @@ def function(*args, **kwargs): def test_staticmethod_kwargs(lop): _args = () - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} - class Class(object): + class Class: @staticmethod def function(*args, **kwargs): return args, kwargs @@ -690,9 +688,9 @@ def function(*args, **kwargs): def test_staticmethod_args_plus_kwargs(lop): _args = (1, 2) - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} - class Class(object): + class Class: @staticmethod def function(*args, **kwargs): return args, kwargs @@ -708,7 +706,7 @@ def test_staticmethod_via_class_no_args(lop): _args = () _kwargs = {} - class Class(object): + class Class: @staticmethod def function(*args, **kwargs): return args, kwargs @@ -724,7 +722,7 @@ def test_staticmethod_via_class_args(lop): _args = (1, 2) _kwargs = {} - class Class(object): + class Class: @staticmethod def function(*args, **kwargs): return args, kwargs @@ -738,9 +736,9 @@ def function(*args, **kwargs): def test_staticmethod_via_class_kwargs(lop): _args = () - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} - class Class(object): + class Class: @staticmethod def function(*args, **kwargs): return args, kwargs @@ -754,9 +752,9 @@ def function(*args, **kwargs): def test_staticmethod_via_class_args_plus_kwargs(lop): _args = (1, 2) - _kwargs = {"one": 1, "two": 2} + _kwargs = {'one': 1, 'two': 2} - class Class(object): + class Class: @staticmethod def function(*args, **kwargs): return args, kwargs @@ -788,7 +786,7 @@ def test_iter_builtin(lop): def test_context_manager(lop): - class Class(object): + class Class: def __enter__(self): return self @@ -1313,7 +1311,7 @@ def test_hex(lop): def test_index(lop): - class Class(object): + class Class: def __index__(self): return 1 @@ -1563,7 +1561,7 @@ def __getattr__(self, name): def test_proxy_hasattr_call(lop): proxy = lop.Proxy(lambda: None) - assert not hasattr(proxy, '__call__') + assert not callable(proxy) @skipcallable @@ -1583,7 +1581,7 @@ def test_proxy_is_callable(lop): def test_callable_proxy_hasattr_call(lop): proxy = lop.Proxy(lambda: None) - assert hasattr(proxy, '__call__') + assert callable(proxy) @skipcallable @@ -1600,7 +1598,7 @@ def test_callable_proxy_is_callable(lop): def test_class_bytes(lop): - class Class(object): + class Class: def __bytes__(self): return b'BYTES' @@ -1672,7 +1670,7 @@ def make_foo(): def test_raise_attribute_error(lop): def foo(): - raise AttributeError("boom!") + raise AttributeError('boom!') proxy = lop.Proxy(foo) pytest.raises(AttributeError, str, proxy) @@ -1682,7 +1680,7 @@ def foo(): def test_patching_the_factory(lop): def foo(): - raise AttributeError("boom!") + raise AttributeError('boom!') proxy = lop.Proxy(foo) pytest.raises(AttributeError, lambda: proxy.__wrapped__) @@ -1746,15 +1744,15 @@ def test_set_wrapped_regular(lop): @pytest.fixture( params=[ - "pickle", + 'pickle', ] ) def pickler(request): return pytest.importorskip(request.param) -@pytest.mark.parametrize("obj", [1, 1.2, "a", ["b", "c"], {"d": "e"}, date(2015, 5, 1), datetime(2015, 5, 1), Decimal("1.2")]) -@pytest.mark.parametrize("level", range(pickle.HIGHEST_PROTOCOL + 1)) +@pytest.mark.parametrize('obj', [1, 1.2, 'a', ['b', 'c'], {'d': 'e'}, date(2015, 5, 1), datetime(2015, 5, 1), Decimal('1.2')]) +@pytest.mark.parametrize('level', range(pickle.HIGHEST_PROTOCOL + 1)) def test_pickling(lop, obj, pickler, level): proxy = lop.Proxy(lambda: obj) dump = pickler.dumps(proxy, protocol=level) @@ -1762,13 +1760,13 @@ def test_pickling(lop, obj, pickler, level): assert obj == result -@pytest.mark.parametrize("level", range(pickle.HIGHEST_PROTOCOL + 1)) +@pytest.mark.parametrize('level', range(pickle.HIGHEST_PROTOCOL + 1)) def test_pickling_exception(lop, pickler, level): class BadStuff(Exception): pass def trouble_maker(): - raise BadStuff("foo") + raise BadStuff('foo') proxy = lop.Proxy(trouble_maker) pytest.raises(BadStuff, pickler.dumps, proxy, protocol=level) @@ -1780,7 +1778,7 @@ def test_garbage_collection(lop): proxy = lop.Proxy(leaky) leaky.leak = proxy ref = weakref.ref(leaky) - assert proxy == "foobar" + assert proxy == 'foobar' del leaky del proxy gc.collect() @@ -1796,10 +1794,10 @@ def test_garbage_collection_count(lop): assert count == sys.getrefcount(obj) -@pytest.mark.parametrize("name", ["slots", "cext", "simple", "django", "objproxies"]) +@pytest.mark.parametrize('name', ['slots', 'cext', 'simple', 'django', 'objproxies']) def test_perf(benchmark, name, lop_loader): implementation = lop_loader(name) - obj = "foobar" + obj = 'foobar' proxied = implementation.Proxy(lambda: obj) assert benchmark(partial(str, proxied)) == obj @@ -1807,15 +1805,15 @@ def test_perf(benchmark, name, lop_loader): empty = object() -@pytest.fixture(scope="module", params=["SimpleProxy", "LocalsSimpleProxy", "CachedPropertyProxy", "LocalsCachedPropertyProxy"]) +@pytest.fixture(scope='module', params=['SimpleProxy', 'LocalsSimpleProxy', 'CachedPropertyProxy', 'LocalsCachedPropertyProxy']) def prototype(request): from lazy_object_proxy.simple import cached_property name = request.param - if name == "SimpleProxy": + if name == 'SimpleProxy': - class SimpleProxy(object): + class SimpleProxy: def __init__(self, factory): self.factory = factory self.object = empty @@ -1826,9 +1824,9 @@ def __str__(self): return str(self.object) return SimpleProxy - elif name == "CachedPropertyProxy": + elif name == 'CachedPropertyProxy': - class CachedPropertyProxy(object): + class CachedPropertyProxy: def __init__(self, factory): self.factory = factory @@ -1840,9 +1838,9 @@ def __str__(self): return str(self.object) return CachedPropertyProxy - elif name == "LocalsSimpleProxy": + elif name == 'LocalsSimpleProxy': - class LocalsSimpleProxy(object): + class LocalsSimpleProxy: def __init__(self, factory): self.factory = factory self.object = empty @@ -1853,9 +1851,9 @@ def __str__(self, func=str): return func(self.object) return LocalsSimpleProxy - elif name == "LocalsCachedPropertyProxy": + elif name == 'LocalsCachedPropertyProxy': - class LocalsCachedPropertyProxy(object): + class LocalsCachedPropertyProxy: def __init__(self, factory): self.factory = factory @@ -1869,9 +1867,9 @@ def __str__(self, func=str): return LocalsCachedPropertyProxy -@pytest.mark.benchmark(group="prototypes") +@pytest.mark.benchmark(group='prototypes') def test_proto(benchmark, prototype): - obj = "foobar" + obj = 'foobar' proxied = prototype(lambda: obj) assert benchmark(partial(str, proxied)) == obj @@ -1897,7 +1895,7 @@ def __init__(self, func, **lazy_attr): def test_subclassing_dynamic_with_local_attr(lop): if lop.kind == 'cext': - pytest.skip("Not possible.") + pytest.skip('Not possible.') class Foo: pass @@ -1915,12 +1913,12 @@ def __init__(self, func, **lazy_attr): assert not called -class FSPathMock(object): +class FSPathMock: def __fspath__(self): return '/tmp' -@pytest.mark.skipif(not hasattr(os, "fspath"), reason="No os.fspath support.") +@pytest.mark.skipif(not hasattr(os, 'fspath'), reason='No os.fspath support.') def test_fspath(lop): assert os.fspath(lop.Proxy(lambda: '/tmp')) == '/tmp' assert os.fspath(lop.Proxy(FSPathMock)) == '/tmp'