diff options
| author | Ceridwen <ceridwenv@gmail.com> | 2015-11-20 18:10:38 -0500 |
|---|---|---|
| committer | Ceridwen <ceridwenv@gmail.com> | 2015-11-20 18:10:38 -0500 |
| commit | 96228676c3692f341789f533406f2ae2d0ece4ff (patch) | |
| tree | 71b708b03ab0c5bea422e7acb68fab9ee678568b | |
| parent | 724b82119e6ab2f98eefacabc8809544a6962190 (diff) | |
| download | astroid-git-96228676c3692f341789f533406f2ae2d0ece4ff.tar.gz | |
Add deprecations for node attributes in scoped_nodes
--HG--
branch : 1.4.0
| -rw-r--r-- | astroid/builder.py | 2 | ||||
| -rw-r--r-- | astroid/scoped_nodes.py | 91 | ||||
| -rw-r--r-- | astroid/util.py | 24 |
3 files changed, 109 insertions, 8 deletions
diff --git a/astroid/builder.py b/astroid/builder.py index fcd9892f..d2773896 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -144,7 +144,7 @@ class AstroidBuilder(raw_building.InspectBuilder): def string_build(self, data, modname='', path=None): """Build astroid from source code string.""" module = self._data_build(data, modname, path) - module.file_bytes = data.encode('utf-8') + module.source_code = data.encode('utf-8') return self._post_build(module, 'utf-8') def _post_build(self, module, encoding): diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index d2daba50..2378a10d 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -146,6 +146,18 @@ class LocalsDictNodeNG(node_classes.LookupMixIn, bases.NodeNG): # dictionary of locals with name as key and node defining the local as # value + @property + def locals(self): + util.attribute_to_function_warning('locals', 2.0, 'get_locals') + return self._locals + @locals.setter + def locals(self, _locals): + util.attribute_to_function_warning('locals', 2.0, 'get_locals') + self._locals = _locals + @locals.deleter + def locals(self): + util.attribute_to_function_warning('locals', 2.0, 'get_locals') + del self._locals def qname(self): """return the 'qualified' name of the node, eg module.name, @@ -256,9 +268,9 @@ class Module(LocalsDictNodeNG): # the file from which as been extracted the astroid representation. It may # be None if the representation has been built from a built-in module - file = None + source_file = None # Alternatively, if built from a string/bytes, this can be set - file_bytes = None + source_code = None # encoding of python source file, so we can get unicode out of it (python2 # only) file_encoding = None @@ -270,10 +282,10 @@ class Module(LocalsDictNodeNG): package = None # dictionary of globals with name as key and node defining the global # as value - globals = None + _globals = None # Future imports - future_imports = None + _future_imports = None # names of python special attributes (handled by getattr impl.) special_attributes = set(('__name__', '__doc__', '__file__', '__path__', @@ -289,9 +301,75 @@ class Module(LocalsDictNodeNG): self.body = [] self.future_imports = set() + # Future deprecation warnings + @property + def file(self): + util.rename_warning('file', 2.0, 'source_file') + return self.source_file + @file.setter + def file(self, source_file): + util.rename_warning('file', 2.0, 'source_file') + self.source_file = source_file + @file.deleter + def file(self): + util.rename_warning('file', 2.0, 'source_file') + del self.source_file + + @property + def path(self): + util.rename_warning('path', 2.0, 'source_file') + return self.source_file + @path.setter + def path(self, source_file): + util.rename_warning('path', 2.0, 'source_file') + self.source_file = source_file + @path.deleter + def path(self): + util.rename_warning('path', 2.0, 'source_file') + del self.source_file + + @property + def file_bytes(self): + util.rename_warning('file_bytes', 2.0, 'source_code') + return self.source_code + @file_bytes.setter + def file_bytes(self, source_code): + util.rename_warning('file_bytes', 2.0, 'source_code') + self.source_code = source_code + @file_bytes.deleter + def file_bytes(self): + util.rename_warning('file_bytes', 2.0, 'source_code') + del self.source_code + + @property + def globals(self): + util.attribute_to_function_warning('globals', 2.0, 'get_locals') + return self._locals + @globals.setter + def globals(self, _globals): + util.attribute_to_function_warning('globals', 2.0, 'get_locals') + self._locals = _globals + @globals.deleter + def globals(self): + util.attribute_to_function_warning('globals', 2.0, 'get_locals') + del self._locals + + @property + def future_imports(self): + util.attribute_to_function_warning('future_imports', 2.0, 'future_imports') + return self._future_imports + @future_imports.setter + def future_imports(self, _future_imports): + util.attribute_to_function_warning('future_imports', 2.0, 'future_imports') + self._future_imports = _future_imports + @future_imports.deleter + def future_imports(self): + util.attribute_to_function_warning('future_imports', 2.0, 'future_imports') + del self._future_imports + def _get_stream(self): - if self.file_bytes is not None: - return io.BytesIO(self.file_bytes) + if self.source_code is not None: + return io.BytesIO(self.source_code) if self.file is not None: stream = open(self.file, 'rb') return stream @@ -1225,6 +1303,7 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, bases.Statement): def instanciate_class(self): """return Instance of ClassDef node, else return self""" + util.rename_warning('instanciate_class()', 2.0, 'instantiate_class()') return bases.Instance(self) def getattr(self, name, context=None): diff --git a/astroid/util.py b/astroid/util.py index 899bb65b..2dd9d308 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -18,9 +18,31 @@ # # The code in this file was originally part of logilab-common, licensed under # the same license. +import warnings from astroid import exceptions + +def generate_warning(message, warning): + return lambda *args: warnings.warn(message % args, warning, stacklevel=3) + +rename_warning = generate_warning( + "%r is deprecated and will be removed in astroid %.1f, use %r instead", + PendingDeprecationWarning) + +attribute_to_method_warning = generate_warning( + "%s is deprecated and will be removed in astroid %.1f, use the " + "method '%s()' instead.", PendingDeprecationWarning) + +attribute_to_function_warning = generate_warning( + "%s is deprecated and will be removed in astroid %.1f, use the " + "function '%s()' instead.", PendingDeprecationWarning) + +method_to_function_warning = generate_warning( + "%s() is deprecated and will be removed in astroid %.1f, use the " + "function '%s()' instead.", PendingDeprecationWarning) + + class _Yes(object): """Special inference object, which is returned when inference fails.""" def __repr__(self): @@ -56,4 +78,4 @@ def safe_infer(node, context=None): except exceptions.InferenceError: return # there is some kind of ambiguity except StopIteration: - return value
\ No newline at end of file + return value |
