Skip to content

Commit

Permalink
[flake8-simplify] - extend open-file-with-context-handler to work w…
Browse files Browse the repository at this point in the history
…ith 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`
  • Loading branch information
diceroll123 authored Aug 26, 2024
1 parent 5af4833 commit 0b5828a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,16 @@ def aliased():
from tarfile import TarFile as TF
f = TF("foo").open()
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()
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fn is_open_preview(semantic: &SemanticModel, call: &ast::ExprCall) -> bool {
| "tokenize"
| "wave",
"open"
] | ["dbm", "gnu" | "ndbm" | "dumb", "open"]
] | ["dbm", "gnu" | "ndbm" | "dumb" | "sqlite3", "open"]
| ["fileinput", "FileInput" | "input"]
| ["io", "open" | "open_code"]
| ["lzma", "LZMAFile" | "open"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,11 @@ SIM115.py:244:9: SIM115 Use a context manager for opening files
| ^^^^^^^^^^^^^^ SIM115
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()
|

0 comments on commit 0b5828a

Please sign in to comment.