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
Implementing TRY400 #2115
Implementing TRY400 #2115
Conversation
We use the RustPython AST, defined here: https://github.com/RustPython/RustPython/blob/main/compiler/ast/src/ast_gen.rs It's actually generated by reading from the Python ASDL files, so it's pretty similar to the CPython AST. |
8a6c429
to
102972e
Compare
I have translated the raw python logic from the tryceratops library and I copied other rules to handle the reporting. I think the violation might be triggered for any method "error" used in the except block. I would need to ensure that the object that the method is called on is a logger, but I'm not sure about how to do that. |
Yeah, we don't have any way to know for sure that the object is a logger. Like the original plugin, this will raise on any |
(Or we just accept this limitation, given that it exists "upstream".) |
I think sticking to the "upstream" plugin when there are no clear better alternatives for the implementation is preferable. If you're ok with the code I can make this PR ready for review. |
102972e
to
592f9fa
Compare
592f9fa
to
99c3eff
Compare
I fixed the last issue, it's ready for review ! |
Sounds good. I'll give this a look today and merge. |
I was reading about other rules and their auto fixing part and was wondering what is the approach in this case: we know that the rule will have false negatives, which an autofix would definitely break. If we want to auto fix this rule, we might actually switch to checking specific cases (like logging and logger) to ensure we only have false positives and then autofix it by changing |
I think this one is too uncertain to allow an autofix. The one case we could fix is, if the call is |
@@ -20,6 +20,7 @@ mod tests { | |||
#[test_case(Rule::VerboseRaise, Path::new("TRY201.py"); "TRY201")] | |||
#[test_case(Rule::TryConsiderElse, Path::new("TRY300.py"); "TRY300")] | |||
#[test_case(Rule::RaiseWithinTry , Path::new("TRY301.py"); "TRY301")] | |||
#[test_case(Rule::ErrorInsteadOfException, Path::new("TRY400.py"); "TRY400")] |
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.
Quick tip for the future: in order to get the fixture to run on CI etc., we need to add a test case here via this macro.
#[derive(Default)] | ||
/// Collect `logging.error`-like calls from an AST. Matches `logging.error`, | ||
/// `logger.error`, `self.logger.error`, etc., but not arbitrary `foo.error` | ||
/// calls. |
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 decided to make this a bit stricter. I'm just erring on the side of false negatives these days a little bit, especially for checks like this that lack strong static verifiability. Sorry for the last minute change!
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [ruff](https://togithub.com/charliermarsh/ruff) | `^0.0.235` -> `^0.0.236` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.236/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.236/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.236/compatibility-slim/0.0.235)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.236/confidence-slim/0.0.235)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.236`](https://togithub.com/charliermarsh/ruff/releases/tag/v0.0.236) [Compare Source](https://togithub.com/charliermarsh/ruff/compare/v0.0.235...v0.0.236) #### What's Changed ##### Rules - feat: implement `TRY002` and `TRY003` by [@​karpa4o4](https://togithub.com/karpa4o4) in [astral-sh/ruff#2135 - Implementing `TRY400` by [@​Flowake](https://togithub.com/Flowake) in [astral-sh/ruff#2115 - Implement some rules from `flake8-logging-format` (`G`) by [@​edgarrmondragon](https://togithub.com/edgarrmondragon) in [astral-sh/ruff#2150 ##### Settings - Add strictness setting for `flake8-typing-imports` by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#2221 - Implement `exempt-modules` setting from flake8-type-checking by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#2230 ##### Bug fixes - flake8\_executable: Only match shebang at beginning of line by [@​andersk](https://togithub.com/andersk) in [astral-sh/ruff#2183 - Don't flag B009/B010 if identifiers would be mangled by [@​sciyoshi](https://togithub.com/sciyoshi) in [astral-sh/ruff#2204 - fix: --explain reporting the wrong linter by [@​not-my-profile](https://togithub.com/not-my-profile) in [astral-sh/ruff#2215 - Preserve indentation when fixing via LibCST by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#2223 - Avoid erroneous class autofixes in indented blocks by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#2226 - Fix range for `try-consider-else` by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#2228 - Avoid flagging blind exceptions with valid logging by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#2232 - Avoid removing trailing comments on `pass` statements by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#2235 - Allow `pytest` in shebang by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#2237 - Wrap return-bool-condition-directly fixes in bool() by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#2240 #### New Contributors - [@​Flowake](https://togithub.com/Flowake) made their first contribution in [astral-sh/ruff#2115 - [@​henryiii](https://togithub.com/henryiii) made their first contribution in [astral-sh/ruff#2200 - [@​sciyoshi](https://togithub.com/sciyoshi) made their first contribution in [astral-sh/ruff#2204 **Full Changelog**: astral-sh/ruff@v0.0.235...v0.0.236 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/ixm-one/pytest-cmake-presets). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4xMTEuMSIsInVwZGF0ZWRJblZlciI6IjM0LjExMS4xIn0=--> Signed-off-by: Renovate Bot <bot@renovateapp.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Currently implementing TRY400 (#2056)
I cannot find the documentation for the ast parser used by ruff, where could I find it ? That would make my life easier.