Skip to content
This repository was archived by the owner on May 29, 2026. It is now read-only.
This repository was archived by the owner on May 29, 2026. It is now read-only.

get_unused_import_aliases struggling with imports not at the top  #32

Description

@akov

I get an error when there's an unused import alias but its inside the code:

In [4]: import pasta

In [5]: src = """
   ...: def fun():
   ...:   import a_thing
   ...:   return 1
   ...: 
   ...: fun()
   ...: """

In [6]: tree = pasta.parse(src)

In [7]: from pasta.augment import import_utils

In [8]: import_utils.get_unused_import_aliases(tree)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-12-1eb7af7a6dac> in <module>()
----> 1 import_utils.get_unused_import_aliases(tree)

pasta/augment/import_utils.py in get_unused_import_aliases(tree, sc)
     76     if isinstance(node, ast.alias):
     77       name = sc.names[
---> 78           node.asname if node.asname is not None else node.name]
     79       if not name.reads:
     80         unused_aliases.add(node)

KeyError: 'a_thing'

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions