Skip to content

ignore type for mock.pyi inheritance from Any #1492

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

Merged
merged 3 commits into from
Jul 19, 2017

Conversation

toejough
Copy link
Contributor

@toejough toejough commented Jul 18, 2017

Problem:
Any time I run mypy --strict --ignore-missing-imports, I get an error like <site-packages>/lib/mypy/typeshed/stdlib/3/unittest/mock.pyi:34: error: Class cannot subclass 'Any' (has type 'Any')

Analysis:
#1446 complains about the inheritance from Any, among other things, but what's relevant to this PR is where the use of # type: ignore is followed everywhere else this inheritance happens.

I'm not sure if it's the "right" solution, but adding # type: ignore to mock.pyi's use of Any resolves the error message I keep seeing. It also passes all of the tests requested.

Resolution:
I added the same # type: ignore comment to the inheritance from Any in mock.pyi that is used in other places that that inheritance is done.

Documentation:
No docs modified/added for this change.

Testing:
Ran the tests locally on osx 10.11.6, and they all passed.

Impact:
Patch - ignoring an illegal type inheritance that appears to work but is flagged by mypy (and is ignored in other areas of typeshed code).

toejough added 2 commits July 17, 2017 22:55
**Problem**:
Any time I run `mypy`, I get an error like `<site-packages>/lib/mypy/typeshed/stdlib/3/unittest/mock.pyi:34: error: Class cannot subclass 'Any' (has type 'Any')`

**Analysis**:
python#1446 complains about the inheritance from `Any`, among other things, but what's relevant to this PR is where the use of `# type: ignore` is followed everywhere else this inheritance happens.

I'm not sure if it's the "right" solution, but adding `# type: ignore` to `mock.pyi`'s use of `Any` resolves the error message I keep seeing.  It also passes all of the [tests requested](https://github.com/python/typeshed/blob/master/README.md#running-the-tests).

**Resolution**:
I added the same `# type: ignore` comment to the inheritance from `Any` in `mock.pyi` that is used in other places that that inheritance is done.

**Documentation**:
No docs modified/added for this change.

**Testing**:
Ran the [tests](https://github.com/python/typeshed/blob/master/README.md#running-the-tests) locally on osx 10.11.6, and they all passed.

**Impact**:
Patch - ignoring an illegal type inheritance that appears to work but is flagged by mypy (and is ignored in other areas of typeshed code).
Since the change was so simple, I made it here and locally, instead of making a local branch and pushing it up.  My local branch that I ran the tests on had the right spacing, and the online change didn't :-/ 

It does now.
@JelleZijlstra
Copy link
Member

See also #1446 (cc @ethanhs).

@emmatyping
Copy link
Member

I'd prefer to remove these to be more correct. Also putting an ignore makes it hard for mypy to pick them up to be removed eventually. People look to typeshed as a good example, and inheriting from Any should be discouraged.

@smessmer made the original addition of the Any. It seems you were suspicious of the change when it was proposed. Im not familiar enough with unittest.mock to know what the best decision is here, but if no alternative is found, I don't think this solution is terrible.

I believe the type for it must be Any in some way, as it can return any type based on its usage, but again Im not familiar with it enough to say for sure.

@gvanrossum
Copy link
Member

The OP, in his comprehensive post, does not mention what flags he passes to mypy, but I suspect he uses --strict. This implies --disallow-subclassing-any which explains the error. I am not aware of any workarounds other than a # type: ignore comment. If you're worried that such a comment's significance will be lost in the future, add a # TODO comment on the line before, e.g.

# TODO: Get rid of the # type: ignore below.
# It is currently required to shut up mypy when run with `--strict`
# or `--disallow-subclassing-any`. The `Any` base class is currently
# the only way to allow passing an instance of `Mock` to functions
# expecting other classes (as is Mock's purpose)

@toejough
Copy link
Contributor Author

toejough commented Jul 19, 2017

Ooh, sorry, yes, I'm running mypy --strict --ignore-missing-imports. (I also edited the original post to include this for clarity).

I realize this "fix" is a little quick-and-dirty, but until a more correct solution is agreed upon, is there any downside to including it?

With respect to people looking to typeshed for examples, the use of Any is present with or without the PR. Could this be merged and a new issue opened to discuss/track the correct resolution?

I would like to be able to run mypy --strict on my own code without encountering failures due to typeshed code.

Ooh. Is there somewhere I could look for guidance on the right way to write a test for ensuring mypy --strict doesn't error on typeshed code? Is that kind of test something you guys would want?

@emmatyping
Copy link
Member

@toejough considering a better solution is not evident right now this fix is fine, but please add a TODO comment please. Also the issue I opened tracks all subclassing from Any. As for making typeshed mypy --strict safe, we are still working on that, and will likely need to ignore some errors.

@toejough
Copy link
Contributor Author

cool. added.

@gvanrossum gvanrossum merged commit f6b013b into python:master Jul 19, 2017
@gvanrossum
Copy link
Member

Thanks!

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 this pull request may close these issues.

4 participants