-
Notifications
You must be signed in to change notification settings - Fork 116
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
Support for type checking any mock with spec #121
Comments
I've recently merged a PR for bypassing type checks for |
That's in a good direction, but still does not cover these 2 cases:
What do you think? |
For the first item, I would be inclined to accept a PR for that. For the second item, I would consider a proposal for such an API. |
Cool! I'll do a PR for the first item as soon as I have time. For the second, here's the proposal:
|
This feature is trickier to implement than I thought at first because it requires a class compatibility check instead of the instance/value checks that typeguard currently uses. |
+1 Would like to see this happen. Mocks of third party package classes are being flagged by Typeguard as being incorrect types because of the way I'm setting the Mocks' edit-- Typeguard docs say they ignore Mocks, but that doesn't seem to be the case: https://typeguard.readthedocs.io/en/latest/userguide.html?highlight=mock#support-for-mock-objects edit^2 -- Actually it looks like I've unfortunately hit some kind of strange edge case scenario (explained below) where a mock is not used by Typeguard. |
Can you create a minimal script to demonstrate the problem? |
Unfortunately for me it seems to be a really weird edge case involving type expectations, because my attempts to reproduce it in a minimal way aren't working. I imagine the underlying reason could be because of the type expectations being more permissive when it's imported in a certain way or when the underlying classes are less complex.
The idea is that we want to mock the minimal behavior for a unit test without needing to instantiate the entire class itself, which could potentially take up a lot of memory for integration/acceptance tests. The underlying third party package in question here is I imagine there must be something strange about its implementation that doesn't allow me to mock it naively for Typeguard (though Pytest and Mypy have no issue with it), and it somehow falls through the cracks with Typeguard. In reality, what we get from Typeguard is something like this:
What I'd expect from Typeguard is to recognize that I'm passing in a Mock object to the constructor of |
The error you pasted here indicates that mocks are not used at all in the function call. Please paste the relevant target function header and the exact type of the object being passed. |
I only have an xfailing test for this in the code base, but I don't think it's going to start working any time soon. |
While working on TestSlide (a Python test framework from Facebook), @david-caro, @fabriziocucci and myself found an issue with typeguard integration.
In summary, typeguard checks for types recursively (eg: for
Union
,Tuple
etc), which is a good thing. However, we got failures when one of these recursive checks hits a mock object.TestSlide type validation functionality plays nicely with mocks, and can detect and extract their templates for use in type validation. But, if a mock has no template, no type validation happens (just like typeguard).
TestSlide's interface for this is public and extensible, so we can add supports for any mock object, including TestSlide's own StrictMock.
I cut this clowny PR for TestSlide, to add the extra logic to deal with mocks, by patching
typeguard.check_type
and wrapping it, so we can move on temporarily.@agronholm , I'm willing to cut a PR for typeguard to implement this public interface for dealing with mocks, what do you think? Once available at typeguard, we can drop it from TestSlide.
The text was updated successfully, but these errors were encountered: