Skip to content

Always infer in operator as returning bool #5688

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 4 commits into from
Feb 1, 2019

Conversation

TV4Fun
Copy link
Contributor

@TV4Fun TV4Fun commented Sep 28, 2018

Resolves #5656

@TV4Fun
Copy link
Contributor Author

TV4Fun commented Sep 28, 2018

Tangentially related: Is there a way to have the test suite do type checks on Mypy's own code?

@@ -1686,6 +1686,8 @@ def visit_ellipsis(self, e: EllipsisExpr) -> Type:

def visit_op_expr(self, e: OpExpr) -> Type:
"""Type check a binary operator expression."""
if e.op == 'in':
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean that we won't check type errors within the subexpressions of the in expressions?

Concretely, does mypy still report an error on code like 1 in ([1] + ['x'])?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The example you gave still produces an error, but just to be on the safe side, I've added some extra type checking, and a test to make sure this is still an error.

@JelleZijlstra
Copy link
Member

We run a self-check (mypy typechecking itself) as part of the test suite.

@TV4Fun
Copy link
Contributor Author

TV4Fun commented Sep 29, 2018

@JelleZijlstra, the reason I asked about type checking Mypy, is that before I submitted this PR, I ran the full test suite with pytest, but there was still a type error that was only caught by the CI build after I submitted.

@gvanrossum
Copy link
Member

before I submitted this PR, I ran the full test suite with pytest, but there was still a type error that was only caught by the CI build after I submitted.

That's because CI does a bit more than just run pytest. Check out the contents of .travis.yml and appveyor.yml to see what.

You can type-check mypy with itself using something like python3 -m mypy --config-file mypy_self_check.ini mypy.

@TV4Fun
Copy link
Contributor Author

TV4Fun commented Sep 30, 2018

Thanks for that information @gvanrossum. I might suggest adding that to the documentation for tests to run before submitting a PR.

@gvanrossum
Copy link
Member

gvanrossum commented Sep 30, 2018 via email

@gvanrossum
Copy link
Member

I forgot one thing. (It's also not in any README file.) There's a script runtests.py that runs all the tests, including the self tests. I was reminded of this by #5702.

gvanrossum pushed a commit that referenced this pull request Oct 1, 2018
@jonapich
Copy link

jonapich commented Oct 2, 2018

Am I seeing the same behavior with == operator?

    def __eq__(self, other: Any) -> bool:
        return other == self.value

warning: Returning Any from function declared to return "bool"

@ilevkivskyi
Copy link
Member

@jonapich
No, you don't:

>>> class C:
...     def __eq__(self, other):
...         return 'surprise'
...     def __contains__(self, other):
...         return 'no way'
... 
>>> 1 in C()
True
>>> 1 == C()
'surprise'

@jonapich
Copy link

jonapich commented Oct 2, 2018

🙊

🏃‍♂️

TV4Fun added a commit to TV4Fun/mypy that referenced this pull request Oct 4, 2018
Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

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

This seems solid to me. If you rebase and fix merge conflicts I think we can merge it.

@msullivan
Copy link
Collaborator

I went and rebased this and will merge it now

@msullivan msullivan merged commit 83775ed into python:master Feb 1, 2019
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.

6 participants