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

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

Merged
merged 1 commit into from
Nov 6, 2023

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
/// ## 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
15 checks passed
@charliermarsh charliermarsh deleted the charlie/key-in branch November 6, 2023 21:06
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