|
| 1 | +# ARG --exception-suppress-context-managers=mysuppress,*.dangerousname,dangerouslibrary.* |
| 2 | +# ARG --enable=ASYNC910,ASYNC911 |
| 3 | + |
| 4 | +# AUTOFIX |
| 5 | +# ASYNCIO_NO_AUTOFIX |
| 6 | + |
| 7 | +# 912 is tested in eval_files/async912.py to avoid problems with autofix/asyncio |
| 8 | + |
| 9 | +import contextlib |
| 10 | +from typing import Any |
| 11 | + |
| 12 | +import trio |
| 13 | + |
| 14 | +mysuppress: Any |
| 15 | +anything: Any |
| 16 | +dangerouslibrary: Any |
| 17 | + |
| 18 | + |
| 19 | +async def foo() -> Any: |
| 20 | + await foo() |
| 21 | + |
| 22 | + |
| 23 | +async def foo_suppress(): # ASYNC910: 0, "exit", Statement('function definition', lineno) |
| 24 | + with contextlib.suppress(): |
| 25 | + await foo() |
| 26 | + await trio.lowlevel.checkpoint() |
| 27 | + |
| 28 | + |
| 29 | +async def foo_suppress_1(): # ASYNC910: 0, "exit", Statement('function definition', lineno) |
| 30 | + with mysuppress(): |
| 31 | + await foo() |
| 32 | + await trio.lowlevel.checkpoint() |
| 33 | + |
| 34 | + |
| 35 | +async def foo_suppress_2(): # ASYNC910: 0, "exit", Statement('function definition', lineno) |
| 36 | + with anything.dangerousname(): |
| 37 | + await foo() |
| 38 | + await trio.lowlevel.checkpoint() |
| 39 | + |
| 40 | + |
| 41 | +async def foo_suppress_3(): # ASYNC910: 0, "exit", Statement('function definition', lineno) |
| 42 | + with dangerouslibrary.anything(): |
| 43 | + await foo() |
| 44 | + await trio.lowlevel.checkpoint() |
| 45 | + |
| 46 | + |
| 47 | +async def foo_suppress_async911(): # ASYNC911: 0, "exit", Statement("function definition", lineno) |
| 48 | + with contextlib.suppress(): |
| 49 | + await foo() |
| 50 | + yield |
| 51 | + await foo() |
| 52 | + await trio.lowlevel.checkpoint() |
| 53 | + |
| 54 | + |
| 55 | +# the `async with` checkpoints, so there's no error |
| 56 | +async def foo_suppress_async(): |
| 57 | + async with mysuppress: |
| 58 | + await foo() |
| 59 | + |
| 60 | + |
| 61 | +async def foo_multiple(): # ASYNC910: 0, "exit", Statement('function definition', lineno) |
| 62 | + with anything, contextlib.suppress(): |
| 63 | + await foo() |
| 64 | + await trio.lowlevel.checkpoint() |
| 65 | + |
| 66 | + |
| 67 | +# we only match on *calls* |
| 68 | +async def foo_no_call(): |
| 69 | + with contextlib.suppress: # type: ignore[attr-defined] |
| 70 | + await foo() |
| 71 | + |
| 72 | + |
| 73 | +# doesn't work on namedexpr, but those should use `as` anyway |
| 74 | +async def foo_namedexpr(): |
| 75 | + with (ref := contextlib.suppress()): |
| 76 | + await foo() |
| 77 | + |
| 78 | + |
| 79 | +async def foo_suppress_as(): # ASYNC910: 0, "exit", Statement('function definition', lineno) |
| 80 | + with contextlib.suppress() as my_suppressor: |
| 81 | + await foo() |
| 82 | + await trio.lowlevel.checkpoint() |
| 83 | + |
| 84 | + |
| 85 | +# ############################### |
| 86 | +# from contextlib import suppress |
| 87 | +# ############################### |
| 88 | + |
| 89 | + |
| 90 | +# not enabled unless it's imported from contextlib |
| 91 | +async def foo_suppress_directly_imported_1(): |
| 92 | + with suppress(): |
| 93 | + await foo() |
| 94 | + |
| 95 | + |
| 96 | +from contextlib import suppress |
| 97 | + |
| 98 | + |
| 99 | +# now it's enabled |
| 100 | +async def foo_suppress_directly_imported_2(): # ASYNC910: 0, "exit", Statement('function definition', lineno) |
| 101 | + with suppress(): |
| 102 | + await foo() |
| 103 | + await trio.lowlevel.checkpoint() |
| 104 | + |
| 105 | + |
| 106 | +# it also supports importing with an alias |
| 107 | +from contextlib import suppress as adifferentname |
| 108 | + |
| 109 | + |
| 110 | +async def foo_suppress_directly_imported_3(): # ASYNC910: 0, "exit", Statement('function definition', lineno) |
| 111 | + with adifferentname(): |
| 112 | + await foo() |
| 113 | + await trio.lowlevel.checkpoint() |
| 114 | + |
| 115 | + |
| 116 | +# and will keep track of all identifiers it's been assigned as |
| 117 | +async def foo_suppress_directly_imported_4(): # ASYNC910: 0, "exit", Statement('function definition', lineno) |
| 118 | + with suppress(): |
| 119 | + await foo() |
| 120 | + await trio.lowlevel.checkpoint() |
| 121 | + |
| 122 | + |
| 123 | +# basic function scoping is supported |
| 124 | +async def function_that_contains_the_import(): |
| 125 | + from contextlib import suppress as bar |
| 126 | + |
| 127 | + with adifferentname(): |
| 128 | + await foo() |
| 129 | + await trio.lowlevel.checkpoint() |
| 130 | + yield # ASYNC911: 4, "yield", Stmt("function definition", lineno-5) |
| 131 | + with bar(): |
| 132 | + await foo() |
| 133 | + await trio.lowlevel.checkpoint() |
| 134 | + yield # ASYNC911: 4, "yield", Stmt("yield", lineno-3) |
| 135 | + await foo() |
| 136 | + |
| 137 | + |
| 138 | +# bar is not suppressing |
| 139 | +async def foo_suppress_directly_imported_scoped(): |
| 140 | + with bar(): # type: ignore[name-defined] |
| 141 | + await foo() |
| 142 | + |
| 143 | + |
| 144 | +# adifferentname is still suppressing |
| 145 | +async def foo_suppress_directly_imported_restored_after_scope(): # ASYNC910: 0, "exit", Statement('function definition', lineno) |
| 146 | + with adifferentname(): |
| 147 | + await foo() |
| 148 | + await trio.lowlevel.checkpoint() |
| 149 | + |
| 150 | + |
| 151 | +# We don't track the identifier being overridden though. |
| 152 | +adifferentname = None # type: ignore[assignment] |
| 153 | + |
| 154 | + |
| 155 | +# shouldn't give an error |
| 156 | +async def foo_suppress_directly_imported_5(): # ASYNC910: 0, "exit", Statement('function definition', lineno) |
| 157 | + with adifferentname(): |
| 158 | + await foo() |
| 159 | + await trio.lowlevel.checkpoint() |
| 160 | + |
| 161 | + |
| 162 | +# or assignments to different identifiers |
| 163 | +from contextlib import suppress |
| 164 | + |
| 165 | +my_second_suppress = suppress |
| 166 | + |
| 167 | + |
| 168 | +# should give an error |
| 169 | +async def foo_suppress_directly_imported_assignment(): |
| 170 | + with my_second_suppress(): |
| 171 | + await foo() |
0 commit comments