Skip to content

Conversation

@Angel98518
Copy link

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.

Copy link
Contributor

@thewhaleking thewhaleking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PRs should be opened against staging.

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.
@Angel98518 Angel98518 force-pushed the fix/specific-exception-handling branch from 4a5b46f to 0777571 Compare December 19, 2025 15:33
@Angel98518 Angel98518 changed the base branch from main to staging December 19, 2025 15:35
else:
norm_value = value
except Exception:
except (KeyError, ValueError, TypeError, AttributeError):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under which situations could an AttributeError be raised?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AttributeError guards against cases where value passed to the normalization functions is None or an object lacking expected attributes (e.g., calling .rao or .tao on a malformed Balance-like object), or when norm_value unexpectedly lacks the to_dict() method. This is defensive handling since subnet hyperparameters come from chain data that may have unexpected types.

try:
keypair = Keypair.create_from_uri(uri)
except Exception as e:
except (ValueError, TypeError) as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this can only raise a TypeError or a KeyFileError, but I could be wrong. Where did you get the ValueError here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on py-substrate-interface source, Keypair.create_from_uri() raises ValueError for invalid URI formats or derivation paths (e.g., malformed suri strings). However, if you've traced through and confirmed it only raises TypeError in practice for btcli's usage, I'm happy to remove ValueError from the exception tuple. Let me know your preference

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you checking py-substrate-interface? We don't use that library at all.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake - I referenced the wrong library. btcli uses bittensor_wallet.Keypair, not py-substrate-interface. If you've confirmed it only raises TypeError, I'll update both lines to except TypeError as e:.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed ValueError from both lines.

try:
keypair = Keypair.create_from_uri(uri)
except Exception as e:
except (ValueError, TypeError) as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question about ValueError here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reasoning as line 380 - both are Keypair.create_from_uri() calls. If you'd prefer I remove ValueError from both and keep only TypeError, I can update both together. Just let me know!

@Angel98518
Copy link
Author

@thewhaleking Please review my answer when you are available to check

Per reviewer feedback: bittensor_wallet.Keypair.create_from_uri() only raises
TypeError, not ValueError. Removed ValueError from exception tuples on lines
380 and 431.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants