From 0777571b3cfc5fe503c351af9c7c5790f03d04b8 Mon Sep 17 00:00:00 2001 From: daniel Date: Fri, 19 Dec 2025 08:37:17 +0100 Subject: [PATCH 1/2] fix: replace broad exception catches with specific exception types Replace overly broad `except Exception` catches with specific exception types to improve error handling clarity and debugging: - subtensor_interface.py: Handle both TimeoutError and asyncio.TimeoutError for substrate initialization timeout (resolves TODO comment) - utils.py (is_valid_github_url): Catch ValueError, TypeError, AttributeError for URL parsing exceptions (resolves TODO comment) - utils.py (normalize_hyperparameters): Catch KeyError, ValueError, TypeError, AttributeError for parameter normalization - wallets.py (new_hotkey): Catch ValueError, TypeError for Keypair.create_from_uri - wallets.py (new_coldkey): Catch ValueError, TypeError for Keypair.create_from_uri - wallets.py (wallet_create): Catch ValueError, TypeError, KeyFileError for keypair and wallet creation This change improves code quality by making exception handling more explicit and easier to debug while maintaining the same error recovery behavior. --- bittensor_cli/src/bittensor/subtensor_interface.py | 2 +- bittensor_cli/src/bittensor/utils.py | 4 ++-- bittensor_cli/src/commands/wallets.py | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bittensor_cli/src/bittensor/subtensor_interface.py b/bittensor_cli/src/bittensor/subtensor_interface.py index abdcfa961..7338b70b0 100644 --- a/bittensor_cli/src/bittensor/subtensor_interface.py +++ b/bittensor_cli/src/bittensor/subtensor_interface.py @@ -135,7 +135,7 @@ async def __aenter__(self): try: await self.substrate.initialize() return self - except TimeoutError: # TODO verify + except (TimeoutError, asyncio.TimeoutError): err_console.print( "\n[red]Error[/red]: Timeout occurred connecting to substrate. " f"Verify your chain and network settings: {self}" diff --git a/bittensor_cli/src/bittensor/utils.py b/bittensor_cli/src/bittensor/utils.py index c15b8f287..7f50ed5dc 100644 --- a/bittensor_cli/src/bittensor/utils.py +++ b/bittensor_cli/src/bittensor/utils.py @@ -850,7 +850,7 @@ def normalize_hyperparameters( norm_value = norm_value.to_dict() else: norm_value = value - except Exception: + except (KeyError, ValueError, TypeError, AttributeError): # bittensor.logging.warning(f"Error normalizing parameter '{param}': {e}") norm_value = "-" if not json_output: @@ -1728,7 +1728,7 @@ def is_valid_github_url(url: str) -> bool: return False return True - except Exception: # TODO figure out the exceptions that can be raised in here + except (ValueError, TypeError, AttributeError): return False diff --git a/bittensor_cli/src/commands/wallets.py b/bittensor_cli/src/commands/wallets.py index fd205237a..645799b87 100644 --- a/bittensor_cli/src/commands/wallets.py +++ b/bittensor_cli/src/commands/wallets.py @@ -377,7 +377,7 @@ async def new_hotkey( if uri: try: keypair = Keypair.create_from_uri(uri) - except Exception as e: + except (ValueError, TypeError) as e: print_error(f"Failed to create keypair from URI {uri}: {str(e)}") return wallet.set_hotkey(keypair=keypair, encrypt=use_password) @@ -428,7 +428,7 @@ async def new_coldkey( if uri: try: keypair = Keypair.create_from_uri(uri) - except Exception as e: + except (ValueError, TypeError) as e: print_error(f"Failed to create keypair from URI {uri}: {str(e)}") wallet.set_coldkey(keypair=keypair, encrypt=False, overwrite=False) wallet.set_coldkeypub(keypair=keypair, encrypt=False, overwrite=False) @@ -501,7 +501,7 @@ async def wallet_create( "hotkey_ss58": wallet.hotkeypub.ss58_address, "coldkey_ss58": wallet.coldkeypub.ss58_address, } - except Exception as e: + except (ValueError, TypeError, KeyFileError) as e: err = f"Failed to create keypair from URI: {str(e)}" print_error(err) output_dict["error"] = err From bb4956d9beb058b04e9a4a89418fdfc1ac421655 Mon Sep 17 00:00:00 2001 From: Angel98518 Date: Tue, 23 Dec 2025 16:44:33 +0100 Subject: [PATCH 2/2] fix: remove ValueError from Keypair.create_from_uri exception handling Per reviewer feedback: bittensor_wallet.Keypair.create_from_uri() only raises TypeError, not ValueError. Removed ValueError from exception tuples on lines 380 and 431. --- bittensor_cli/src/commands/wallets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bittensor_cli/src/commands/wallets.py b/bittensor_cli/src/commands/wallets.py index b614ae94b..50b66a5a6 100644 --- a/bittensor_cli/src/commands/wallets.py +++ b/bittensor_cli/src/commands/wallets.py @@ -377,7 +377,7 @@ async def new_hotkey( if uri: try: keypair = Keypair.create_from_uri(uri) - except (ValueError, TypeError) as e: + except TypeError as e: print_error(f"Failed to create keypair from URI {uri}: {str(e)}") return wallet.set_hotkey(keypair=keypair, encrypt=use_password) @@ -428,7 +428,7 @@ async def new_coldkey( if uri: try: keypair = Keypair.create_from_uri(uri) - except (ValueError, TypeError) as e: + except TypeError as e: print_error(f"Failed to create keypair from URI {uri}: {str(e)}") wallet.set_coldkey(keypair=keypair, encrypt=False, overwrite=False) wallet.set_coldkeypub(keypair=keypair, encrypt=False, overwrite=False)