Skip to content

Instantly share code, notes, and snippets.

@ichux
Created November 5, 2025 06:04
Show Gist options
  • Select an option

  • Save ichux/e1ff8fa2a6f2cb30d174476383a6b460 to your computer and use it in GitHub Desktop.

Select an option

Save ichux/e1ff8fa2a6f2cb30d174476383a6b460 to your computer and use it in GitHub Desktop.
python3 << 'EOF'
import ast
import sys
file_path = "~/test_clinical_study.py"
with open(file_path, 'r') as f:
tree = ast.parse(f.read(), filename=file_path)
imported = set()
used = set()
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
name = alias.asname if alias.asname else alias.name
imported.add(name.split('.')[0])
elif isinstance(node, ast.ImportFrom):
for alias in node.names:
name = alias.asname if alias.asname else alias.name
imported.add(name)
elif isinstance(node, ast.Name):
used.add(node.id)
elif isinstance(node, ast.Attribute):
if isinstance(node.value, ast.Name):
used.add(node.value.id)
unused = imported - used
if unused:
print("Unused imports found:", sorted(unused))
else:
print("No unused imports found")
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment