-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
Add MockOf typing helper #120752
Add MockOf typing helper #120752
Conversation
Hey there @chishm, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
Hey there @Galorhallen, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
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.
I don't think we should add that. As you point out yourself
It is completely pointless from a type-checking perspective (ie. mypy/pylance)
That also means it isn't validated at any point. We'd have to know to update the type annotation if the patch location or the mock object changes.
Yes, better typing for mocks is a valid issue but that's probably better solved at the Python typing level instead. I'd encourage you to look for / create a new discussion about it over at https://discuss.python.org/c/typing/32
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
Adding some context: |
Interestingly, """Test MockOf for mypy."""
from typing import Generic, TypeVar
from unittest.mock import Mock
_T = TypeVar("_T")
class MockOf(Mock, Generic[_T]):
"""Add ability to set underlying type for Mock."""
class ApiClient:
def get_devices(self) -> list[str]:
return []
class MockOfApiClient(Mock, ApiClient):
"""Mock of ApiClient."""
mock_client = Mock(spec=ApiClient)
reveal_type(mock_client)
# Revealed type is "unittest.mock.Mock"
reveal_type(mock_client.get_devices)
# Revealed type is "Any"
mock_typed_client: MockOfApiClient = mock_client
reveal_type(mock_typed_client)
# Revealed type is "test_mypy.MockOfApiClient"
reveal_type(mock_typed_client.get_devices)
# Revealed type is "def () -> builtins.list[builtins.str]"
mock_generic_typed_client: MockOf[ApiClient] = mock_client
reveal_type(mock_generic_typed_client)
# Revealed type is test_mypy.MockOf[test_mypy.ApiClient] "
reveal_type(mock_generic_typed_client.get_devices)
# Revealed type is "Any" |
There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. |
Proposed change
Add some typing helpers for
Mock
, when used in conjuction with a class constructor.It is completely pointless from a type-checking perspective (ie. mypy/pylance), but I think it makes the code more readable:
Type of change
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: