mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-20 01:24:42 +04:00
Merge pull request #97 from pre-commit/use_asottile_cached_property
Use asottile.cached_property.
This commit is contained in:
commit
3baae2fd1e
7 changed files with 5 additions and 45 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
import os.path
|
import os.path
|
||||||
|
from asottile.cached_property import cached_property
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit.clientlib.validate_manifest import load_manifest
|
from pre_commit.clientlib.validate_manifest import load_manifest
|
||||||
from pre_commit.util import cached_property
|
|
||||||
|
|
||||||
|
|
||||||
class Manifest(object):
|
class Manifest(object):
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
|
from asottile.cached_property import cached_property
|
||||||
from asottile.ordereddict import OrderedDict
|
from asottile.ordereddict import OrderedDict
|
||||||
|
|
||||||
from pre_commit.languages.all import languages
|
from pre_commit.languages.all import languages
|
||||||
from pre_commit.manifest import Manifest
|
from pre_commit.manifest import Manifest
|
||||||
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
|
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
|
||||||
from pre_commit.util import cached_property
|
|
||||||
|
|
||||||
|
|
||||||
class Repository(object):
|
class Repository(object):
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
|
from asottile.cached_property import cached_property
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit import git
|
from pre_commit import git
|
||||||
from pre_commit.clientlib.validate_config import load_config
|
from pre_commit.clientlib.validate_config import load_config
|
||||||
from pre_commit.repository import Repository
|
from pre_commit.repository import Repository
|
||||||
from pre_commit.store import Store
|
from pre_commit.store import Store
|
||||||
from pre_commit.util import cached_property
|
|
||||||
|
|
||||||
|
|
||||||
class Runner(object):
|
class Runner(object):
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,10 @@ import logging
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import tempfile
|
import tempfile
|
||||||
|
from asottile.cached_property import cached_property
|
||||||
from plumbum import local
|
from plumbum import local
|
||||||
|
|
||||||
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
|
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
|
||||||
from pre_commit.util import cached_property
|
|
||||||
from pre_commit.util import clean_path_on_failure
|
from pre_commit.util import clean_path_on_failure
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,23 +6,6 @@ import shutil
|
||||||
import sys
|
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):
|
def memoize_by_cwd(func):
|
||||||
"""Memoize a function call based on os.getcwd()."""
|
"""Memoize a function call based on os.getcwd()."""
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
|
|
|
||||||
1
setup.py
1
setup.py
|
|
@ -29,6 +29,7 @@ setup(
|
||||||
},
|
},
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'argparse',
|
'argparse',
|
||||||
|
'asottile.cached_property',
|
||||||
'asottile.ordereddict',
|
'asottile.ordereddict',
|
||||||
'asottile.yaml',
|
'asottile.yaml',
|
||||||
'jsonschema',
|
'jsonschema',
|
||||||
|
|
|
||||||
|
|
@ -6,35 +6,11 @@ import random
|
||||||
import sys
|
import sys
|
||||||
from plumbum import local
|
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 clean_path_on_failure
|
||||||
from pre_commit.util import entry
|
from pre_commit.util import entry
|
||||||
from pre_commit.util import memoize_by_cwd
|
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
|
@pytest.fixture
|
||||||
def memoized_by_cwd():
|
def memoized_by_cwd():
|
||||||
@memoize_by_cwd
|
@memoize_by_cwd
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue