Skip to content

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

Closed

Description

@rodrigondec

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

Activity

  1. srittau commented on Mar 12, 2020

    @srittau
    Collaborator

    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.

  2. rodrigondec commented on Mar 12, 2020

    @rodrigondec
    ContributorAuthor

    @srittau Assign this to me plz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions