-
-
Notifications
You must be signed in to change notification settings - Fork 207
Add type hints to config controller functions #2639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
|
@OzGav FYI - this helps your other PR(s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds type hints to config controller functions to eliminate the need for type casts when retrieving configuration values. The changes introduce overloaded function signatures with a return_type parameter for type inference, along with TypeVar-based type hints for better static type checking.
Key Changes
- Added
_ConfigValueTTypeVar bound toConfigValueTypefor type inference in config getter functions - Added overloaded signatures to
get_provider_config_value,get_player_config_value,get_core_config_value, and theget_raw_*methods - Removed explicit type casts throughout the codebase in favor of the new
return_typeparameter - Added comprehensive docstrings explaining the new
return_typeparameter
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| music_assistant/controllers/config.py | Core changes adding TypeVar definition, overload signatures for all config getter methods, and updated docstrings |
| music_assistant/providers/universal_group/player.py | Removed cast and updated to use return_type parameter with positional argument syntax |
| music_assistant/providers/squeezelite/provider.py | Removed cast from get_raw_player_config_value call and replaced cast with int() for explicit type conversion |
| music_assistant/providers/airplay/protocols/raop.py | Removed explicit None default argument and updated to use return_type as keyword argument |
| music_assistant/controllers/music.py | Removed cast and updated to use return_type as keyword argument |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| """ | ||
| Return single configentry value for a provider. | ||
| :param instance_id: The provider instance ID. | ||
| :param key: The config key to retrieve. | ||
| :param return_type: Optional type hint for type inference (e.g., str, int, bool). | ||
| """ |
Copilot
AI
Nov 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The return_type parameter is used purely for type inference and doesn't perform runtime type validation. This means if a caller specifies return_type=str but the actual config value is an int, the type checker will assume it's a str while the runtime value will be an int, potentially causing type safety issues.
Consider adding a note in the documentation to clarify that return_type is for static type checking only and callers are responsible for ensuring the specified type matches the actual config value type. Alternatively, consider adding runtime validation with isinstance() to catch type mismatches early.
| self, | ||
| player_id: str, | ||
| key: str, | ||
| unpack_splitted_values: bool = False, |
Copilot
AI
Nov 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return_type parameter should be keyword-only (using *, before it) to be consistent with the other config getter methods (get_provider_config_value and get_core_config_value), which both use keyword-only parameters for return_type. This ensures API consistency and prevents potential errors from positional argument ordering.
Additionally, usage in this file should be updated to use keyword argument syntax: return_type=str instead of passing str positionally.
| unpack_splitted_values: bool = False, | |
| unpack_splitted_values: bool = False, | |
| *, |
| "str", | ||
| await self.mass.config.get_player_config_value(child_player_id, CONF_HTTP_PROFILE), | ||
| http_profile = await self.mass.config.get_player_config_value( | ||
| child_player_id, CONF_HTTP_PROFILE, False, str |
Copilot
AI
Nov 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return_type parameter should be passed as a keyword argument (return_type=str) instead of a positional argument for consistency with similar usages elsewhere in the codebase (e.g., airplay/protocols/raop.py line 67) and to match the API design of other config getter methods.
| child_player_id, CONF_HTTP_PROFILE, False, str | |
| child_player_id, CONF_HTTP_PROFILE, False, return_type=str |
| @overload | ||
| async def get_player_config_value( | ||
| self, | ||
| player_id: str, | ||
| key: str, | ||
| unpack_splitted_values: bool = ..., | ||
| return_type: type[_ConfigValueT] = ..., | ||
| ) -> _ConfigValueT: ... | ||
|
|
||
| @overload | ||
| async def get_player_config_value( | ||
| self, | ||
| player_id: str, | ||
| key: str, | ||
| unpack_splitted_values: bool = ..., | ||
| return_type: None = ..., | ||
| ) -> ConfigValueType | tuple[str, ...] | list[tuple[str, ...]]: ... |
Copilot
AI
Nov 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The overloads don't correctly handle the interaction between unpack_splitted_values and return_type. When unpack_splitted_values=True, the function always returns tuple[str, ...] | list[tuple[str, ...]] (from line 545) regardless of the return_type parameter. The overloads should be structured as:
- When
unpack_splitted_values=True→ returntuple[str, ...] | list[tuple[str, ...]] - When
unpack_splitted_values=Falseandreturn_type=type[T]→ returnT - When
unpack_splitted_values=Falseandreturn_type=None→ returnConfigValueType
This requires using Literal[True] and Literal[False] in the overloads to properly discriminate the return type based on the unpack_splitted_values parameter.
Add type hints to the config controller functions to get config entry values.
This prevents having to do a lot of type casts everywhere when retrieving config values.