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

[BUG] Imports removed from class scopes #204

Open
joaoe opened this issue Jun 7, 2023 · 1 comment
Open

[BUG] Imports removed from class scopes #204

joaoe opened this issue Jun 7, 2023 · 1 comment
Labels
bug Something isn't working
Projects

Comments

@joaoe
Copy link

joaoe commented Jun 7, 2023

Describe the bug A clear and concise description of what the bug is.

Imports are incorrectly removed from class scopes.
Sometimes I use class scopes to keep imports contained when implementing multiple versions of the same interface

class TaskAdapter:
    def run(self, fn, *a, **kw):
        raise NotImplementedError

class ThreadAdapter(TaskAdapter):
    import threading

    def run(self, fn, *a, **kw):
        result = []
        fn = functools.partial(fn, *a, **kw)
        t = self.threading.Thread(target=lambda: result.append(fn()))
        t.start()
        t.join()
        return result[0]

class MultiProcessingAdapter(TaskAdapter):
    import multiprocessing
    
    def run(self, fn, *a, **kw):
        p = self.multiprocessing.Process()
        ....

class AsyncIOAdapter(TaskAdapter):
    import asyncio
    
    def do_something(self, fn, *a, **kw):
        loop = self.asyncio.new_event_loop()
        ....

pycln incorrectly removes these. Note that I also tried with autoflake and it works correctly and does not remove these imports.

To Reproduce Steps to reproduce the behavior:

  1. Take this code snippet:

     class MyClass:
         import threading
         def run(self):
             assert self.threading
    
     assert MyClass.threading
     assert MyClass().threading
  2. Run this Pycln command:

     $ pycln snippet.py
     a.py:2:4 'import threading' was removed! �
     a.py 1 import was removed! �
    
     All done! � �
     1 import was removed, 1 file was changed.
  3. Error traceback or unexpected output (if present):

     Traceback (most recent call last):
       File "snippet.py", line 6, in <module>
         assert MyClass.threading
     AttributeError: type object 'MyClass' has no attribute 'threading'
  4. Unexpected fixed code (if present):

     class MyClass:
     
         def run(self):
             assert self.threading

Expected behavior:

  1. Description: A clear and concise description of what you expected to happen.

NOTHING. The code snippet should not be changed.

  1. Expected fixed code (if present): SAME AS INPUT

    class MyClass:
        import threading
        def run(self):
            assert self.threading
    
    assert MyClass.threading
    assert MyClass().threading

Environment (please complete the following informations):

  • Python Version: 3.9
  • Pycln Version: 2.1.5
  • OS Type: windows

Additional context Add any other context about the problem here.

@joaoe joaoe added the bug Something isn't working label Jun 7, 2023
@hadialqattan hadialqattan added this to To do in Bug Fixing via automation Jun 7, 2023
@hadialqattan
Copy link
Owner

hadialqattan commented Jun 7, 2023

@joaoe I see that problem.. amma fix that problem asap.. Happy to see you around

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Bug Fixing
  
To do
Development

No branches or pull requests

2 participants