deshaw / pyflyby

A set of productivity tools for Python
https://deshaw.github.io/pyflyby/
Other
347 stars 52 forks source link

tidy-imports not happy with class self referencing it via type hints (PyInf#12204) #318

Closed dshivashankar1994 closed 5 months ago

dshivashankar1994 commented 6 months ago

If a class is added in pyflyby global imports and in the class defn itself if the class is referenced via type-hints, tidy-imports wrongly suggests to add the import.

Reproducer:

> cat /var/tmp/test.py 

from __future__ import annotations

class _TestCls():
    class WrappedFunction():
        pass

class Popen():
    def test(self) -> Popen:
        return None

> tidy-imports /var/tmp/test.py
[PYFLYBY] /var/tmp/test.py: added 'from subprocess import Popen'
--- /var/tmp/test.py    2024-03-12 11:39:26.907800081 -0400
+++ /tmp/tmpdmys0zsi    2024-03-12 11:39:32.834774372 -0400
@@ -1,4 +1,5 @@
 from __future__ import annotations
+from   subprocess               import Popen

 class _TestCls():
     class WrappedFunction():

Replace /var/tmp/test.py? [y/N] 

But the same type hints doesn't suggest for imports if the extra class is not present but the type-hints exist.

> cat /var/tmp/test.py 

from __future__ import annotations

class Popen():
    def test(self) -> Popen:
        return None

> tidy-imports /var/tmp/test.py
> 
Carreau commented 5 months ago

This should be closed by #320