Skip to content
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

unittest.mock.Mock is not treated as Any in strict mode #6136

Closed
MartinAltmayerTMH opened this issue Oct 12, 2023 · 3 comments
Closed

unittest.mock.Mock is not treated as Any in strict mode #6136

MartinAltmayerTMH opened this issue Oct 12, 2023 · 3 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@MartinAltmayerTMH
Copy link

As far as I know, Mock objects should be treated as Any by type checkers (see e.g. #3611). However, the following code raises type warnings in strict mode.

from unittest.mock import Mock

def test_something():
  item = Mock()
  item.start.assert_not_called()

The errors are

my_test.py
  my_test.py:6:3 - error: Type of "start" is unknown (reportUnknownMemberType)
  my_test.py:6:3 - error: Type of "assert_not_called" is unknown (reportUnknownMemberType)

And if this should be the expected behavior, what is the correct way to work with mock functions like assert_not_called?

Tested with pyright 1.1.331 on the command line.

@MartinAltmayerTMH MartinAltmayerTMH added the bug Something isn't working label Oct 12, 2023
erictraut pushed a commit that referenced this issue Oct 12, 2023
…s that derives from `Any`. Previously, these were evaluated as `Unknown`, but they are now evaluated as `Any`. This is related to #6136.
erictraut added a commit that referenced this issue Oct 12, 2023
…s that derives from `Any`. Previously, these were evaluated as `Unknown`, but they are now evaluated as `Any`. This is related to #6136. (#6137)

Co-authored-by: Eric Traut <erictr@microsoft.com>
@erictraut
Copy link
Collaborator

The type declaration for Mock indicates that it derives from NonCallableMock which derives from Any.

Currently, when accessing an attribute from a class that derives from Any or , pyright evaluates the type of that attribute as Unknown. This is the intended behavior, but now that you mention it, this behavior seems inconsistent. If you access an attribute from an object that is explicitly declared as Any, pyright evaluates the result as Any. It should arguably do the same if the object is an instance of a class that derives from Any.

c1: Any = 1
reveal_type(c1.foo) # Any

class DerivesFromAny(Any): ...

c2 = DerivesFromAny()
reveal_type(c2.foo) # Unknown

I think it makes sense to change this behavior for consistency. This will be addressed in the next release.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Oct 12, 2023
@erictraut
Copy link
Collaborator

This is included in pyright 1.1.332, which I just published. It will also be included in a future release of pylance.

@MartinAltmayerTMH
Copy link
Author

Thanks for the quick resolution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants