Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make isort import lazy + Typing issues #317

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading