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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ruff] Ambiguous pattern passed to pytest.raises() (RUF043) #14966

Merged
merged 10 commits into from
Dec 18, 2024

Conversation

InSyncWithFoo
Copy link
Contributor

Summary

Resolves #13705.

Test Plan

cargo nextest run and cargo insta test.

Copy link
Contributor

github-actions bot commented Dec 14, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+503 -0 violations, +0 -0 fixes in 16 projects; 39 projects unchanged)

DisnakeDev/disnake (+14 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ tests/test_embeds.py:353:42: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/test_embeds.py:359:42: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/test_embeds.py:365:42: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/test_embeds.py:371:42: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/test_embeds.py:393:42: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/test_embeds.py:400:42: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/test_embeds.py:406:42: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/test_embeds.py:412:42: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/test_flags.py:163:45: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/test_flags.py:167:45: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
... 4 additional changes omitted for project

RasaHQ/rasa (+10 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ tests/core/test_migrate.py:1066:30: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/core/test_migrate.py:1116:15: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/core/test_migrate.py:680:15: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/core/test_migrate.py:720:15: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/core/test_migrate.py:877:30: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/engine/test_validation.py:146:62: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/engine/test_validation.py:352:47: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/engine/test_validation.py:390:47: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/engine/test_validation.py:907:47: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/graph_components/validators/test_default_recipe_validator.py:462:54: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw

apache/airflow (+114 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ kubernetes_tests/test_kubernetes_pod_operator.py:631:52: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ kubernetes_tests/test_kubernetes_pod_operator.py:671:52: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ providers/tests/amazon/aws/auth_manager/avp/test_facade.py:203:52: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ providers/tests/amazon/aws/auth_manager/avp/test_facade.py:246:37: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ providers/tests/amazon/aws/auth_manager/avp/test_facade.py:288:52: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ providers/tests/amazon/aws/hooks/test_base_aws.py:861:46: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ providers/tests/amazon/aws/hooks/test_base_aws.py:882:46: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ providers/tests/amazon/aws/hooks/test_comprehend.py:131:37: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ providers/tests/amazon/aws/hooks/test_datasync.py:129:52: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ providers/tests/amazon/aws/hooks/test_s3.py:1406:45: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ providers/tests/amazon/aws/hooks/test_s3.py:160:17: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
... 103 additional changes omitted for project

apache/superset (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ tests/integration_tests/db_engine_specs/hive_tests.py:256:19: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw

bokeh/bokeh (+3 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ tests/unit/bokeh/core/test_properties.py:395:48: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/unit/bokeh/core/test_properties.py:398:48: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/unit/bokeh/core/test_properties.py:401:48: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw

ibis-project/ibis (+18 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ ibis/backends/clickhouse/tests/test_client.py:424:40: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ ibis/backends/flink/tests/test_ddl.py:141:15: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ ibis/backends/impala/tests/test_exprs.py:593:48: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ ibis/backends/pyspark/tests/test_udf.py:72:15: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ ibis/backends/tests/test_client.py:330:34: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ ibis/backends/tests/test_generic.py:449:34: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ ibis/backends/tests/test_generic.py:454:34: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ ibis/backends/tests/test_vectorized_udf.py:358:15: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ ibis/backends/tests/test_vectorized_udf.py:479:41: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ ibis/common/tests/test_annotations.py:251:32: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
... 8 additional changes omitted for project

pandas-dev/pandas (+178 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ pandas/tests/apply/test_numba.py:110:15: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/apply/test_numba.py:116:15: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/arithmetic/test_datetime64.py:1857:34: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/arrays/boolean/test_construction.py:189:42: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/arrays/boolean/test_construction.py:192:42: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/arrays/boolean/test_construction.py:30:42: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/arrays/boolean/test_construction.py:33:42: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/arrays/categorical/test_analytics.py:125:45: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/arrays/interval/test_overlaps.py:66:55: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/arrays/sparse/test_accessor.py:247:30: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/arrays/sparse/test_accessor.py:96:50: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/arrays/string_/test_string.py:296:45: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/arrays/test_array.py:518:26: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/arrays/test_datetimelike.py:504:45: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/computation/test_eval.py:1871:41: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/config/test_config.py:273:48: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ pandas/tests/dtypes/cast/test_construct_object_arr.py:19:41: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
... 161 additional changes omitted for project

pypa/build (+3 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ tests/test_env.py:66:44: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/test_projectbuilder.py:464:52: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/test_util.py:35:15: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw

pypa/cibuildwheel (+2 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ unit_test/projectfiles_test.py:272:49: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ unit_test/projectfiles_test.py:277:40: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw

python-poetry/poetry (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ tests/packages/test_locker.py:744:44: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw

reflex-dev/reflex (+3 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ tests/units/components/core/test_match.py:202:15: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/units/components/core/test_match.py:232:15: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw
+ tests/units/components/core/test_match.py:307:42: RUF043 Pattern passed to `match=` contains metacharacters but is neither escaped nor raw

... Truncated remaining completed project reports due to GitHub comment length restrictions

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF043 503 503 0 0 0

@InSyncWithFoo
Copy link
Contributor Author

InSyncWithFoo commented Dec 14, 2024

This is a really noisy rule. I checked some of the new violations; they all seem to be true positives.

The rule currently don't have a fix. Should I add an unsafe "Wrap in re.escape()"?

@AlexWaygood AlexWaygood self-requested a review December 14, 2024 10:59
@AlexWaygood
Copy link
Member

This looks much better now, thank you! I feel like I'd prefer to have an example in the docs that uses a . character in the string, though. Some of these ecosystem hits are great examples of cases where they're almost certainly not testing exactly what they wanted to test:

And I think using an unescaped . in the string is almost certainly the most common way in which people hit this bug-waiting-to-happen

@InSyncWithFoo
Copy link
Contributor Author

Done. Should we suggest an unsafe fix to go with it?

@AlexWaygood
Copy link
Member

Done. Should we suggest an unsafe fix to go with it?

I'm not sure it's a good idea. Lots of the time it's pretty easy as a human to tell from looking at the string whether they intended the string to be a regex or a "plain" string. But I think it would be hard for Ruff to provide an accurate fix.

E.g. here it's obvious that they did want the string to be a regex, so the correct fix would be to add the r prefix to the string. But in other hits in the same repo, it's pretty obvious that the correct fix would be to escape the character, either using backslashes or re.escape(). Whichever fix we provide, it might be more annoying than helpful to have Ruff suggest the autofix if it's going to be wrong ~50% of the time, even if it's an unsafe fix.

@InSyncWithFoo
Copy link
Contributor Author

Whichever fix we provide, it might be more annoying than helpful to have Ruff suggest the autofix if it's going to be wrong ~50% of the time, even if it's an unsafe fix.

Too bad we can only suggest one fix per violation. If that weren't a thing, letting the user choose would be a very good solution.

@AlexWaygood AlexWaygood self-requested a review December 15, 2024 12:18
@AlexWaygood AlexWaygood added rule Implementing or modifying a lint rule preview Related to preview mode features labels Dec 15, 2024
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

I'm not sure this should be a PT rule. We generally don't add a rule to a category corresponding to a pre-existing linter unless:

  • the pre-existing linter also has the rule
  • or the linter has been publicly declared to be deprecated/ummaintained
  • or the linter has not seen any activity for >=2 years

Could you move this to the RUF category? (The other option would be to open an issue with flake8-pytest-style asking if they would be willing to add the rule)

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Looks fantastic other than the comments I've just left. I think this is going to be a really useful rule!

@InSyncWithFoo InSyncWithFoo changed the title [flake8-pytest-style] Ambiguous pattern passed to pytest.raises() (PT201) [ruff] Ambiguous pattern passed to pytest.raises() (RUF043) Dec 17, 2024
@InSyncWithFoo
Copy link
Contributor Author

@AlexWaygood Actually, it wasn't so fantastic. There was a major bug: The last = next line is not reached in many cases due to early continues.

It was only because of your suggestion that I added more tests and thereby decided to also detect metasequences, which eventually lead to the discover of the bug. Thanks a lot!

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@AlexWaygood AlexWaygood enabled auto-merge (squash) December 18, 2024 11:52
@AlexWaygood AlexWaygood merged commit ac81c72 into astral-sh:main Dec 18, 2024
20 checks passed
@InSyncWithFoo InSyncWithFoo deleted the PT201 branch December 18, 2024 15:33
MichaReiser pushed a commit that referenced this pull request Dec 18, 2024
(Accidentally introduced in #14966.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

detect if a regex escape is needed in a pytest match statement
3 participants