diff --git a/jedi/api/refactoring.py b/jedi/api/refactoring.py index de79a995..4ac0b2aa 100644 --- a/jedi/api/refactoring.py +++ b/jedi/api/refactoring.py @@ -134,15 +134,15 @@ def inline(grammar, names): if not names: raise RefactoringError("There is no name under the cursor") if any(n.api_type == 'module' for n in names): - raise RefactoringError("Cannot inline imports or modules.") + raise RefactoringError("Cannot inline imports or modules") if any(n.tree_name is None for n in names): - raise RefactoringError("Cannot inline builtins.") + raise RefactoringError("Cannot inline builtins/extensions") definitions = [n for n in names if n.tree_name.is_definition()] if len(definitions) == 0: - raise RefactoringError("No definition found to inline.") + raise RefactoringError("No definition found to inline") if len(definitions) > 1: - raise RefactoringError("Cannot inline a name with multiple definitions.") + raise RefactoringError("Cannot inline a name with multiple definitions") tree_name = definitions[0].tree_name @@ -156,6 +156,19 @@ def inline(grammar, names): if len(expr_stmt.get_defined_names(include_setitem=True)) > 1: raise RefactoringError("Cannot inline a statement with multiple definitions") + first_child = expr_stmt.children[1] + if first_child.type == 'annassign' and len(first_child.children) == 4: + first_child = first_child.children[2] + if first_child != '=': + if first_child.type == 'annassign': + raise RefactoringError( + 'Cannot inline a statement that is defined by an annotation' + ) + else: + raise RefactoringError( + 'Cannot inline a statement with "%s"' + % first_child.get_code(include_prefix=False) + ) rhs = expr_stmt.get_rhs() replace_code = rhs.get_code(include_prefix=False) diff --git a/test/refactor/inline.py b/test/refactor/inline.py index 31344656..18d43894 100644 --- a/test/refactor/inline.py +++ b/test/refactor/inline.py @@ -1,10 +1,70 @@ -# -------------------------------------------------- multi-equal +# -------------------------------------------------- no-name-error +#? 0 error +1 +# ++++++++++++++++++++++++++++++++++++++++++++++++++ +There is no name under the cursor +# -------------------------------------------------- multi-equal-error def test(): #? 4 error a = b = 3 return test(100, a) # ++++++++++++++++++++++++++++++++++++++++++++++++++ Cannot inline a statement with multiple definitions +# -------------------------------------------------- no-definition-error +#? 5 error +test(a) +# ++++++++++++++++++++++++++++++++++++++++++++++++++ +No definition found to inline +# -------------------------------------------------- multi-names-error +#? 0 error +a, b[1] = 3 +test(a) +# ++++++++++++++++++++++++++++++++++++++++++++++++++ +Cannot inline a statement with multiple definitions +# -------------------------------------------------- addition-error +#? 0 error +a = 2 +a += 3 +test(a) +# ++++++++++++++++++++++++++++++++++++++++++++++++++ +Cannot inline a name with multiple definitions +# -------------------------------------------------- only-addition-error +#? 0 error +a += 3 +test(a) +# ++++++++++++++++++++++++++++++++++++++++++++++++++ +Cannot inline a statement with "+=" +# -------------------------------------------------- with-annotation +foobarb: int = 1 +#? 5 +test(foobarb) +# ++++++++++++++++++++++++++++++++++++++++++++++++++ +--- /home/dave/source/jedi/test/refactor/inline.py ++++ /home/dave/source/jedi/test/refactor/inline.py +@@ -1,4 +1,4 @@ +-foobarb: int = 1 ++ + #? 5 +-test(foobarb) ++test(1) +# -------------------------------------------------- only-annotation-error +a: int +#? 5 error +test(a) +# ++++++++++++++++++++++++++++++++++++++++++++++++++ +Cannot inline a statement that is defined by an annotation +# -------------------------------------------------- builtin +import math +#? 7 error +math.cos +# ++++++++++++++++++++++++++++++++++++++++++++++++++ +Cannot inline builtins/extensions +# -------------------------------------------------- module +from import_tree import some_mod +#? 11 error +test(some_mod) +# ++++++++++++++++++++++++++++++++++++++++++++++++++ +Cannot inline imports or modules # -------------------------------------------------- simple def test(): #? 4