Skip to content

Commit 39c4ee6

Browse files
committed
Use asottile.cached_property.
1 parent 619bca2 commit 39c4ee6

7 files changed

Lines changed: 5 additions & 45 deletions

File tree

pre_commit/manifest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os.path
2+
from asottile.cached_property import cached_property
23

34
import pre_commit.constants as C
45
from pre_commit.clientlib.validate_manifest import load_manifest
5-
from pre_commit.util import cached_property
66

77

88
class Manifest(object):

pre_commit/repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
from asottile.cached_property import cached_property
12
from asottile.ordereddict import OrderedDict
23

34
from pre_commit.languages.all import languages
45
from pre_commit.manifest import Manifest
56
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
6-
from pre_commit.util import cached_property
77

88

99
class Repository(object):

pre_commit/runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import os
22
import os.path
3+
from asottile.cached_property import cached_property
34

45
import pre_commit.constants as C
56
from pre_commit import git
67
from pre_commit.clientlib.validate_config import load_config
78
from pre_commit.repository import Repository
89
from pre_commit.store import Store
9-
from pre_commit.util import cached_property
1010

1111

1212
class Runner(object):

pre_commit/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import os
66
import os.path
77
import tempfile
8+
from asottile.cached_property import cached_property
89
from plumbum import local
910

1011
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
11-
from pre_commit.util import cached_property
1212
from pre_commit.util import clean_path_on_failure
1313

1414

pre_commit/util.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,6 @@
66
import sys
77

88

9-
class cached_property(object):
10-
"""Like @property, but caches the value."""
11-
12-
def __init__(self, func):
13-
self.__name__ = func.__name__
14-
self.__module__ = func.__module__
15-
self.__doc__ = func.__doc__
16-
self._func = func
17-
18-
def __get__(self, obj, cls):
19-
if obj is None:
20-
return self
21-
value = self._func(obj)
22-
obj.__dict__[self.__name__] = value
23-
return value
24-
25-
269
def memoize_by_cwd(func):
2710
"""Memoize a function call based on os.getcwd()."""
2811
@functools.wraps(func)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
install_requires=[
3131
'argparse',
32+
'asottile.cached_property',
3233
'asottile.ordereddict',
3334
'asottile.yaml',
3435
'jsonschema',

tests/util_test.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,11 @@
66
import sys
77
from plumbum import local
88

9-
from pre_commit.util import cached_property
109
from pre_commit.util import clean_path_on_failure
1110
from pre_commit.util import entry
1211
from pre_commit.util import memoize_by_cwd
1312

1413

15-
@pytest.fixture
16-
def class_with_cached_property():
17-
class Foo(object):
18-
@cached_property
19-
def my_property(self):
20-
return "Foo" + str(random.getrandbits(64))
21-
22-
return Foo
23-
24-
25-
def test_cached_property(class_with_cached_property):
26-
instance = class_with_cached_property()
27-
val = instance.my_property
28-
val2 = instance.my_property
29-
assert val is val2
30-
31-
32-
def test_unbound_cached_property(class_with_cached_property):
33-
# Make sure we don't blow up when accessing the property unbound
34-
prop = class_with_cached_property.my_property
35-
assert isinstance(prop, cached_property)
36-
37-
3814
@pytest.fixture
3915
def memoized_by_cwd():
4016
@memoize_by_cwd

0 commit comments

Comments
 (0)