diff --git a/lib/python/pyflyby/_modules.py b/lib/python/pyflyby/_modules.py index 87d5e9b9..4a1b24a8 100644 --- a/lib/python/pyflyby/_modules.py +++ b/lib/python/pyflyby/_modules.py @@ -2,20 +2,18 @@ # Copyright (C) 2011, 2012, 2013, 2014, 2015 Karl Chen. # License: MIT http://opensource.org/licenses/MIT - - +from __future__ import print_function import ast -from functools import total_ordering +from functools import cached_property, total_ordering import itertools import os from pyflyby._file import FileText, Filename from pyflyby._idents import DottedIdentifier, is_identifier from pyflyby._log import logger -from pyflyby._util import (ExcludeImplicitCwdFromPathCtx, - cached_attribute, cmp, memoize, - prefixes) +from pyflyby._util import (ExcludeImplicitCwdFromPathCtx, cmp, + memoize, prefixes) import re from six import reraise @@ -167,17 +165,17 @@ def _from_filename(cls, filename): raise NotImplementedError( "TODO: look at sys.path to guess module name") - @cached_attribute + @cached_property def parent(self): if not self.name.parent: return None return ModuleHandle(self.name.parent) - @cached_attribute + @cached_property def ancestors(self): return tuple(ModuleHandle(m) for m in self.name.prefixes) - @cached_attribute + @cached_property def module(self): """ Return the module instance. @@ -196,7 +194,7 @@ def module(self): # Import. return import_module(self.name) - @cached_attribute + @cached_property def exists(self): """ Return whether the module exists, according to pkgutil. @@ -221,7 +219,7 @@ def exists(self): pkg = None return pkg is not None - @cached_attribute + @cached_property def filename(self): """ Return the filename, if appropriate. @@ -268,14 +266,14 @@ def filename(self): return None return Filename(pyc_to_py(filename)) - @cached_attribute + @cached_property def text(self): return FileText(self.filename) def __text__(self): return self.text - @cached_attribute + @cached_property def block(self): from pyflyby._parse import PythonBlock return PythonBlock(self.text) @@ -309,7 +307,7 @@ def list(): # Canonicalize. return tuple(ModuleHandle(m) for m in sorted(set(module_names))) - @cached_attribute + @cached_property def submodules(self): """ Enumerate the importable submodules of this module. @@ -349,7 +347,7 @@ def _member_from_node(node): return extractors[type(node)](node) return [] - @cached_attribute + @cached_property def exports(self): """ Get symbols exported by this module. diff --git a/lib/python/pyflyby/_parse.py b/lib/python/pyflyby/_parse.py index c41fd6e0..257419f6 100644 --- a/lib/python/pyflyby/_parse.py +++ b/lib/python/pyflyby/_parse.py @@ -8,13 +8,13 @@ from collections import namedtuple from doctest import DocTestParser -from functools import total_ordering +from functools import cached_property, total_ordering from itertools import groupby from pyflyby._file import FilePos, FileText, Filename from pyflyby._flags import CompilerFlags from pyflyby._log import logger -from pyflyby._util import cached_attribute, cmp +from pyflyby._util import cmp import re import sys @@ -1044,7 +1044,7 @@ def startpos(self): def endpos(self): return self.text.endpos - @cached_attribute + @cached_property def _ast_node_or_parse_exception(self): """ Attempt to parse this block of code into an abstract syntax tree. @@ -1069,7 +1069,7 @@ def _ast_node_or_parse_exception(self): # Cache the exception to avoid re-attempting while debugging. return e - @cached_attribute + @cached_property def parsable(self): """ Whether the contents of this ``PythonBlock`` are parsable as Python @@ -1080,7 +1080,7 @@ def parsable(self): """ return isinstance(self._ast_node_or_parse_exception, ast.AST) - @cached_attribute + @cached_property def parsable_as_expression(self): """ Whether the contents of this ``PythonBlock`` are parsable as a single @@ -1091,7 +1091,7 @@ def parsable_as_expression(self): """ return self.parsable and self.expression_ast_node is not None - @cached_attribute + @cached_property def ast_node(self): """ Parse this block of code into an abstract syntax tree. @@ -1112,7 +1112,7 @@ def ast_node(self): else: raise r - @cached_attribute + @cached_property def annotated_ast_node(self) -> AnnotatedAst: """ Return ``self.ast_node``, annotated in place with positions. @@ -1127,7 +1127,7 @@ def annotated_ast_node(self) -> AnnotatedAst: # ! result is mutated and returned return _annotate_ast_nodes(result) - @cached_attribute + @cached_property def expression_ast_node(self) -> Optional[ast.Expression]: """ Return an ``ast.Expression`` if ``self.ast_node`` can be converted into @@ -1188,7 +1188,7 @@ def compile(self, mode=None): filename = str(self.filename or "") return compile(ast_node, filename, mode) - @cached_attribute + @cached_property def statements(self) -> Tuple[PythonStatement, ...]: r""" Partition of this ``PythonBlock`` into individual ``PythonStatement`` s. @@ -1245,7 +1245,7 @@ def statements(self) -> Tuple[PythonStatement, ...]: statements.append(statement) return tuple(statements) - @cached_attribute + @cached_property def source_flags(self): """ If the AST contains __future__ imports, then the compiler_flags @@ -1261,7 +1261,7 @@ def source_flags(self): """ return self.ast_node.source_flags - @cached_attribute + @cached_property def flags(self): """ The compiler flags for this code block, including both the input flags