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

[UP031] When encountering "%s" % var offer unsafe fix #11019

Merged
merged 12 commits into from Apr 22, 2024
Merged

Conversation

plredmond
Copy link
Contributor

@plredmond plredmond commented Apr 19, 2024

Resolves #10187

Old PR description; accurate through commit e86dd7d; probably best to leave this fold closed

Description of change

In the case of a printf-style format string with only one %-placeholder and a variable at right (e.g. "%s" % var):

  • The new behavior attempts to dereference the variable and then match on the bound expression to distinguish between a 1-tuple (fix), n-tuple (bug 🐛), or a non-tuple (fix). Dereferencing is via analyze::typing::find_binding_value.
  • If the variable cannot be dereferenced, then the type-analysis routine is called to distinguish only tuple (no-fix) or non-tuple (fix). Type analysis is via analyze::typing::is_tuple.
  • If any of the above fails, the rule still fires, but no fix is offered.

Alternatives

  • If the reviewers think that singling out the 1-tuple case is too complicated, I will remove that.
  • The ecosystem results show that no new fixes are detected. So I could probably delete all the variable dereferencing code and code that tries to generate fixes, tbh.

Changes to existing behavior

All the previous rule-firings and fixes are unchanged except for the "false negatives" in crates/ruff_linter/resources/test/fixtures/pyupgrade/UP031_1.py. Those previous "false negatives" are now true positives and so I moved them to crates/ruff_linter/resources/test/fixtures/pyupgrade/UP031_0.py.

Existing false negatives that are now true positives
crates/ruff_linter/resources/test/fixtures/pyupgrade/UP031_0.py:134:1: UP031 Use format specifiers instead of percent format
    |
133 | # UP031 (no longer false negatives)
134 | 'Hello %s' % bar
    | ^^^^^^^^^^^^^^^^ UP031
135 |
136 | 'Hello %s' % bar.baz
    |
    = help: Replace with format specifiers

crates/ruff_linter/resources/test/fixtures/pyupgrade/UP031_0.py:136:1: UP031 Use format specifiers instead of percent format
    |
134 | 'Hello %s' % bar
135 |
136 | 'Hello %s' % bar.baz
    | ^^^^^^^^^^^^^^^^^^^^ UP031
137 |
138 | 'Hello %s' % bar['bop']
    |
    = help: Replace with format specifiers

crates/ruff_linter/resources/test/fixtures/pyupgrade/UP031_0.py:138:1: UP031 Use format specifiers instead of percent format
    |
136 | 'Hello %s' % bar.baz
137 |
138 | 'Hello %s' % bar['bop']
    | ^^^^^^^^^^^^^^^^^^^^^^^ UP031
    |
    = help: Replace with format specifiers

One of them newly offers a fix.

 # UP031 (no longer false negatives)
-'Hello %s' % bar
+'Hello {}'.format(bar)

This fix occurs because the new code dereferences bar to where it was defined earlier in the file as a non-tuple:

bar = {"bar": y}

Behavior requiring new tests

Additionally, we now handle a few cases that we didn't previously test. These cases are when a string has a single %-placeholder and the righthand operand to the modulo operator is a variable which can be dereferenced. One of those was shown in the previous section (the "dereference non-tuple" case).

New cases handled
crates/ruff_linter/resources/test/fixtures/pyupgrade/UP031_0.py:126:1: UP031 [*] Use format specifiers instead of percent format
    |
125 | t1 = (x,)
126 | "%s" % t1
    | ^^^^^^^^^ UP031
127 | # UP031: deref t1 to 1-tuple, offer fix
    |
    = help: Replace with format specifiers

crates/ruff_linter/resources/test/fixtures/pyupgrade/UP031_0.py:130:1: UP031 Use format specifiers instead of percent format
    |
129 | t2 = (x,y)
130 | "%s" % t2
    | ^^^^^^^^^ UP031
131 | # UP031: deref t2 to n-tuple, this is a bug
    |
    = help: Replace with format specifiers

One of these offers a fix.

 t1 = (x,)
-"%s" % t1
+"{}".format(t1[0])
 # UP031: deref t1 to 1-tuple, offer fix

The other doesn't offer a fix because it's a bug.



Changes to existing behavior

In the case of a string with a single %-placeholder and a single ambiguous righthand argument to the modulo operator, (e.g. "%s" % var) the rule now fires and offers a fix. We explain about this in the "fix safety" section of the updated documentation.

Documentation changes

I swapped the order of the "known problems" and the "examples" sections so that the examples which describe the rule are first, before the exceptions to the rule are described. I also tweaked the language to be more explicit, as I had trouble understanding the documentation at first. The "known problems" section is now "fix safety" but the content is largely similar.

The diff of the documentation changes looks a little difficult unless you look at the individual commits.

Copy link
Contributor

github-actions bot commented Apr 19, 2024

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+658 -0 violations, +0 -0 fixes in 9 projects; 35 projects unchanged)

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

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

+ examples/interaction/js_callbacks/customjs_for_hover.py:37:8: UP031 Use format specifiers instead of percent format
+ examples/models/trail.py:73:23: UP031 Use format specifiers instead of percent format
+ examples/models/trail.py:92:23: UP031 Use format specifiers instead of percent format
+ examples/server/app/server_auth/app.py:56:50: UP031 Use format specifiers instead of percent format
+ src/bokeh/application/application.py:168:32: UP031 Use format specifiers instead of percent format
+ src/bokeh/client/connection.py:355:31: UP031 Use format specifiers instead of percent format
+ src/bokeh/command/bootstrap.py:92:13: UP031 Use format specifiers instead of percent format
+ src/bokeh/command/subcommands/file_output.py:88:19: UP031 Use format specifiers instead of percent format
+ src/bokeh/command/subcommands/serve.py:487:19: UP031 Use format specifiers instead of percent format
+ src/bokeh/command/subcommands/serve.py:494:19: UP031 Use format specifiers instead of percent format
... 53 additional changes omitted for project

demisto/content (+326 -0 violations, +0 -0 fixes)

+ Packs/Accessdata/Integrations/Accessdata/Accessdata.py:380:9: UP031 Use format specifiers instead of percent format
+ Packs/ActiveMQ/Integrations/ActiveMQ/ActiveMQ.py:25:25: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/ACME/ACME.py:176:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/AlibabaCloud/AlibabaCloud.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/AzureComputeV3/AzureComputeV3.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/AzureNetworking/AzureNetworking.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/CiscoIOS/CiscoIOS.py:181:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/CiscoNXOS/CiscoNXOS.py:181:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/DNS/DNS.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/HCloud/HCloud.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/Kubernetes/Kubernetes.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/Linux/Linux.py:176:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/MicrosoftWindows/MicrosoftWindows.py:173:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/OpenSSL/OpenSSL.py:176:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/VMwareV2/VMwareV2.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/AwakeSecurity/Integrations/AwakeSecurity/AwakeSecurity.py:447:5: UP031 Use format specifiers instead of percent format
+ Packs/BMCDiscovery/Integrations/BMCDiscovery/BMCDiscovery.py:336:64: UP031 Use format specifiers instead of percent format
+ Packs/BMCDiscovery/Integrations/BMCDiscovery/BMCDiscovery.py:56:21: UP031 Use format specifiers instead of percent format
+ Packs/BMCDiscovery/Integrations/BMCDiscovery/BMCDiscovery.py:58:21: UP031 Use format specifiers instead of percent format
+ Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:386:27: UP031 Use format specifiers instead of percent format
+ Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:481:41: UP031 Use format specifiers instead of percent format
+ Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:490:17: UP031 Use format specifiers instead of percent format
+ Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:539:26: UP031 Use format specifiers instead of percent format
+ Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:588:20: UP031 Use format specifiers instead of percent format
... 302 additional changes omitted for project

docker/docker-py (+1 -0 violations, +0 -0 fixes)

+ docker/utils/ports.py:41:22: UP031 Use format specifiers instead of percent format

freedomofpress/securedrop (+120 -0 violations, +0 -0 fixes)

+ securedrop/models.py:201:16: UP031 Use format specifiers instead of percent format
+ securedrop/models.py:299:16: UP031 Use format specifiers instead of percent format
+ securedrop/models.py:94:16: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_logger.py:66:14: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_meta.py:1000:29: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_meta.py:1026:27: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_meta.py:1034:23: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_meta.py:1037:22: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_meta.py:1048:29: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_meta.py:1050:29: UP031 Use format specifiers instead of percent format
... 110 additional changes omitted for project

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

+ ibis/backends/postgres/tests/test_functions.py:746:55: UP031 Use format specifiers instead of percent format
+ ibis/backends/postgres/tests/test_functions.py:759:34: UP031 Use format specifiers instead of percent format
+ ibis/backends/postgres/tests/test_functions.py:772:55: UP031 Use format specifiers instead of percent format
+ ibis/backends/risingwave/tests/test_functions.py:546:55: UP031 Use format specifiers instead of percent format
+ ibis/backends/risingwave/tests/test_functions.py:561:55: UP031 Use format specifiers instead of percent format

mlflow/mlflow (+33 -0 violations, +0 -0 fixes)

+ examples/pytorch/CaptumExample/Titanic_Captum_Interpret.py:133:39: UP031 Use format specifiers instead of percent format
+ examples/pytorch/mnist_tensorboard_artifact.py:218:9: UP031 Use format specifiers instead of percent format
+ examples/pytorch/mnist_tensorboard_artifact.py:226:9: UP031 Use format specifiers instead of percent format
+ examples/rapids/mlflow_project/notebooks/rapids_mlflow.ipynb:cell 13:44:18: UP031 Use format specifiers instead of percent format
+ examples/sklearn_elasticnet_wine/train.ipynb:cell 2:66:15: UP031 Use format specifiers instead of percent format
+ examples/sklearn_elasticnet_wine/train.ipynb:cell 2:67:15: UP031 Use format specifiers instead of percent format
+ examples/sklearn_elasticnet_wine/train.ipynb:cell 2:68:15: UP031 Use format specifiers instead of percent format
+ mlflow/legacy_databricks_cli/configure/provider.py:131:13: UP031 Use format specifiers instead of percent format
+ mlflow/legacy_databricks_cli/configure/provider.py:175:25: UP031 Use format specifiers instead of percent format
+ mlflow/projects/_project_spec.py:111:17: UP031 Use format specifiers instead of percent format
... 23 additional changes omitted for project

python/mypy (+82 -0 violations, +0 -0 fixes)

+ misc/dump-ast.py:42:34: UP031 Use format specifiers instead of percent format
+ misc/gen_blog_post_html.py:31:26: UP031 Use format specifiers instead of percent format
+ mypy/config_parser.py:346:27: UP031 Use format specifiers instead of percent format
+ mypy/config_parser.py:666:12: UP031 Use format specifiers instead of percent format
+ mypy/dmypy/client.py:599:37: UP031 Use format specifiers instead of percent format
+ mypy/main.py:1320:13: UP031 Use format specifiers instead of percent format
+ mypy/main.py:1487:17: UP031 Use format specifiers instead of percent format
+ mypy/main.py:1491:17: UP031 Use format specifiers instead of percent format
+ mypy/semanal.py:2015:31: UP031 Use format specifiers instead of percent format
+ mypy/semanal.py:2332:17: UP031 Use format specifiers instead of percent format
... 72 additional changes omitted for project

scikit-build/scikit-build (+4 -0 violations, +0 -0 fixes)

+ tests/test_hello_fortran.py:61:9: UP031 Use format specifiers instead of percent format
+ tests/test_hello_fortran.py:62:9: UP031 Use format specifiers instead of percent format
+ tests/test_issue342_cmake_osx_args_in_setup.py:156:41: UP031 Use format specifiers instead of percent format
+ tests/test_setup.py:349:45: UP031 Use format specifiers instead of percent format

indico/indico (+24 -0 violations, +0 -0 fixes)

+ indico/cli/shell.py:39:29: UP031 Use format specifiers instead of percent format
+ indico/cli/shell.py:41:29: UP031 Use format specifiers instead of percent format
+ indico/cli/shell.py:52:29: UP031 Use format specifiers instead of percent format
+ indico/cli/shell.py:54:29: UP031 Use format specifiers instead of percent format
+ indico/core/db/sqlalchemy/custom/ip_network.py:44:22: UP031 Use format specifiers instead of percent format
+ indico/migrations/versions/20200904_1543_f37d509e221c_add_user_profile_picture_source_column.py:38:16: UP031 Use format specifiers instead of percent format
+ indico/modules/events/static/offline.py:196:41: UP031 Use format specifiers instead of percent format
+ indico/modules/events/static/offline.py:198:40: UP031 Use format specifiers instead of percent format
+ indico/modules/events/static/offline.py:202:42: UP031 Use format specifiers instead of percent format
+ indico/modules/rb/api.py:329:22: UP031 Use format specifiers instead of percent format
... 14 additional changes omitted for project

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

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
UP031 658 658 0 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+658 -0 violations, +0 -0 fixes in 9 projects; 35 projects unchanged)

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

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

+ examples/interaction/js_callbacks/customjs_for_hover.py:37:8: UP031 Use format specifiers instead of percent format
+ examples/models/trail.py:73:23: UP031 Use format specifiers instead of percent format
+ examples/models/trail.py:92:23: UP031 Use format specifiers instead of percent format
+ examples/server/app/server_auth/app.py:56:50: UP031 Use format specifiers instead of percent format
+ src/bokeh/application/application.py:168:32: UP031 Use format specifiers instead of percent format
+ src/bokeh/client/connection.py:355:31: UP031 Use format specifiers instead of percent format
+ src/bokeh/command/bootstrap.py:92:13: UP031 Use format specifiers instead of percent format
+ src/bokeh/command/subcommands/file_output.py:88:19: UP031 Use format specifiers instead of percent format
+ src/bokeh/command/subcommands/serve.py:487:19: UP031 Use format specifiers instead of percent format
+ src/bokeh/command/subcommands/serve.py:494:19: UP031 Use format specifiers instead of percent format
... 53 additional changes omitted for project

demisto/content (+326 -0 violations, +0 -0 fixes)

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

+ Packs/Accessdata/Integrations/Accessdata/Accessdata.py:380:9: UP031 Use format specifiers instead of percent format
+ Packs/ActiveMQ/Integrations/ActiveMQ/ActiveMQ.py:25:25: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/ACME/ACME.py:176:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/AlibabaCloud/AlibabaCloud.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/AzureComputeV3/AzureComputeV3.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/AzureNetworking/AzureNetworking.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/CiscoIOS/CiscoIOS.py:181:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/CiscoNXOS/CiscoNXOS.py:181:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/DNS/DNS.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/HCloud/HCloud.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/Kubernetes/Kubernetes.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/Linux/Linux.py:176:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/MicrosoftWindows/MicrosoftWindows.py:173:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/OpenSSL/OpenSSL.py:176:40: UP031 Use format specifiers instead of percent format
+ Packs/Ansible_Powered_Integrations/Integrations/VMwareV2/VMwareV2.py:136:40: UP031 Use format specifiers instead of percent format
+ Packs/AwakeSecurity/Integrations/AwakeSecurity/AwakeSecurity.py:447:5: UP031 Use format specifiers instead of percent format
+ Packs/BMCDiscovery/Integrations/BMCDiscovery/BMCDiscovery.py:336:64: UP031 Use format specifiers instead of percent format
+ Packs/BMCDiscovery/Integrations/BMCDiscovery/BMCDiscovery.py:56:21: UP031 Use format specifiers instead of percent format
+ Packs/BMCDiscovery/Integrations/BMCDiscovery/BMCDiscovery.py:58:21: UP031 Use format specifiers instead of percent format
+ Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:386:27: UP031 Use format specifiers instead of percent format
+ Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:481:41: UP031 Use format specifiers instead of percent format
+ Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:490:17: UP031 Use format specifiers instead of percent format
+ Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:539:26: UP031 Use format specifiers instead of percent format
+ Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:588:20: UP031 Use format specifiers instead of percent format
... 302 additional changes omitted for project

docker/docker-py (+1 -0 violations, +0 -0 fixes)

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

+ docker/utils/ports.py:41:22: UP031 Use format specifiers instead of percent format

freedomofpress/securedrop (+120 -0 violations, +0 -0 fixes)

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

+ securedrop/models.py:201:16: UP031 Use format specifiers instead of percent format
+ securedrop/models.py:299:16: UP031 Use format specifiers instead of percent format
+ securedrop/models.py:94:16: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_logger.py:66:14: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_meta.py:1000:29: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_meta.py:1026:27: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_meta.py:1034:23: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_meta.py:1037:22: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_meta.py:1048:29: UP031 Use format specifiers instead of percent format
+ securedrop/pretty_bad_protocol/_meta.py:1050:29: UP031 Use format specifiers instead of percent format
... 110 additional changes omitted for project

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

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

+ ibis/backends/postgres/tests/test_functions.py:746:55: UP031 Use format specifiers instead of percent format
+ ibis/backends/postgres/tests/test_functions.py:759:34: UP031 Use format specifiers instead of percent format
+ ibis/backends/postgres/tests/test_functions.py:772:55: UP031 Use format specifiers instead of percent format
+ ibis/backends/risingwave/tests/test_functions.py:546:55: UP031 Use format specifiers instead of percent format
+ ibis/backends/risingwave/tests/test_functions.py:561:55: UP031 Use format specifiers instead of percent format

mlflow/mlflow (+33 -0 violations, +0 -0 fixes)

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

+ examples/pytorch/CaptumExample/Titanic_Captum_Interpret.py:133:39: UP031 Use format specifiers instead of percent format
+ examples/pytorch/mnist_tensorboard_artifact.py:218:9: UP031 Use format specifiers instead of percent format
+ examples/pytorch/mnist_tensorboard_artifact.py:226:9: UP031 Use format specifiers instead of percent format
+ examples/rapids/mlflow_project/notebooks/rapids_mlflow.ipynb:cell 13:44:18: UP031 Use format specifiers instead of percent format
+ examples/sklearn_elasticnet_wine/train.ipynb:cell 2:66:15: UP031 Use format specifiers instead of percent format
+ examples/sklearn_elasticnet_wine/train.ipynb:cell 2:67:15: UP031 Use format specifiers instead of percent format
+ examples/sklearn_elasticnet_wine/train.ipynb:cell 2:68:15: UP031 Use format specifiers instead of percent format
+ mlflow/legacy_databricks_cli/configure/provider.py:131:13: UP031 Use format specifiers instead of percent format
+ mlflow/legacy_databricks_cli/configure/provider.py:175:25: UP031 Use format specifiers instead of percent format
+ mlflow/projects/_project_spec.py:111:17: UP031 Use format specifiers instead of percent format
... 23 additional changes omitted for project

python/mypy (+82 -0 violations, +0 -0 fixes)

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

+ misc/dump-ast.py:42:34: UP031 Use format specifiers instead of percent format
+ misc/gen_blog_post_html.py:31:26: UP031 Use format specifiers instead of percent format
+ mypy/config_parser.py:346:27: UP031 Use format specifiers instead of percent format
+ mypy/config_parser.py:666:12: UP031 Use format specifiers instead of percent format
+ mypy/dmypy/client.py:599:37: UP031 Use format specifiers instead of percent format
+ mypy/main.py:1320:13: UP031 Use format specifiers instead of percent format
+ mypy/main.py:1487:17: UP031 Use format specifiers instead of percent format
+ mypy/main.py:1491:17: UP031 Use format specifiers instead of percent format
+ mypy/semanal.py:2015:31: UP031 Use format specifiers instead of percent format
+ mypy/semanal.py:2332:17: UP031 Use format specifiers instead of percent format
... 72 additional changes omitted for project

scikit-build/scikit-build (+4 -0 violations, +0 -0 fixes)

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

+ tests/test_hello_fortran.py:61:9: UP031 Use format specifiers instead of percent format
+ tests/test_hello_fortran.py:62:9: UP031 Use format specifiers instead of percent format
+ tests/test_issue342_cmake_osx_args_in_setup.py:156:41: UP031 Use format specifiers instead of percent format
+ tests/test_setup.py:349:45: UP031 Use format specifiers instead of percent format

indico/indico (+24 -0 violations, +0 -0 fixes)

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

+ indico/cli/shell.py:39:29: UP031 Use format specifiers instead of percent format
+ indico/cli/shell.py:41:29: UP031 Use format specifiers instead of percent format
+ indico/cli/shell.py:52:29: UP031 Use format specifiers instead of percent format
+ indico/cli/shell.py:54:29: UP031 Use format specifiers instead of percent format
+ indico/core/db/sqlalchemy/custom/ip_network.py:44:22: UP031 Use format specifiers instead of percent format
+ indico/migrations/versions/20200904_1543_f37d509e221c_add_user_profile_picture_source_column.py:38:16: UP031 Use format specifiers instead of percent format
+ indico/modules/events/static/offline.py:196:41: UP031 Use format specifiers instead of percent format
+ indico/modules/events/static/offline.py:198:40: UP031 Use format specifiers instead of percent format
+ indico/modules/events/static/offline.py:202:42: UP031 Use format specifiers instead of percent format
+ indico/modules/rb/api.py:329:22: UP031 Use format specifiers instead of percent format
... 14 additional changes omitted for project

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

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
UP031 658 658 0 0 0

@plredmond plredmond marked this pull request as ready for review April 19, 2024 00:51
@dhruvmanila dhruvmanila added the rule Implementing or modifying a lint rule label Apr 19, 2024
@plredmond plredmond changed the title [UP031] When encountering "%s" % var attempt to dereference var and offer fixes more consistently. [UP031] When encountering "%s" % var offer unsafe fix Apr 19, 2024
@ThiefMaster
Copy link
Contributor

Am I the only one who does not like the fact that this even converts % formatting in cases where it introduces escaping of literal { in the format string/

I mean cases like this:

 def friendly_name(cls):
-    return '{%s}' % cls.name
+    return f'{{{cls.name}}}'

 def get_regex(cls, **kwargs):
-    return re.compile(r'\{%s}' % re.escape(cls.name))
+    return re.compile(rf'\{{{re.escape(cls.name)}}}')

IMHO this is clearly less readable than the previous version.

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.

UP031 not violated for variables, only literal values
5 participants