diff options
author | Florian Bruhin <me@the-compiler.org> | 2015-07-25 15:16:56 +0200 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2015-07-25 15:16:56 +0200 |
commit | a9582e447362cac362e20c0ca4d30ec0221bfec2 (patch) | |
tree | 83e119483c59e84cf1dac944a61cfdb13e81247f | |
parent | 1f6e4d633932f938224dbdf40fb29efb41ba0c10 (diff) | |
download | astroid-git-a9582e447362cac362e20c0ca4d30ec0221bfec2.tar.gz |
Get rid of astroid.common.decorators.
This also implements an easier @cache decorator and removes @classproperty as
it only was used in an insignificant test.
--HG--
branch : no-logilab-common
-rw-r--r-- | astroid/bases.py | 3 | ||||
-rw-r--r-- | astroid/decorators.py | 78 | ||||
-rw-r--r-- | astroid/mixins.py | 3 | ||||
-rw-r--r-- | astroid/node_classes.py | 2 | ||||
-rw-r--r-- | astroid/objects.py | 2 | ||||
-rw-r--r-- | astroid/scoped_nodes.py | 2 | ||||
-rw-r--r-- | astroid/tests/unittest_builder.py | 13 |
7 files changed, 83 insertions, 20 deletions
diff --git a/astroid/bases.py b/astroid/bases.py index e756057f..163d49dd 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -24,10 +24,9 @@ __docformat__ = "restructuredtext en" import sys from contextlib import contextmanager -from logilab.common.decorators import cachedproperty - from astroid.exceptions import (InferenceError, AstroidError, NotFoundError, UnresolvableName, UseInferenceDefault) +from astroid.decorators import cachedproperty if sys.version_info >= (3, 0): diff --git a/astroid/decorators.py b/astroid/decorators.py new file mode 100644 index 00000000..36c9caee --- /dev/null +++ b/astroid/decorators.py @@ -0,0 +1,78 @@ +# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +# +# This file is part of astroid. +# +# astroid is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by the +# Free Software Foundation, either version 2.1 of the License, or (at your +# option) any later version. +# +# astroid is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with astroid. If not, see <http://www.gnu.org/licenses/>. +# +# The code in this file was originally part of logilab-common, licensed under +# the same license. + +""" A few useful function/method decorators. """ +__docformat__ = "restructuredtext en" + +import functools + +def cached(func): + """Simple decorator to cache result of method calls without args.""" + + @functools.wraps(func) + def wrapped(wrapped_self): + if not getattr(wrapped_self, '__cache', None): + wrapped_self.__cache = {} + if func in wrapped_self.__cache: + return wrapped_self.__cache[func] + else: + result = func(wrapped_self) + wrapped_self.__cache[func] = result + return result + + return wrapped + +class cachedproperty(object): + """ Provides a cached property equivalent to the stacking of + @cached and @property, but more efficient. + + After first usage, the <property_name> becomes part of the object's + __dict__. Doing: + + del obj.<property_name> empties the cache. + + Idea taken from the pyramid_ framework and the mercurial_ project. + + .. _pyramid: http://pypi.python.org/pypi/pyramid + .. _mercurial: http://pypi.python.org/pypi/Mercurial + """ + __slots__ = ('wrapped',) + + def __init__(self, wrapped): + try: + wrapped.__name__ + except AttributeError: + raise TypeError('%s must have a __name__ attribute' % + wrapped) + self.wrapped = wrapped + + @property + def __doc__(self): + doc = getattr(self.wrapped, '__doc__', None) + return ('<wrapped by the cachedproperty decorator>%s' + % ('\n%s' % doc if doc else '')) + + def __get__(self, inst, objtype=None): + if inst is None: + return self + val = self.wrapped(inst) + setattr(inst, self.wrapped.__name__, val) + return val diff --git a/astroid/mixins.py b/astroid/mixins.py index e7021906..ba36fbb9 100644 --- a/astroid/mixins.py +++ b/astroid/mixins.py @@ -18,10 +18,9 @@ """This module contains some mixins for the different nodes. """ -from logilab.common.decorators import cachedproperty - from astroid.exceptions import (AstroidBuildingException, InferenceError, NotFoundError) +from astroid.decorators import cachedproperty class BlockRangeMixIn(object): diff --git a/astroid/node_classes.py b/astroid/node_classes.py index dd770bd0..a838fcd5 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -19,7 +19,6 @@ """ import six -from logilab.common.decorators import cachedproperty from astroid.exceptions import ( NoDefault, UnaryOperationError, @@ -29,6 +28,7 @@ from astroid.bases import (NodeNG, Statement, Instance, InferenceContext, _infer_stmts, YES, BUILTINS) from astroid.mixins import (BlockRangeMixIn, AssignTypeMixin, ParentAssignTypeMixin, FromImportMixIn) +from astroid.decorators import cachedproperty def unpack_infer(stmt, context=None): diff --git a/astroid/objects.py b/astroid/objects.py index b3f3caea..aeb4ecd9 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -26,7 +26,6 @@ leads to an inferred FrozenSet: """ -from logilab.common.decorators import cachedproperty import six from astroid import MANAGER @@ -41,6 +40,7 @@ from astroid.exceptions import ( from astroid.node_classes import const_factory from astroid.scoped_nodes import Class, Function from astroid.mixins import ParentAssignTypeMixin +from astroid.decorators import cachedproperty class FrozenSet(NodeNG, Instance, ParentAssignTypeMixin): diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 11dcee0c..6d05af98 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -29,13 +29,13 @@ import sys import warnings import six -from logilab.common import decorators as decorators_mod from astroid import bases from astroid import exceptions from astroid import manager from astroid import mixins from astroid import node_classes +from astroid import decorators as decorators_mod BUILTINS = six.moves.builtins.__name__ diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index 5719cb12..ab1f8029 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -567,19 +567,6 @@ class BuilderTest(unittest.TestCase): self.assertIsInstance(chain, nodes.Const) self.assertEqual(chain.value, 'None') - def test_lgc_classproperty(self): - '''test expected values of constants after rebuilding''' - code = ''' - from logilab.common.decorators import classproperty - - class A(object): - @classproperty - def hop(cls): #@ - return None - ''' - method = test_utils.extract_node(code) - self.assertEqual('classmethod', method.type) - def test_not_implemented(self): node = test_utils.extract_node(''' NotImplemented #@ |