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

Type checker fails with mocks #130

Closed
fornellas opened this issue Mar 25, 2020 · 2 comments · Fixed by #132
Closed

Type checker fails with mocks #130

fornellas opened this issue Mar 25, 2020 · 2 comments · Fixed by #132
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed
Milestone

Comments

@fornellas
Copy link
Contributor

This PR added type checking for function calls. It works as expected, but it is failing when a call is made using a mock, but it should not.

Suggestion on how to fix this:

  • Have a constant with a list of known mock classes: Python's unittest (eg: the ones listed here) and StrictMock.
  • Have a map of mock class to a function that extracts its template:
    • For StrictMock, get the given template (IIRC mock_callable already does this, there's a trick there).
    • For Python's unittest, get the spec.

Using that:

  • If the given argument is a mock:
    • Without a template / spec, then do no type checking.
    • With a template, do type checking using the template.
@fornellas fornellas added bug Something isn't working help wanted Extra attention is needed labels Mar 25, 2020
david-caro added a commit to david-caro/TestSlide that referenced this issue Mar 30, 2020
david-caro added a commit to david-caro/TestSlide that referenced this issue Mar 30, 2020
We were not propely checking the signature validation when the
parameters passed were mocks and failing with TypeError.

This tries to extract the spec or template of the parameter to do the
type checking allowing to pass mocks to the functions without failing
the signature type validation.

NOTE: this assumes that all unittest.mock mocks inherit from
NonCallableMock, that assumption might change with time, though
currently seems to be the case:
```
$ grep '^class .*Mock(' /usr/lib64/python3.7/unittest/mock.py
class NonCallableMock(Base):
class Mock(CallableMixin, NonCallableMock):
class NonCallableMagicMock(MagicMixin, NonCallableMock):
class MagicMock(MagicMixin, Mock):
class PropertyMock(Mock):
```

Fixes facebook#130
david-caro added a commit to david-caro/TestSlide that referenced this issue Mar 31, 2020
We were not propely checking the signature validation when the
parameters passed were mocks and failing with TypeError.

This tries to extract the spec or template of the parameter to do the
type checking allowing to pass mocks to the functions without failing
the signature type validation.

NOTE: this assumes that all unittest.mock mocks inherit from
NonCallableMock, that assumption might change with time, though
currently seems to be the case:
```
$ grep '^class .*Mock(' /usr/lib64/python3.7/unittest/mock.py
class NonCallableMock(Base):
class Mock(CallableMixin, NonCallableMock):
class NonCallableMagicMock(MagicMixin, NonCallableMock):
class MagicMock(MagicMixin, Mock):
class PropertyMock(Mock):
```

Fixes facebook#130
@david-caro
Copy link
Contributor

I sent a PR, not 100% sure if that's what you meant that was the problem xd

@david-caro david-caro self-assigned this Mar 31, 2020
@fornellas
Copy link
Contributor Author

PR is legit! This was exactly the problem, thanks!

@fornellas fornellas added this to the Type Checking milestone Apr 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants