Skip to content

Commit

Permalink
Used cached_properly instead of cached_attribute
Browse files Browse the repository at this point in the history
See #298
  • Loading branch information
Carreau committed Sep 20, 2024
1 parent 7fcf962 commit 8a8ee1f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
28 changes: 13 additions & 15 deletions lib/python/pyflyby/_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
22 changes: 11 additions & 11 deletions lib/python/pyflyby/_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -1188,7 +1188,7 @@ def compile(self, mode=None):
filename = str(self.filename or "<unknown>")
return compile(ast_node, filename, mode)

@cached_attribute
@cached_property
def statements(self) -> Tuple[PythonStatement, ...]:
r"""
Partition of this ``PythonBlock`` into individual ``PythonStatement`` s.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8a8ee1f

Please sign in to comment.