From 1b1a8503234ad9c00e009e5d8b89606d7065d2ab Mon Sep 17 00:00:00 2001
From: Daniel Hollas <daniel.hollas@bristol.ac.uk>
Date: Fri, 10 Jan 2025 00:09:12 +0100
Subject: [PATCH] Fix verdi devel check-undesired-imports when tui extra is
 installed

---
 .github/workflows/ci-code.yml           |  2 --
 src/aiida/cmdline/commands/cmd_devel.py | 12 ++++++++----
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/.github/workflows/ci-code.yml b/.github/workflows/ci-code.yml
index 2dff454067..dc26a30f51 100644
--- a/.github/workflows/ci-code.yml
+++ b/.github/workflows/ci-code.yml
@@ -132,8 +132,6 @@ jobs:
       with:
         python-version: '3.12'
         from-lock: 'true'
-        # NOTE: The `verdi devel check-undesired-imports` fails if
-        # the 'tui' extra is installed.
         extras: ''
 
     - name: Run verdi tests
diff --git a/src/aiida/cmdline/commands/cmd_devel.py b/src/aiida/cmdline/commands/cmd_devel.py
index f8c89d14c1..b311e5706e 100644
--- a/src/aiida/cmdline/commands/cmd_devel.py
+++ b/src/aiida/cmdline/commands/cmd_devel.py
@@ -61,12 +61,11 @@ def devel_check_load_time():
 def devel_check_undesired_imports():
     """Check that verdi does not import python modules it shouldn't.
 
-    Note: The blacklist was taken from the list of packages in the 'atomic_tools' extra but can be extended.
+    This is to keep the verdi CLI snappy, especially for tab-completion.
     """
     loaded_modules = 0
 
-    for modulename in [
-        'asyncio',
+    unwanted_modules = [
         'requests',
         'plumpy',
         'disk_objectstore',
@@ -78,7 +77,12 @@ def devel_check_undesired_imports():
         'spglib',
         'pymysql',
         'yaml',
-    ]:
+    ]
+    # trogon powers the optional TUI and uses asyncio.
+    # Check for asyncio only when the optional tui extras are not installed.
+    if 'trogon' not in sys.modules:
+        unwanted_modules += 'asyncio'
+    for modulename in unwanted_modules:
         if modulename in sys.modules:
             echo.echo_warning(f'Detected loaded module "{modulename}"')
             loaded_modules += 1