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

Fix recursive type validation #172

Merged
merged 2 commits into from
Apr 20, 2020
Merged

Conversation

fornellas
Copy link
Contributor

@fornellas fornellas commented Apr 20, 2020

typeguard recursively digs into types when possible (eg: Union, Tuple). This PR patches and wraps typeguard.check_type to make it aware of mocks and play nicely with them, as it is expected for TestSlide.

This is a stepping stone only, I'll cut a PR for upstream typeguard, so we can remove the ugly patch from here eventually.

I just shuffled tests to make them flatter, without touching any logic. The tests added here are these:

  recursion and mocks
    typing.Union
      passes with StritMock without template
      it works with unittest.mock.Mock without spec
      passes with StritMock with valid template
      passes with unittest.mock.Mock with valid spec
      fails with StritMock with invalid template
      fails with unittest.mock.Mock with invalid spec
    typing.Tuple
      passes with StritMock without template
      it works with unittest.mock.Mock without spec
      passes with StritMock with valid template
      passes with unittest.mock.Mock with valid spec
      fails with StritMock with invalid template
      fails with unittest.mock.Mock with invalid spec

Many thanks to @fabriziocucci & @david-caro to doing all the heavy duty ground work before this fix. It would not have happened without your support!

@fornellas fornellas added the bug Something isn't working label Apr 20, 2020
@fornellas fornellas added this to the Type Checking milestone Apr 20, 2020
@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Apr 20, 2020
)

with unittest.mock.patch.object(typeguard, "check_type", new=wrapped_check_type):
typeguard.check_type(name, value, expected_type)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just note that any other module that did an import of typeguard, will see the patched version for the little time that this with runs, so it might lead to very weird errors if running in parallel, maybe we should monkey-patch at the beginning only, as un-patching might also interfere, wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but not sure how to fix. I put the patch with the smallest scope possible (might even take performance away...) precisely because of that... Not sure which's worse. Hopefully typeguard will get this improved upstream, and we'll get rid of this soon.
Feel free to cut a PR to change this if you feel passionate, I'll gladly merge it.

@fornellas fornellas merged commit 0b36ffd into master Apr 20, 2020
@fornellas fornellas deleted the fix_recursive_type_validation branch April 20, 2020 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working CLA Signed Do not delete this pull request or issue due to inactivity.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants