[flake8-simplify] - extend open-file-with-context-handler to work with dbm.sqlite3 (SIM115) (#13104)

## Summary

Adds upcoming `dbm.sqlite3` to rule that suggests using context managers
to open things with.

See: https://docs.python.org/3.13/library/dbm.html#module-dbm.sqlite3

## Test Plan

`cargo test`
This commit is contained in:
Steve C 2024-08-26 03:11:03 -04:00 committed by GitHub
parent 5af48337a5
commit 0b5828a1e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 1 deletions

View file

@ -243,3 +243,16 @@ def aliased():
from tarfile import TarFile as TF from tarfile import TarFile as TF
f = TF("foo").open() f = TF("foo").open()
f.close() f.close()
import dbm.sqlite3
# OK
with dbm.sqlite3.open("foo.db") as f:
print(f.keys())
# OK
dbm.sqlite3.open("foo.db").close()
# SIM115
f = dbm.sqlite3.open("foo.db")
f.close()

View file

@ -165,7 +165,7 @@ fn is_open_preview(semantic: &SemanticModel, call: &ast::ExprCall) -> bool {
| "tokenize" | "tokenize"
| "wave", | "wave",
"open" "open"
] | ["dbm", "gnu" | "ndbm" | "dumb", "open"] ] | ["dbm", "gnu" | "ndbm" | "dumb" | "sqlite3", "open"]
| ["fileinput", "FileInput" | "input"] | ["fileinput", "FileInput" | "input"]
| ["io", "open" | "open_code"] | ["io", "open" | "open_code"]
| ["lzma", "LZMAFile" | "open"] | ["lzma", "LZMAFile" | "open"]

View file

@ -324,3 +324,11 @@ SIM115.py:244:9: SIM115 Use a context manager for opening files
| ^^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^^^ SIM115
245 | f.close() 245 | f.close()
| |
SIM115.py:257:5: SIM115 Use a context manager for opening files
|
256 | # SIM115
257 | f = dbm.sqlite3.open("foo.db")
| ^^^^^^^^^^^^^^^^ SIM115
258 | f.close()
|