Skip to content

Conversation

twentyone212121
Copy link
Contributor

This fix closes #17026

Summary

The check for the PytestRaisesTooBroad rule is now skipped if there is a second positional argument present, which means pytest.raises is used as a function.

Test Plan

Tested on the example from the issue, which now passes the check.

pytest.raises(Exception, func, *func_args, **func_kwargs).match("error message")

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Apr 7, 2025
@MichaReiser
Copy link
Member

Thanks. Could you add a test example to https://github.com/astral-sh/ruff/blob/2b6d66b793adc9e0cc50817b59187b41f39f7691/crates/ruff_linter/resources/test/fixtures/flake8_pytest_style/PT011.py#L1-L0 with your example from the test plan

@twentyone212121
Copy link
Contributor Author

I added an ok test example. The diff in snapshots is big because error tests are moved by 7 lines

Copy link
Contributor

github-actions bot commented Apr 7, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser requested a review from Copilot April 8, 2025 06:58
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 2 out of 6 changed files in this pull request and generated no comments.

Files not reviewed (4)
  • crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT011_default.snap: Language not supported
  • crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT011_extend_broad_exceptions.snap: Language not supported
  • crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT011_glob_all.snap: Language not supported
  • crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT011_glob_prefix.snap: Language not supported
Comments suppressed due to low confidence (1)

crates/ruff_linter/src/rules/flake8_pytest_style/rules/raises.rs:192

  • [nitpick] Using the string literal "func" with a hard-coded index (1) may reduce readability. Consider defining a constant for this argument name and index to clarify its purpose when detecting the second overload.
if call.arguments.find_argument("func", 1).is_none() {

@MichaReiser MichaReiser merged commit 97dd6d1 into astral-sh:main Apr 8, 2025
22 checks passed
dcreager added a commit that referenced this pull request Apr 9, 2025
* origin/main:
  [red-knot] Default `python-platform` to current platform (#17183)
  [red-knot] Add new 'unreachable code' test case (#17306)
  [red-knot] mypy_primer: Run on `async-utils` (#17303)
  [red-knot] Add custom `__setattr__` support (#16748)
  [red-knot] Add `__init__` arguments check when doing `try_call` on a class literal (#16512)
  [`flake8-pie`] Avoid false positive for multiple assignment with `auto()` (`PIE796`) (#17274)
  [syntax-errors] Async comprehension in sync comprehension (#17177)
  [`airflow`] Expand module path check to individual symbols (`AIR302`) (#17278)
  [syntax-errors] Check annotations in annotated assignments (#17283)
  [syntax-errors] Extend annotation checks to `await` (#17282)
  [red-knot] Add support for `assert_never` (#17287)
  [`flake8-pytest-style`] Avoid false positive for legacy form of `pytest.raises` (`PT011`) (#17231)
  [red-knot] Do not show types for literal expressions on hover (#17290)
  [red-knot] Fix dead-code clippy warning (#17291)
  [red-knot] Reachability analysis (#17199)
  [red-knot] Don't use latency-sensitive for handlers (#17227)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive PT011 when using pytest.raises as a function
2 participants