Skip to content

Commit

Permalink
Allow open without context manager in return statement (#14066)
Browse files Browse the repository at this point in the history
## Summary

Closes #13862.
  • Loading branch information
charliermarsh authored Nov 3, 2024
1 parent 3ca2478 commit 71a122f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,11 @@ def aliased():
# SIM115
f = dbm.sqlite3.open("foo.db")
f.close()

# OK
def func(filepath, encoding):
return open(filepath, mode="rt", encoding=encoding)

# OK
def func(filepath, encoding):
return f(open(filepath, mode="rt", encoding=encoding))
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ pub(crate) fn open_file_with_context_handler(checker: &mut Checker, call: &ast::
return;
}

// Ex) `return open("foo.txt")`
if semantic.current_statement().is_return_stmt() {
return;
}

// Ex) `with contextlib.ExitStack() as exit_stack: ...`
if match_exit_stack(semantic) {
return;
Expand Down

0 comments on commit 71a122f

Please sign in to comment.