From 39c4ee6e9632a6f10615dbebf58cc4e23157464b Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 1 Jun 2014 16:41:58 -0700 Subject: [PATCH] Use asottile.cached_property. --- pre_commit/manifest.py | 2 +- pre_commit/repository.py | 2 +- pre_commit/runner.py | 2 +- pre_commit/store.py | 2 +- pre_commit/util.py | 17 ----------------- setup.py | 1 + tests/util_test.py | 24 ------------------------ 7 files changed, 5 insertions(+), 45 deletions(-) diff --git a/pre_commit/manifest.py b/pre_commit/manifest.py index 53f52c6..3b2363e 100644 --- a/pre_commit/manifest.py +++ b/pre_commit/manifest.py @@ -1,8 +1,8 @@ import os.path +from asottile.cached_property import cached_property import pre_commit.constants as C from pre_commit.clientlib.validate_manifest import load_manifest -from pre_commit.util import cached_property class Manifest(object): diff --git a/pre_commit/repository.py b/pre_commit/repository.py index 7b29dea..2fe7a00 100644 --- a/pre_commit/repository.py +++ b/pre_commit/repository.py @@ -1,9 +1,9 @@ +from asottile.cached_property import cached_property from asottile.ordereddict import OrderedDict from pre_commit.languages.all import languages from pre_commit.manifest import Manifest from pre_commit.prefixed_command_runner import PrefixedCommandRunner -from pre_commit.util import cached_property class Repository(object): diff --git a/pre_commit/runner.py b/pre_commit/runner.py index cac90b5..815c3ab 100644 --- a/pre_commit/runner.py +++ b/pre_commit/runner.py @@ -1,12 +1,12 @@ import os import os.path +from asottile.cached_property import cached_property import pre_commit.constants as C from pre_commit import git from pre_commit.clientlib.validate_config import load_config from pre_commit.repository import Repository from pre_commit.store import Store -from pre_commit.util import cached_property class Runner(object): diff --git a/pre_commit/store.py b/pre_commit/store.py index 9b58c06..c35bcba 100644 --- a/pre_commit/store.py +++ b/pre_commit/store.py @@ -5,10 +5,10 @@ import logging import os import os.path import tempfile +from asottile.cached_property import cached_property from plumbum import local from pre_commit.prefixed_command_runner import PrefixedCommandRunner -from pre_commit.util import cached_property from pre_commit.util import clean_path_on_failure diff --git a/pre_commit/util.py b/pre_commit/util.py index 5884e4d..121e280 100644 --- a/pre_commit/util.py +++ b/pre_commit/util.py @@ -6,23 +6,6 @@ import shutil import sys -class cached_property(object): - """Like @property, but caches the value.""" - - def __init__(self, func): - self.__name__ = func.__name__ - self.__module__ = func.__module__ - self.__doc__ = func.__doc__ - self._func = func - - def __get__(self, obj, cls): - if obj is None: - return self - value = self._func(obj) - obj.__dict__[self.__name__] = value - return value - - def memoize_by_cwd(func): """Memoize a function call based on os.getcwd().""" @functools.wraps(func) diff --git a/setup.py b/setup.py index 23a184d..3a5a857 100644 --- a/setup.py +++ b/setup.py @@ -29,6 +29,7 @@ setup( }, install_requires=[ 'argparse', + 'asottile.cached_property', 'asottile.ordereddict', 'asottile.yaml', 'jsonschema', diff --git a/tests/util_test.py b/tests/util_test.py index 10f2293..22aea85 100644 --- a/tests/util_test.py +++ b/tests/util_test.py @@ -6,35 +6,11 @@ import random import sys from plumbum import local -from pre_commit.util import cached_property from pre_commit.util import clean_path_on_failure from pre_commit.util import entry from pre_commit.util import memoize_by_cwd -@pytest.fixture -def class_with_cached_property(): - class Foo(object): - @cached_property - def my_property(self): - return "Foo" + str(random.getrandbits(64)) - - return Foo - - -def test_cached_property(class_with_cached_property): - instance = class_with_cached_property() - val = instance.my_property - val2 = instance.my_property - assert val is val2 - - -def test_unbound_cached_property(class_with_cached_property): - # Make sure we don't blow up when accessing the property unbound - prop = class_with_cached_property.my_property - assert isinstance(prop, cached_property) - - @pytest.fixture def memoized_by_cwd(): @memoize_by_cwd