importmagic icon indicating copy to clipboard operation
importmagic copied to clipboard

Python3 type hint annotations are not considered

Open djanderson opened this issue 8 years ago • 4 comments

Hi, thanks for the great project.

Currently it seems that importmagic doesn't look for __annotations__ properties added in recent version of python 3, so python3 syntax-aware checkers complain if I use a type hint without importing the name, but importmagic will delete any import that's only referenced in a type annotation.

Test case:

import importmagic


code = """
from typing import Iterable

def print_it(it: Iterable):
    for i in it:
        print(i)

print_it(['a', 'b', 'c'])
"""

scope = importmagic.Scope.from_source(code)
unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
print("unreferenced: {}".format(unreferenced))

from typing import Iterable

def print_it(it: Iterable):
    for i in it:
        print(i)

print("function annotation: {}".format(print_it.__annotations__))

Output:

$ python3 importmagictest.py 
unreferenced: {'Iterable'}
function annotation: {'it': typing.Iterable<+T_co>}

djanderson avatar May 22 '17 14:05 djanderson

Well spotted, and great test case, thanks for the bug report. I'll try to get to that this weekend.

alecthomas avatar May 26 '17 22:05 alecthomas

(unrelated, but both of our avatars are photographs of Autumn leaves)

alecthomas avatar May 26 '17 22:05 alecthomas

Any updates with this? Running into the same issue.

leos avatar Jul 09 '17 12:07 leos

It looks like #49 fixed this?

glyph avatar May 09 '23 04:05 glyph