alecthomas / alecthomas/importmagic

Python3 type hint annotations are not considered

Offen
#46 4 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Python
Sterne
122
Forks
21
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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:
```python
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>}
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.