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

Now typecheckes traceback in raise e, msg, traceback on py2 #11289

Merged
merged 6 commits into from Oct 10, 2021
Merged

Now typecheckes traceback in raise e, msg, traceback on py2 #11289

merged 6 commits into from Oct 10, 2021

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Oct 7, 2021

No description provided.

@sobolevn sobolevn marked this pull request as draft October 7, 2021 10:28
@sobolevn
Copy link
Member Author

sobolevn commented Oct 7, 2021

I found several other corner-cases, which I want to cover later today.

@sobolevn
Copy link
Member Author

sobolevn commented Oct 7, 2021

Corner cases:

  1. Old styled classes are allowed to be used with raise in python2:
class Old:
    pass

raise Old

# Traceback (most recent call last):
#  File "Main.py", line 4, in <module>
#    raise Old
# __main__.Old: <__main__.Old instance at 0x14ada04a35f0>

I don't think that we should ever support this. First of all, mypy does not have any old-styled / new-styled distinction.
Secondly, it will require a lot of work to add this.

  1. raise can be used with tuple like this:
e = (Exception, 2, 3, 4, 5, 6, 7, 8)  # or `Exception('a')`
raise e

# Traceback (most recent call last):
#  File "Main.py", line 8, in <module>
#    raise e
# Exception

This is something we can clearly support. I will add this to this PR.

  1. This, on the other side is not allowed:
e = Exception('a')  # Or `(Exception('a'),)`
raise e, 'b'
# TypeError: instance exception may not have a separate value

I will add a check for that as well.

  1. Case with callable exception type:
class My(Exception):
    def __init__(self, start, finish):
        pass

raise My, 1  # should raise

Currently, we don't support this. But in runtime if fails:

Traceback (most recent call last):
  File "Main.py", line 8, in <module>
    raise My, 1
TypeError: __init__() takes exactly 3 arguments (2 given)

We need to check this similarly to Python3.

@JelleZijlstra
Copy link
Member

This seems fine, but honestly it doesn't feel that valuable to fix arcane aspects of Python 2 raise syntax. Python 2 support is a low priority already and we may drop support at some point.

If this solves problems in your codebase, though, I'm happy to review and merge it.

@sobolevn
Copy link
Member Author

sobolevn commented Oct 9, 2021

If this solves problems in your codebase, though, I'm happy to review and merge it.

Not production code, fortunately 🙂
I was teaching how to use python2 (for historic reasons) and type annotations there. And got hit by this.

and we may drop support at some point

Sure thing! Right now it would be even easier: because all python2 related code for this feature now lives in a separate protected function with _python2 postfix. We can just remove it later and done! 👏

@JelleZijlstra it's now ready to be reviewed.

@sobolevn sobolevn marked this pull request as ready for review October 9, 2021 07:07
Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Thanks! I have two small comments.

mypy/checker.py Outdated Show resolved Hide resolved

# Plain 'raise' is a valid statement.
expr: Optional[Expression]
from_expr: Optional[Expression]
# Is set when python2 has `raise exc, msg, traceback`.
legacy_mode: bool

def __init__(self, expr: Optional[Expression], from_expr: Optional[Expression]) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Why not make it a constructor argument?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because we only use it in fastparse2 and nowhere else. It would be easier to remove one day.

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@JelleZijlstra JelleZijlstra merged commit c22beb4 into python:master Oct 10, 2021
@sobolevn
Copy link
Member Author

sobolevn commented Oct 10, 2021

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.

None yet

2 participants