-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[flake8-async
] Implement blocking-http-call-httpx
(ASYNC212
)
#20091
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
Conversation
Adds new rule to find and report use of `httpx.Client` in synchronous functions.
Still needs some work (and learning) to catch usage of |
|
|
||
async def foo(): | ||
client: httpx.Client = ... | ||
client.request() # ASYNC212 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one still isn't caught, presumably because the TypeChecker bits only capture annotations at the function level?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should work. I played around a bit with the PTH210 rule, which uses the PathlibPathChecker
, and it will detect both module-level and function-level bindings like this:
def foo():
path: Path = ...
path.with_suffix(".")
I'm wondering if the union traversal might be disrupting this check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, thank you! I just had a couple of small suggestions and some ideas about the union handling that might help with the simple annotation case.
crates/ruff_linter/src/rules/flake8_async/rules/blocking_http_call_httpx.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_async/rules/blocking_http_call_httpx.rs
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_async/rules/blocking_http_call_httpx.rs
Outdated
Show resolved
Hide resolved
|
||
async def foo(): | ||
client: httpx.Client = ... | ||
client.request() # ASYNC212 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should work. I played around a bit with the PTH210 rule, which uses the PathlibPathChecker
, and it will detect both module-level and function-level bindings like this:
def foo():
path: Path = ...
path.with_suffix(".")
I'm wondering if the union traversal might be disrupting this check.
49 | async def foo(client: httpx.Client | None): | ||
50 | client.request() # ASYNC212 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay, this doesn't work for PTH210. I understand the traverse_union
call now. I'd probably lean toward not traversing unions and matching our other type inference calls, but I don't feel too strongly if you think this is an important use case. We could also consider making a more general change to our TypeChecker
code if this would always be helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was doing this mostly to match behavior with the upstream ASYNC212 implementation, which explicitly includes these unions/optionals in its test cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense! If we can handle both this and the regular annotation case, it's great to handle both. We can punt on trying to update any other TypeChecker
s for now too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks!
Would you mind updating the title to:
[flake8-async
] Implement blocking-http-call-httpx
(ASYNC212
)
(just wrapping the linter name and rule code in backticks)
We pull the PR titles for changelog entries, and that's how we usually stylize them.
Other than that, feel free to merge whenever you're ready!
blocking-http-call-httpx
(ASYNC212)flake8-async
] Implement blocking-http-call-httpx
(ASYNC212
)
* main: Fix mdtest ignore python code blocks (#20139) [ty] add support for cyclic legacy generic protocols (#20125) [ty] add cycle detection for find_legacy_typevars (#20124) Use new diff rendering format in tests (#20101) [ty] Fix 'too many cycle iterations' for unions of literals (#20137) [ty] No boundness analysis for implicit instance attributes (#20128) Bump 0.12.11 (#20136) [ty] Benchmarks for problematic implicit instance attributes cases (#20133) [`pyflakes`] Fix `allowed-unused-imports` matching for top-level modules (`F401`) (#20115) Move GitLab output rendering to `ruff_db` (#20117) [ty] Evaluate reachability of non-definitely-bound to Ambiguous (#19579) [ty] Introduce a representation for the top/bottom materialization of an invariant generic (#20076) [`flake8-async`] Implement `blocking-http-call-httpx` (`ASYNC212`) (#20091) [ty] print diagnostics with fully qualified name to disambiguate some cases (#19850) [`ruff`] Preserve relative whitespace in multi-line expressions (`RUF033`) (#19647)
…stral-sh#20091) ## Summary Adds new rule to find and report use of `httpx.Client` in synchronous functions. See issue astral-sh#8451 ## Test Plan New snapshots for `ASYNC212.py` with `cargo insta test`.
Summary
Adds new rule to find and report use of
httpx.Client
in synchronousfunctions.
See issue #8451
Test Plan
New snapshots for
ASYNC212.py
withcargo insta test
.