Skip to content

--fix remove used imports in from clause #50

@cafewang

Description

@cafewang

This case occurs in main branch.
Test file

import os
import sys
import json  # never used
from typing import (
    List, Set, Tuple
)

def foo():
    print(os.getcwd())
    s: Set[int] = set()
    print(s)

test command

pyward -f ./test_file_path

expected output after --fix

import os
from typing import (
    Set
)

def foo():
    print(os.getcwd())
    s: Set[int] = set()
    print(s)

actual output

import os
from typing import (
)

def foo():
    print(os.getcwd())
    s: Set[int] = set()
    print(s)

The whole line 5 is removed, which is not correct, only List and Tuple should be removed.
Error msg printed as below:

Error analyzing demo\optimization\unused_imports.py: invalid syntax (<unknown>, line 4)

One possible solution: generating from clause other than deleting unused import items could be more reliable and less error-prone.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions