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

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

Closed
dshivashankar1994 opened this issue Mar 12, 2024 · 1 comment
Assignees

Comments

@dshivashankar1994
Copy link
Collaborator

dshivashankar1994 commented Mar 12, 2024

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
> 
@dshivashankar1994 dshivashankar1994 changed the title tidy-imports not happy with class self referencing it via type hints tidy-imports not happy with class self referencing it via type hints (PyInf#12204) Mar 12, 2024
@Carreau Carreau self-assigned this Mar 18, 2024
Carreau added a commit to Carreau/pyflyby that referenced this issue Mar 18, 2024
This adds a failing test.
Carreau added a commit to Carreau/pyflyby that referenced this issue Mar 18, 2024
Carreau added a commit to Carreau/pyflyby that referenced this issue Mar 18, 2024
Carreau added a commit to Carreau/pyflyby that referenced this issue Mar 18, 2024
Carreau added a commit that referenced this issue Mar 19, 2024
This adds failing test, cleanup and fix for #318 (incorrect import insert due to improper stack count)
@Carreau
Copy link
Collaborator

Carreau commented Mar 19, 2024

This should be closed by #320

@Carreau Carreau closed this as completed Mar 19, 2024
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

No branches or pull requests

2 participants