KEMBAR78
Better handling of compile-time relational operators by refi64 · Pull Request #3158 · python/mypy · GitHub
Skip to content

Conversation

refi64
Copy link
Contributor

@refi64 refi64 commented Apr 12, 2017

Inspired by (but doesn't fix [yet!]) #3156. (A "fix" would to be to add an early return to check_boolean_op if e.right_unreachable is True, but I'm not sure if that change would have other repercussions that I'm not aware of.)

Now stuff like this works better:

if PY2 and sys.platform == 'linux':
    # ...
else:
    # ...

a = PY2 and 's'  # Type is str under Python 2, bool under Python 3.

b = PY3 and 's'
c = PY2 or 's'
d = PY3 or 's'
reveal_type(a) # E: Revealed type is 'builtins.bool'
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be Union[bool, str]? Same for all of these, really.

Copy link
Member

Choose a reason for hiding this comment

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

Oh wait this is testing the special-casing for PY2 and PY3 as well. I guess it's okay. I may not get to the full review until next week.

@gvanrossum
Copy link
Member

@ilevkivskyi Could you review this?
@kirbyfan64 Can you fix the merge conflict?

@gvanrossum gvanrossum requested a review from ilevkivskyi May 10, 2017 22:21
@ilevkivskyi
Copy link
Member

OK, will review this tomorrow.

@refi64
Copy link
Contributor Author

refi64 commented May 11, 2017

Sorry for the delays; I kinda got hung up on finals...

@gvanrossum
Copy link
Member

NP, hope finals went/are going/will go well!

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Mostly looks good to me. Just have few ideas for additional tests.

PY2 = f()
PY3 = f()

a = PY2 and 's'
Copy link
Member

Choose a reason for hiding this comment

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

I would add few more tests exchanging the order of 's' and PY2/PY3. So that e.g. 's' and False == False.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ilevkivskyi That doesn't work, though, since infer_condition_value doesn't take constants into account...

y = 'x' / 1 # E: Unsupported left operand type for / ("str")
y


Copy link
Member

Choose a reason for hiding this comment

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

This empty line is unnecessary.

x = 'foo'
else:
x = 3
reveal_type(x) # E: Revealed type is 'builtins.str'
Copy link
Member

Choose a reason for hiding this comment

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

I would also add few more tests here and for and, so that more combinations of True and False are tested.

@ilevkivskyi
Copy link
Member

@kirbyfan64 Are you going to continue working on this PR (here are just few tests missing and a merge conflict needs to be fixed)?

@refi64
Copy link
Contributor Author

refi64 commented Jun 3, 2017

My motherboard died. Somehow, I feel like this has been one of the craziest college years I've ever had...

@refi64 refi64 force-pushed the relop branch 2 times, most recently from 19e8556 to 3c78e82 Compare June 9, 2017 18:50
@refi64
Copy link
Contributor Author

refi64 commented Jun 9, 2017

@ilevkivskyi Done!

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Thank! LGTM now.

@ilevkivskyi ilevkivskyi merged commit de80e76 into python:master Jun 9, 2017
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.

3 participants