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

Move imports on lines with other, resting, imports #57

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lmmx
Copy link
Owner

@lmmx lmmx commented Mar 24, 2023

Purpose

To support moving imports from lines which they are currently on.

Use case

Consider this file

$ cat bar.py 
from pathlib import Path

from .foo import FOO, read

p = Path()

def bar(path):
    return read(path)

if FOO:
    bar(path=p)

If you do this now you get

$ mvdef -d -m=bar bar.py dst.py
Traceback (most recent call last):
  File "/home/louis/miniconda3/bin/mvdef", line 8, in <module>
    sys.exit(cli_move())
  File "/home/louis/miniconda3/lib/python3.10/site-packages/mvdef/cli.py", line 57, in cli_move
    return cli(MvCls=MvDef, *args, **kwargs)
  File "/home/louis/miniconda3/lib/python3.10/site-packages/mvdef/cli.py", line 43, in cli
    diffs = mover.diffs(print_out=True)
  File "/home/louis/miniconda3/lib/python3.10/site-packages/mvdef/transfer/move.py", line 98, in diffs
    src_unidiff = self.src_diff.unidiff()
  File "/home/louis/miniconda3/lib/python3.10/site-packages/mvdef/core/diff/diff.py", line 38, in unidiff
    return self.agenda.unidiff(target_file=self.target_file, is_src=self.is_src)
  File "/home/louis/miniconda3/lib/python3.10/site-packages/mvdef/core/agenda.py", line 267, in unidiff
    new = self.simulate(input_text=old)
  File "/home/louis/miniconda3/lib/python3.10/site-packages/mvdef/core/agenda.py", line 479, in simulate
    return self.resimulate(
  File "/home/louis/miniconda3/lib/python3.10/site-packages/mvdef/core/agenda.py", line 450, in resimulate
    filtered = self.apply(
  File "/home/louis/miniconda3/lib/python3.10/site-packages/mvdef/core/agenda.py", line 340, in apply
    raise NotImplementedError("Imports staying and going on same line")
NotImplementedError: Imports staying and going on same line

Here is the beginning of the apply method:

    def apply(
        self,
        input_text: str,
        *,
        imports_in: list[ArrivingImport],
        imports_out: list[DepartingImport],
        recheck: Checker | None = None,
    ) -> str:
        if imports_out:
            resting_imports = [
                imp
                for departure in imports_out
                for imp in self.original_ref.imports
                if imp.source.lineno == departure.lineno
                if imp is not departure.bound
            ]
            if resting_imports:
                raise NotImplementedError("Imports staying and going on same line")

@lmmx lmmx linked an issue Mar 24, 2023 that may be closed by this pull request
@lmmx
Copy link
Owner Author

lmmx commented Mar 28, 2023

Where I left off:

(mvdef) louis 🚶 ~/dev/testing/mvdef/tests/surplus_import $ mvdef -d -m=bar bar.py dst.py
> /home/louis/dev/mvdef/src/mvdef/core/agenda.py(341)apply()
-> raise NotImplementedError("Imports staying and going on same line")
(Pdb) p input_text
'from pathlib import Path\n\nfrom .foo import FOO, read\n\np = Path()\n\n\ndef bar(path):\n    return read(path)\n\n\nif FOO:\n    bar(path=p)\n'
(Pdb) p imports_in
[]
(Pdb) p imports_out
[DepartingImport(bound=<ImportationFrom object 'read' from line 3 at 0x7f5cc4537070>, lineno=3, end_lineno=3)]
(Pdb) p recheck
None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"NotImplementedError: Imports staying and going on same line"
1 participant