Skip to content

Make SIM118 fix as safe when the expression is a known dictionary #8525

Merged
charliermarsh merged 1 commit intomainfrom
charlie/key-in
Nov 6, 2023
Merged

Make SIM118 fix as safe when the expression is a known dictionary #8525
charliermarsh merged 1 commit intomainfrom
charlie/key-in

Conversation

@charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Nov 6, 2023

Summary

Given key in obj.keys(), obj could be a dictionary, or it could be another type that defines
a .keys() method. In the latter case, removing the .keys() attribute
could lead to a runtime error.

Previously, we marked all SIM118 fixes as unsafe for this reason; however, in preview, we now mark them as safe if we can
infer that the expression is a dictionary.

Test Plan

Added a preview fixture.

@charliermarsh charliermarsh added fixes Related to suggested fixes for violations preview Related to preview mode features labels Nov 6, 2023
@charliermarsh charliermarsh requested a review from zanieb November 6, 2023 20:15
/// ## References
/// - [Python documentation: Mapping Types](https://docs.python.org/3/library/stdtypes.html#mapping-types-dict)
///
/// [preview]: https://docs.astral.sh/ruff/preview/
Copy link
Member

Choose a reason for hiding this comment

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

How does this render?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed, thanks.

@charliermarsh charliermarsh enabled auto-merge (squash) November 6, 2023 21:00
@charliermarsh charliermarsh merged commit 3730137 into main Nov 6, 2023
@charliermarsh charliermarsh deleted the charlie/key-in branch November 6, 2023 21:06
@github-actions
Copy link
Contributor

github-actions bot commented Nov 6, 2023

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+0 -0 violations, +380 -0 fixes in 41 projects)

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

ruff check --no-cache --exit-zero --select ALL --preview

- airflow/providers/google/cloud/utils/datafusion.py:33:79: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ airflow/providers/google/cloud/utils/datafusion.py:33:79: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- dev/breeze/src/airflow_breeze/utils/provider_dependencies.py:68:13: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ dev/breeze/src/airflow_breeze/utils/provider_dependencies.py:68:13: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- tests/providers/amazon/aws/executors/ecs/test_ecs_executor.py:765:13: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ tests/providers/amazon/aws/executors/ecs/test_ecs_executor.py:765:13: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- tests/providers/amazon/aws/executors/ecs/test_ecs_executor.py:805:24: SIM118 Use `key not in dict` instead of `key not in dict.keys()`
+ tests/providers/amazon/aws/executors/ecs/test_ecs_executor.py:805:24: SIM118 [*] Use `key not in dict` instead of `key not in dict.keys()`
- tests/providers/amazon/aws/executors/ecs/test_ecs_executor.py:807:24: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ tests/providers/amazon/aws/executors/ecs/test_ecs_executor.py:807:24: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
... 10 additional changes omitted for project

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

ruff check --no-cache --exit-zero --select ALL --preview

- tests/unit/bokeh/models/test_sources.py:108:13: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ tests/unit/bokeh/models/test_sources.py:108:13: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- tests/unit/bokeh/models/test_sources.py:120:13: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ tests/unit/bokeh/models/test_sources.py:120:13: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- tests/unit/bokeh/models/test_sources.py:134:13: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ tests/unit/bokeh/models/test_sources.py:134:13: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- tests/unit/bokeh/models/test_sources.py:82:13: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ tests/unit/bokeh/models/test_sources.py:82:13: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- tests/unit/bokeh/models/test_sources.py:96:13: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ tests/unit/bokeh/models/test_sources.py:96:13: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`

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

ruff check --no-cache --exit-zero --preview

- Packs/ANYRUN/Integrations/ANYRUN/ANYRUN.py:921:12: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/ANYRUN/Integrations/ANYRUN/ANYRUN.py:921:12: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/Absolute/Integrations/Absolute/Absolute.py:331:8: SIM118 Use `key not in dict` instead of `key not in dict.keys()`
+ Packs/Absolute/Integrations/Absolute/Absolute.py:331:8: SIM118 [*] Use `key not in dict` instead of `key not in dict.keys()`
- Packs/AbuseDB/Integrations/AbuseDB/AbuseDB.py:144:57: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/AbuseDB/Integrations/AbuseDB/AbuseDB.py:144:57: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/AlienVault_OTX/Integrations/AlienVault_OTX_v2/AlienVault_OTX_v2.py:282:46: SIM118 Use `key not in dict` instead of `key not in dict.keys()`
+ Packs/AlienVault_OTX/Integrations/AlienVault_OTX_v2/AlienVault_OTX_v2.py:282:46: SIM118 [*] Use `key not in dict` instead of `key not in dict.keys()`
- Packs/Anomali_ThreatStream/Integrations/Anomali_ThreatStream_v2/Anomali_ThreatStream_v2.py:383:40: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/Anomali_ThreatStream/Integrations/Anomali_ThreatStream_v2/Anomali_ThreatStream_v2.py:383:40: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/Anomali_ThreatStream/Integrations/Anomali_ThreatStream_v2/Anomali_ThreatStream_v2.py:777:47: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/Anomali_ThreatStream/Integrations/Anomali_ThreatStream_v2/Anomali_ThreatStream_v2.py:777:47: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/ApiModules/Scripts/HTTPFeedApiModule/HTTPFeedApiModule.py:291:13: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/ApiModules/Scripts/HTTPFeedApiModule/HTTPFeedApiModule.py:291:13: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/AppNovi/Integrations/appNovi/appNovi.py:157:31: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/AppNovi/Integrations/appNovi/appNovi.py:157:31: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/AppNovi/Integrations/appNovi/appNovi.py:201:31: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/AppNovi/Integrations/appNovi/appNovi.py:201:31: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/AppNovi/Integrations/appNovi/appNovi.py:249:46: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/AppNovi/Integrations/appNovi/appNovi.py:249:46: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/AppNovi/Integrations/appNovi/appNovi.py:288:46: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/AppNovi/Integrations/appNovi/appNovi.py:288:46: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/AppNovi/Integrations/appNovi/appNovi.py:353:50: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/AppNovi/Integrations/appNovi/appNovi.py:353:50: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/AtlassianConfluenceCloud/Integrations/AtlassianConfluenceCloud/AtlassianConfluenceCloud.py:533:12: SIM118 Use `key not in dict` instead of `key not in dict.keys()`
+ Packs/AtlassianConfluenceCloud/Integrations/AtlassianConfluenceCloud/AtlassianConfluenceCloud.py:533:12: SIM118 [*] Use `key not in dict` instead of `key not in dict.keys()`
- Packs/Aws-SecretsManager/Integrations/AwsSecretsManager/AwsSecretsManager.py:235:9: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/Aws-SecretsManager/Integrations/AwsSecretsManager/AwsSecretsManager.py:235:9: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/BPA/Integrations/BPA/BPA.py:125:9: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/BPA/Integrations/BPA/BPA.py:125:9: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:566:12: SIM118 Use `key not in dict` instead of `key not in dict.keys()`
+ Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:566:12: SIM118 [*] Use `key not in dict` instead of `key not in dict.keys()`
- Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:707:9: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/Base/Scripts/DBotFindSimilarIncidents/DBotFindSimilarIncidents.py:707:9: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/Base/Scripts/DBotTrainClustering/DBotTrainClustering.py:241:50: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/Base/Scripts/DBotTrainClustering/DBotTrainClustering.py:241:50: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/Base/Scripts/DBotTrainClustering/DBotTrainClustering.py:242:20: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/Base/Scripts/DBotTrainClustering/DBotTrainClustering.py:242:20: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/Base/Scripts/DBotTrainClustering/DBotTrainClustering.py:259:78: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/Base/Scripts/DBotTrainClustering/DBotTrainClustering.py:259:78: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/BmcHelixRemedyForce/Integrations/BmcHelixRemedyForce/BmcHelixRemedyForce.py:1025:9: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/BmcHelixRemedyForce/Integrations/BmcHelixRemedyForce/BmcHelixRemedyForce.py:1025:9: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/BmcHelixRemedyForce/Integrations/BmcHelixRemedyForce/BmcHelixRemedyForce.py:1413:37: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/BmcHelixRemedyForce/Integrations/BmcHelixRemedyForce/BmcHelixRemedyForce.py:1413:37: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
- Packs/BmcHelixRemedyForce/Integrations/BmcHelixRemedyForce/BmcHelixRemedyForce.py:1426:10: SIM118 Use `key in dict` instead of `key in dict.keys()`
+ Packs/BmcHelixRemedyForce/Integrations/BmcHelixRemedyForce/BmcHelixRemedyForce.py:1426:10: SIM118 [*] Use `key in dict` instead of `key in dict.keys()`
... 304 additional changes omitted for project

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
SIM118 380 0 0 380 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fixes Related to suggested fixes for violations preview Related to preview mode features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants