hadialqattan / pycln

A formatter for finding and removing unused import statements.

Home Page:https://hadialqattan.github.io/pycln

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] References in TypeAlias are not recognized which act as a str

GreyElaina opened this issue ยท comments

Describe the bug

pycln cannot recognize these references in TypeAlias, which brings unexpected "unused" import removed.

To Reproduce

  1. Take this code snippet:

    from typing import MutableMapping
    from typing_extensions import TypeAlias
    
    A: TypeAlias = "MutableMapping[str, int]"
    
    def a(x: A) -> int:
        ...
    
    ...# other part of this program
  2. Run this Pycln command:

    $ pycln program.py
  3. Error traceback or unexpected output (if present):

     program.py:1:0 'from typing import MutableMapping' was removed! ๐Ÿ”ฎ
     program.py 1 import was removed! ๐Ÿš€
    
     All done! ๐Ÿ’ช ๐Ÿ˜Ž
     1 import was removed, 1 file was changed.
  4. Unexpected fixed code (if present):

     from typing_extensions import TypeAlias
    
     A: TypeAlias = "MutableMapping[str, int]"  # pyright reports '"MutableMapping" is not defined'

Expected behavior:

  1. Description: pycln won't clear these references in TypeAlias which described as a string.

  2. Expected output (if present):

    Looks good! โœจ ๐Ÿฐ โœจ
    1 file left unchanged.
  3. Expected fixed code (if present):

    from typing import MutableMapping
    from typing_extensions import TypeAlias
    
    A: TypeAlias = "MutableMapping[str, int]"
    
    def a(x: A) -> int:
        ...
    
    ...# other part of this program

Environment (please complete the following informations):

  • Python Version: 3.9.10 in pdm's venv
  • Pycln Version: v2.1.2
  • OS Type: Windows 10

Additional context

None

Hi @GreyElaina, I've just shipped a PR(#190) that has the fix.

Thanks for reporting the bug.