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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for anyio.get_cancelled_exc_class() in TRIO102, 103 and 104. #127

Merged
merged 2 commits into from
Feb 7, 2023

Conversation

jakkdl
Copy link
Member

@jakkdl jakkdl commented Feb 6, 2023

Fixes #122

rewrote critical_except a bit as I updated it, in large part because I personally failed to read the old version of the code 馃槆
Fix #106 for TRIO102 (#109 only fixed TRIO103 & TRIO104)
Thanks to #125 it was quite smooth to add a different suggestion for TRIO103 when anyio is the primary library.
As I started to write a assert_correct_message test helper I realized I had pretty bad code duplication, so checking of column, message and args are now all handled in assert_correct_attribute.
Improved how ignore_column is handled in anyio tests.
A slew of tests being moved around and modified and stuff so the necessary eval_files are handled with trio/anyio as appropriate. And some additional ones for #106+TRIO102

@jakkdl
Copy link
Member Author

jakkdl commented Feb 6, 2023

Added a separate error message when both are available.

But I'm unsure how to handle TRIO103 when both are imported, should bare excepts require that both have been handled in that case?
See the new trio103_both_imported.py which tests for such cases.

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 looks good to me!

Some non-blocking nitpicks below; I intend to try it out on some anyio libraries to check that the results looks reasonable, and then merge if there's no obvious problems.

Given that LibCST support seems close-ish, if it's noisy I'll wait for auto-fixes before recommending to OSS maintainers.

Comment on lines 24 to 27
_suggestion_dict[
"[trio|anyio]"
] = f'[{_suggestion_dict["trio"]}|{_suggestion_dict["anyio"]}]'
_suggestion_dict["[anyio|trio]"] = _suggestion_dict["[trio|anyio]"]
Copy link
Member

Choose a reason for hiding this comment

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

Let's canonicalize this, so we only have to worry about one ordering.

I'd also recommend a tuple literal rather than the (not-valid-at-runtime) |.

Copy link
Member Author

Choose a reason for hiding this comment

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

I should maybe replace self.library with a tuple instead of a string, but there's semi-intentionally an ordering. Running your project with --anyio and importing trio for a file, vs importing trio in a global scope and anyio in a local scope. (or more reasonable, if it did matter in other contexts and there was a flag for it, running the project with a --trio flag and importing anyio in a file), could plausibly lead to different behaviour / error messages. But writing this out it seems so esoteric/far-fetched/does-not-matter-with-current-implementation I'll disregard that for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

I fiddled a bit with this and ... maybe the code is better now? Idk, I'll likely fiddle around with this more when I write more library-specific/dependent code

Comment on lines 170 to 173
except BaseException:
await foo() # error: 8, Statement("BaseException", lineno-1)
except:
await foo() # safe, since BaseException will catch Cancelled
Copy link
Member

Choose a reason for hiding this comment

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

...safe, yes, but also redundant with the except BaseException: 馃

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't actually know that exceptions have to inherit from BaseException, interesting. I opened PyCQA/flake8-bugbear#349 in case anybody cares for it.

@Zac-HD
Copy link
Member

Zac-HD commented Feb 7, 2023

I'm unsure how to handle TRIO103 when both are imported, should bare excepts require that both have been handled in that case?

I think in this case it's safe to just catch anyio.get_cancelled_exc_class(), because if you're actually running Trio code that will return trio.Cancelled.

@jakkdl
Copy link
Member Author

jakkdl commented Feb 7, 2023

I'm unsure how to handle TRIO103 when both are imported, should bare excepts require that both have been handled in that case?

I think in this case it's safe to just catch anyio.get_cancelled_exc_class(), because if you're actually running Trio code that will return trio.Cancelled.

If you're running all three async libraries, and anyio's current backend is asyncio, and in the try: statement stuff is ran that could either raise trio.Cancelled or anyio.get_cancelled_exc_class(), it might bork? But that seems weird enough that I think not raising false alarms is more important.

@jakkdl
Copy link
Member Author

jakkdl commented Feb 7, 2023

addressed nitpicks and rebased on top of main to ensure coverage.

@Zac-HD Zac-HD merged commit 9fbd54f into python-trio:main Feb 7, 2023
@jakkdl jakkdl deleted the trio102_anyio branch February 8, 2023 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants