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

Trio103: except Cancelled/BaseException must be re-raised #8

Merged
merged 17 commits into from
Jul 30, 2022

Conversation

jakkdl
Copy link
Member

@jakkdl jakkdl commented Jul 27, 2022

First draft, think I like the approach - although not totally sure how nested try's should be handled. And I haven't looked up how raise .. from should be handled.

This should probably have two different error messages and numbers, one for "reached end of block without re-raising" and another for "raising different error or returning".

…eException block with a code path that doesn't re-raise the error> - couple stuff WIP/TODO
flake8_trio.py Outdated Show resolved Hide resolved
Copy link
Member Author

@jakkdl jakkdl left a comment

Choose a reason for hiding this comment

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

(testing review/threading features for commenting on stuff)

@jakkdl jakkdl marked this pull request as draft July 27, 2022 17:08
tests/trio103.py Outdated Show resolved Hide resolved
tests/trio103.py Outdated Show resolved Hide resolved
tests/trio103.py Outdated Show resolved Hide resolved
@Zac-HD
Copy link
Member

Zac-HD commented Jul 27, 2022

Approach looks good overall!

@jakkdl jakkdl changed the title Trio103, except Cancelled/BaseException must be re-raised Trio103: except Cancelled/BaseException must be re-raised Jul 28, 2022
…t re-raising by way of creating a new BaseException/Cancelled, handle nested try's
@jakkdl jakkdl marked this pull request as ready for review July 28, 2022 08:39
@jakkdl
Copy link
Member Author

jakkdl commented Jul 28, 2022

I think it's fully finished now, so review away!

I got almost all the way to a fully thorough parsing of nested except's - but I stopped when I realized I needed to handle this case:

except BaseException as e:
  try:
    raise
  except ValueError:
    raise # error, but parent except is fully covered if replaced with `raise e`
  except:
    raise e

But I can probably fix it if you want. Would require the beautiful if test in visit_Raise to get even bigger though 😁

flake8_trio.py Outdated Show resolved Hide resolved
flake8_trio.py Outdated Show resolved Hide resolved
flake8_trio.py Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
flake8_trio.py Outdated Show resolved Hide resolved
tests/trio103_104.py Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@Zac-HD Zac-HD mentioned this pull request Jul 28, 2022
12 tasks
@Zac-HD
Copy link
Member

Zac-HD commented Jul 29, 2022

OK, I've merged the others and cherry-picked a few parts of this too; I'll let you merge or rebase as you prefer.

@jakkdl
Copy link
Member Author

jakkdl commented Jul 29, 2022

Fixed all comments. Was sloppy and did that in the same commit as the merge, so hope that doesn't mess up your review habits too much.
I might wanna implement #13 before we merge this, the test file is a mess.

…d comments, new exceptions are not accepted, bugs and rewrites and stuff.
@jakkdl
Copy link
Member Author

jakkdl commented Jul 29, 2022

Merged in #14 and was glad I didn't have any mismatches at first, but then I got to reorganizing and restructuring the tests and ooh boi that was some work. Also came up with and added test cases for avoiding re-raising by abusing continue/break that I'll fix later. EDIT: fixed

Comment on lines 87 to 95
# don't bypass raise by raising from nested except
except trio.Cancelled as e:
try:
pass
except ValueError as g:
raise g # error
except BaseException as h:
raise h # error ?
raise e
Copy link
Member Author

Choose a reason for hiding this comment

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

Need help thinking on what is going on here. Is it always wrong to do except <critical> while inside a except <critical>?

Copy link
Member

Choose a reason for hiding this comment

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

No, nesting like that can make sense depending on the contents of the inner try: block.

Copy link
Member Author

Choose a reason for hiding this comment

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

so: nested raise are not okay, unless the nested except is a handler for Cancelled/BaseException?

But hmm, this will bypass the Cancelled:

try:
  ...
except trio.Cancelled:
  try:
    print(1/0)
  except:
    raise

Copy link
Member

Choose a reason for hiding this comment

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

I propose we just ignore this for now - get the simple cases done in this PR, and open an issue for any follow-up which we can do (much) later.

Copy link
Member Author

Choose a reason for hiding this comment

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

sure - would just like an executive decision on whether to be silent about nested critical excepts vs put up a warning and expect users to #noqa. Could add a third message "TRIO107 Warning: nested BaseException/trio.Cancelled" (or increment current 105&106 so they're adjacent - maybe good to not reserve error codes for WIP features in the future).
Putting that in would just be a few lines in visit_ExceptHandler so no big complexity hit.

If you just don't care / prefer status quo, you can just merge it as is - the PR should be done otherwise.

tests/trio103_104.py Outdated Show resolved Hide resolved
Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

This is probably going to complain about a lot of existing code... let's ship it! Hands-on experience is usually the best way to work out if and how we might want to change a check 😁

@Zac-HD Zac-HD merged commit cdd89ca into python-trio:main Jul 30, 2022
@jakkdl
Copy link
Member Author

jakkdl commented Jul 30, 2022

hehe, good luck! 😁

@jakkdl jakkdl deleted the trio103 branch December 9, 2022 11:58
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.

2 participants