summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2020-05-16 23:38:45 -0700
committerTimothy Crosley <timothy.crosley@gmail.com>2020-05-16 23:38:45 -0700
commit28bbd0e703e1b6124cb2866082fa9fcfb5b9d921 (patch)
treed0e06e44c8c41a86325e448615db89cab14df8ee
parentcb260e6fc878bb9e39a9a00c8b6eb0956a23739d (diff)
downloadisort-feature/dont-redefine-from-outer-scope.tar.gz
Make sure outer scope variables aren't redefinedfeature/dont-redefine-from-outer-scope
-rw-r--r--isort/parse.py12
-rw-r--r--isort/wrap.py52
-rw-r--r--isort/wrap_modes.py22
3 files changed, 44 insertions, 42 deletions
diff --git a/isort/parse.py b/isort/parse.py
index 2be46212..0266d23f 100644
--- a/isort/parse.py
+++ b/isort/parse.py
@@ -27,10 +27,10 @@ if TYPE_CHECKING:
)
-def _infer_line_separator(file_contents: str) -> str:
- if "\r\n" in file_contents:
+def _infer_line_separator(contents: str) -> str:
+ if "\r\n" in contents:
return "\r\n"
- elif "\r" in file_contents:
+ elif "\r" in contents:
return "\r"
else:
return "\n"
@@ -86,7 +86,7 @@ def skip_line(
(skip_line: bool,
in_quote: str,)
"""
- skip_line = bool(in_quote)
+ should_skip = bool(in_quote)
if '"' in line or "'" in line:
char_index = 0
while char_index < len(line):
@@ -113,9 +113,9 @@ def skip_line(
and not part.startswith("from ")
and not part.startswith(("import ", "cimport "))
):
- skip_line = True
+ should_skip = True
- return (bool(skip_line or in_quote), in_quote)
+ return (bool(should_skip or in_quote), in_quote)
class ParsedContent(NamedTuple):
diff --git a/isort/wrap.py b/isort/wrap.py
index a6dbeee1..2265218a 100644
--- a/isort/wrap.py
+++ b/isort/wrap.py
@@ -20,7 +20,7 @@ def import_statement(
dynamic_indent = " " * (len(import_start) + 1)
indent = config.indent
line_length = config.wrap_length or config.line_length
- import_statement = formatter(
+ statement = formatter(
statement=import_start,
imports=copy.copy(from_imports),
white_space=dynamic_indent,
@@ -33,15 +33,15 @@ def import_statement(
remove_comments=config.ignore_comments,
)
if config.balanced_wrapping:
- lines = import_statement.split(line_separator)
+ lines = statement.split(line_separator)
line_count = len(lines)
if len(lines) > 1:
minimum_length = min(len(line) for line in lines[:-1])
else:
minimum_length = 0
- new_import_statement = import_statement
+ new_import_statement = statement
while len(lines[-1]) < minimum_length and len(lines) == line_count and line_length > 10:
- import_statement = new_import_statement
+ statement = new_import_statement
line_length -= 1
new_import_statement = formatter(
statement=import_start,
@@ -56,19 +56,19 @@ def import_statement(
remove_comments=config.ignore_comments,
)
lines = new_import_statement.split(line_separator)
- if import_statement.count(line_separator) == 0:
- return _wrap_line(import_statement, line_separator, config)
- return import_statement
+ if statement.count(line_separator) == 0:
+ return _wrap_line(statement, line_separator, config)
+ return statement
-def line(line: str, line_separator: str, config: Config = DEFAULT_CONFIG) -> str:
+def line(content: str, line_separator: str, config: Config = DEFAULT_CONFIG) -> str:
"""Returns a line wrapped to the specified line-length, if possible."""
wrap_mode = config.multi_line_output
- if len(line) > config.line_length and wrap_mode != Modes.NOQA: # type: ignore
- line_without_comment = line
+ if len(content) > config.line_length and wrap_mode != Modes.NOQA: # type: ignore
+ line_without_comment = content
comment = None
- if "#" in line:
- line_without_comment, comment = line.split("#", 1)
+ if "#" in content:
+ line_without_comment, comment = content.split("#", 1)
for splitter in ("import ", ".", "as "):
exp = r"\b" + re.escape(splitter) + r"\b"
if re.search(exp, line_without_comment) and not line_without_comment.strip().startswith(
@@ -79,18 +79,20 @@ def line(line: str, line_separator: str, config: Config = DEFAULT_CONFIG) -> str
_comma_maybe = "," if config.include_trailing_comma else ""
line_parts[-1] = f"{line_parts[-1].strip()}{_comma_maybe} #{comment}"
next_line = []
- while (len(line) + 2) > (config.wrap_length or config.line_length) and line_parts:
+ while (len(content) + 2) > (
+ config.wrap_length or config.line_length
+ ) and line_parts:
next_line.append(line_parts.pop())
- line = splitter.join(line_parts)
- if not line:
- line = next_line.pop()
+ content = splitter.join(line_parts)
+ if not content:
+ content = next_line.pop()
cont_line = _wrap_line(
config.indent + splitter.join(next_line).lstrip(), line_separator, config
)
if config.use_parentheses:
if splitter == "as ":
- output = f"{line}{splitter}{cont_line.lstrip()}"
+ output = f"{content}{splitter}{cont_line.lstrip()}"
else:
_comma = "," if config.include_trailing_comma and not comment else ""
if wrap_mode in (
@@ -101,19 +103,19 @@ def line(line: str, line_separator: str, config: Config = DEFAULT_CONFIG) -> str
else:
_separator = ""
output = (
- f"{line}{splitter}({line_separator}{cont_line}{_comma}{_separator})"
+ f"{content}{splitter}({line_separator}{cont_line}{_comma}{_separator})"
)
lines = output.split(line_separator)
if config.comment_prefix in lines[-1] and lines[-1].endswith(")"):
- line, comment = lines[-1].split(config.comment_prefix, 1)
- lines[-1] = line + ")" + config.comment_prefix + comment[:-1]
+ content, comment = lines[-1].split(config.comment_prefix, 1)
+ lines[-1] = content + ")" + config.comment_prefix + comment[:-1]
return line_separator.join(lines)
- return f"{line}{splitter}\\{line_separator}{cont_line}"
- elif len(line) > config.line_length and wrap_mode == Modes.NOQA: # type: ignore
- if "# NOQA" not in line:
- return f"{line}{config.comment_prefix} NOQA"
+ return f"{content}{splitter}\\{line_separator}{cont_line}"
+ elif len(content) > config.line_length and wrap_mode == Modes.NOQA: # type: ignore
+ if "# NOQA" not in content:
+ return f"{content}{config.comment_prefix} NOQA"
- return line
+ return content
_wrap_line = line
diff --git a/isort/wrap_modes.py b/isort/wrap_modes.py
index a42d48a9..b9ba875d 100644
--- a/isort/wrap_modes.py
+++ b/isort/wrap_modes.py
@@ -3,7 +3,7 @@ import enum
from inspect import signature
from typing import Any, Callable, Dict, List
-from . import comments
+import isort.comments
_wrap_modes: Dict[str, Callable[[Any], str]] = {}
@@ -50,7 +50,7 @@ def grid(**interface):
interface["statement"] += "(" + interface["imports"].pop(0)
while interface["imports"]:
next_import = interface["imports"].pop(0)
- next_statement = comments.add_to_line(
+ next_statement = isort.comments.add_to_line(
interface["comments"],
interface["statement"] + ", " + next_import,
removed=interface["remove_comments"],
@@ -69,7 +69,7 @@ def grid(**interface):
lines[-1] = new_line
next_import = interface["line_separator"].join(lines)
interface["statement"] = (
- comments.add_to_line(
+ isort.comments.add_to_line(
interface["comments"],
f"{interface['statement']},",
removed=interface["remove_comments"],
@@ -89,7 +89,7 @@ def vertical(**interface):
return ""
first_import = (
- comments.add_to_line(
+ isort.comments.add_to_line(
interface["comments"],
interface["imports"].pop(0) + ",",
removed=interface["remove_comments"],
@@ -115,7 +115,7 @@ def hanging_indent(**interface):
# Check for first import
if len(next_statement) + 3 > interface["line_length"]:
next_statement = (
- comments.add_to_line(
+ isort.comments.add_to_line(
interface["comments"],
f"{interface['statement']}\\",
removed=interface["remove_comments"],
@@ -127,7 +127,7 @@ def hanging_indent(**interface):
interface["statement"] = next_statement
while interface["imports"]:
next_import = interface["imports"].pop(0)
- next_statement = comments.add_to_line(
+ next_statement = isort.comments.add_to_line(
interface["comments"],
interface["statement"] + ", " + next_import,
removed=interface["remove_comments"],
@@ -138,7 +138,7 @@ def hanging_indent(**interface):
> interface["line_length"]
):
next_statement = (
- comments.add_to_line(
+ isort.comments.add_to_line(
interface["comments"],
f"{interface['statement']}, \\",
removed=interface["remove_comments"],
@@ -153,7 +153,7 @@ def hanging_indent(**interface):
@_wrap_mode
def vertical_hanging_indent(**interface):
- _line_with_comments = comments.add_to_line(
+ _line_with_comments = isort.comments.add_to_line(
interface["comments"],
"",
removed=interface["remove_comments"],
@@ -172,7 +172,7 @@ def vertical_grid_common(need_trailing_char: bool, **interface):
return ""
interface["statement"] += (
- comments.add_to_line(
+ isort.comments.add_to_line(
interface["comments"],
"(",
removed=interface["remove_comments"],
@@ -302,7 +302,7 @@ def vertical_prefix_from_module_import(**interface):
interface["statement"] += interface["imports"].pop(0)
while interface["imports"]:
next_import = interface["imports"].pop(0)
- next_statement = comments.add_to_line(
+ next_statement = isort.comments.add_to_line(
interface["comments"],
interface["statement"] + ", " + next_import,
removed=interface["remove_comments"],
@@ -313,7 +313,7 @@ def vertical_prefix_from_module_import(**interface):
> interface["line_length"]
):
next_statement = (
- comments.add_to_line(
+ isort.comments.add_to_line(
interface["comments"],
f"{interface['statement']}",
removed=interface["remove_comments"],