Skip to content

stdlib.3.unitest.mock missing NonCallableMock methods #3842

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

Closed
rodrigondec opened this issue Mar 11, 2020 · 2 comments · Fixed by #3846
Closed

stdlib.3.unitest.mock missing NonCallableMock methods #3842

rodrigondec opened this issue Mar 11, 2020 · 2 comments · Fixed by #3846
Assignees

Comments

@rodrigondec
Copy link
Contributor

I'm using pycharm and found on this stack overflow answer that pycharm uses typeshed for its stub autocompletion.

I'm not having the autocompletions of any Mock or MagickMock methods (such as assert_called , assert_called_once and so on).

Browsing the typeshed code I stumbled upon this line

NonCallableMock = Any

There's a TODO annotation 3 lines above mentioning some false positives

# TODO: Defining this and other mock classes as classes in this stub causes

What does the Any do on NonCallableMock?

Shouldn't we have all methods declared no this NonCallableMock? Or this was the cause of the TODO?

I'm volunteering to open a PR if we should have all mock methods declared

@srittau
Copy link
Collaborator

srittau commented Mar 12, 2020

The probable reason NonCallableMock (and the other mock classes) were defined like this is so that they can be passed in place as any other object without type checkers complaining. I think I remember some discussion about it where it was suggested that the mock classes should derive from Any instead, something like this:

class NonCallableMock(Base, Any):
    def __getattr__(self, name: str) -> Any: ...
    # other methods go here

I quickly tried this, and it seems to work fine with mypy. I think this is worth a go.

@rodrigondec
Copy link
Contributor Author

@srittau Assign this to me plz

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 a pull request may close this issue.

2 participants