Skip to content

Commit

Permalink
Merge pull request #317 from Carreau/lazy-isort
Browse files Browse the repository at this point in the history
Make isort import lazy + Typing issues
  • Loading branch information
Carreau authored Mar 18, 2024
2 parents 65b4795 + a3e399e commit 0bb96b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/python/pyflyby/_imports2s.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# License: MIT http://opensource.org/licenses/MIT
from collections import defaultdict

import isort
from pyflyby._autoimp import scan_for_import_issues
from pyflyby._file import FileText, Filename
from pyflyby._flags import CompilerFlags
Expand All @@ -14,10 +13,12 @@
from pyflyby._parse import PythonBlock
from pyflyby._util import ImportPathCtx, Inf, NullCtx, memoize
import re
from six import exec_


class SourceToSourceTransformationBase(object):

input: PythonBlock

def __new__(cls, arg):
if isinstance(arg, cls):
return arg
Expand Down Expand Up @@ -52,11 +53,15 @@ def output(self, params=None):


class SourceToSourceTransformation(SourceToSourceTransformationBase):

_output: PythonBlock

def preprocess(self):
self.output = self.input
assert isinstance(self.input, PythonBlock), self.input
self._output = self.input

def pretty_print(self, params=None):
return self.output.text
return self._output.text


class SourceToSourceImportBlockTransformation(SourceToSourceTransformationBase):
Expand Down Expand Up @@ -214,7 +219,7 @@ def insert_new_import_block(self):
"""
block = SourceToSourceImportBlockTransformation("")
sepblock = SourceToSourceTransformation("")
sepblock.output = PythonBlock("\n")
sepblock._output = PythonBlock("\n")
self.insert_new_blocks_after_comments([block, sepblock])
self.import_blocks.insert(0, block)
return block
Expand Down Expand Up @@ -433,7 +438,7 @@ def remove_broken_imports(codeblock, params=None):
for imp in list(block.importset.imports):
ns = {}
try:
exec_(imp.pretty_print(), ns)
exec(imp.pretty_print(), ns)
except Exception as e:
logger.info("%s: Could not import %r; removing it: %s: %s",
filename, imp.fullname, type(e).__name__, e)
Expand Down Expand Up @@ -540,6 +545,7 @@ def sort_imports(codeblock):
:param codeblock:
:return: codeblock
"""
import isort
sorted_imports = isort.code(
str(codeblock),
# To sort all the import in lexicographic order
Expand Down Expand Up @@ -644,7 +650,7 @@ def transform_block(block):
output_imports = [ transform_import(imp) for imp in input_imports ]
block.importset = ImportSet(output_imports, ignore_shadowed=True)
else:
block.output = transform_block(block.input)
block._output = transform_block(block.input)
return transformer.output(params=params)


Expand Down
2 changes: 2 additions & 0 deletions lib/python/pyflyby/_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,8 @@ class PythonBlock(object):
"""

text: FileText

def __new__(cls, arg, filename=None, startpos=None, flags=None,
auto_flags=None):
if isinstance(arg, PythonStatement):
Expand Down

0 comments on commit 0bb96b3

Please sign in to comment.