[UP031] When encountering "%s" % var offer unsafe fix#11019
Merged
Conversation
… that the "problem" is only that the fix is not offered in some cases; general readability
…UP031_0.py (they are now true positives)
Contributor
|
| 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 |
crates/ruff_linter/src/rules/pyupgrade/rules/printf_string_formatting.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/pyupgrade/rules/printf_string_formatting.rs
Outdated
Show resolved
Hide resolved
MichaReiser
reviewed
Apr 19, 2024
crates/ruff_linter/src/rules/pyupgrade/rules/printf_string_formatting.rs
Outdated
Show resolved
Hide resolved
…y" and change the prose to follow
… variable inference/deref code
…biguous variable is a tuple or not
… merge-base, despite apparent churn in this commit
"%s" % var attempt to dereference var and offer fixes more consistently."%s" % var offer unsafe fix
charliermarsh
approved these changes
Apr 20, 2024
3 tasks
Contributor
|
Am I the only one who does not like the fact that this even converts 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):analyze::typing::find_binding_value.analyze::typing::is_tuple.Alternatives
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 tocrates/ruff_linter/resources/test/fixtures/pyupgrade/UP031_0.py.Existing false negatives that are now true positives
One of them newly offers a fix.
This fix occurs because the new code dereferences
barto where it was defined earlier in the file as a non-tuple: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
One of these offers a 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.