forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Implement `write-whole-file` (`FURB103`), part of astral-sh#1348. This is largely a copy and paste of `read-whole-file` astral-sh#7682. ## Test Plan Text fixture added. --------- Co-authored-by: Dhruv Manilawala <[email protected]>
- Loading branch information
Showing
11 changed files
with
640 additions
and
189 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
crates/ruff_linter/resources/test/fixtures/refurb/FURB103.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
def foo(): | ||
... | ||
|
||
|
||
def bar(x): | ||
... | ||
|
||
|
||
# Errors. | ||
|
||
# FURB103 | ||
with open("file.txt", "w") as f: | ||
f.write("test") | ||
|
||
# FURB103 | ||
with open("file.txt", "wb") as f: | ||
f.write(foobar) | ||
|
||
# FURB103 | ||
with open("file.txt", mode="wb") as f: | ||
f.write(b"abc") | ||
|
||
# FURB103 | ||
with open("file.txt", "w", encoding="utf8") as f: | ||
f.write(foobar) | ||
|
||
# FURB103 | ||
with open("file.txt", "w", errors="ignore") as f: | ||
f.write(foobar) | ||
|
||
# FURB103 | ||
with open("file.txt", mode="w") as f: | ||
f.write(foobar) | ||
|
||
# FURB103 | ||
with open(foo(), "wb") as f: | ||
# The body of `with` is non-trivial, but the recommendation holds. | ||
bar("pre") | ||
f.write(bar()) | ||
bar("post") | ||
print("Done") | ||
|
||
# FURB103 | ||
with open("a.txt", "w") as a, open("b.txt", "wb") as b: | ||
a.write(x) | ||
b.write(y) | ||
|
||
# FURB103 | ||
with foo() as a, open("file.txt", "w") as b, foo() as c: | ||
# We have other things in here, multiple with items, but the user | ||
# writes a single time to file and that bit they can replace. | ||
bar(a) | ||
b.write(bar(bar(a + x))) | ||
bar(c) | ||
|
||
|
||
# FURB103 | ||
with open("file.txt", "w", newline="\r\n") as f: | ||
f.write(foobar) | ||
|
||
|
||
# Non-errors. | ||
|
||
with open("file.txt", errors="ignore", mode="wb") as f: | ||
# Path.write_bytes() does not support errors | ||
f.write(foobar) | ||
|
||
f2 = open("file2.txt", "w") | ||
with open("file.txt", "w") as f: | ||
f2.write(x) | ||
|
||
# mode is dynamic | ||
with open("file.txt", foo()) as f: | ||
f.write(x) | ||
|
||
# keyword mode is incorrect | ||
with open("file.txt", mode="a+") as f: | ||
f.write(x) | ||
|
||
# enables line buffering, not supported in write_text() | ||
with open("file.txt", buffering=1) as f: | ||
f.write(x) | ||
|
||
# dont mistake "newline" for "mode" | ||
with open("file.txt", newline="wb") as f: | ||
f.write(x) | ||
|
||
# I guess we can possibly also report this case, but the question | ||
# is why the user would put "w+" here in the first place. | ||
with open("file.txt", "w+") as f: | ||
f.write(x) | ||
|
||
# Even though we write the whole file, we do other things. | ||
with open("file.txt", "w") as f: | ||
f.write(x) | ||
f.seek(0) | ||
x += f.read(100) | ||
|
||
# This shouldn't error, since it could contain unsupported arguments, like `buffering`. | ||
with open(*filename, mode="w") as f: | ||
f.write(x) | ||
|
||
# This shouldn't error, since it could contain unsupported arguments, like `buffering`. | ||
with open(**kwargs) as f: | ||
f.write(x) | ||
|
||
# This shouldn't error, since it could contain unsupported arguments, like `buffering`. | ||
with open("file.txt", **kwargs) as f: | ||
f.write(x) | ||
|
||
# This shouldn't error, since it could contain unsupported arguments, like `buffering`. | ||
with open("file.txt", mode="w", **kwargs) as f: | ||
f.write(x) | ||
|
||
# This could error (but doesn't), since it can't contain unsupported arguments, like | ||
# `buffering`. | ||
with open(*filename, mode="w") as f: | ||
f.write(x) | ||
|
||
# This could error (but doesn't), since it can't contain unsupported arguments, like | ||
# `buffering`. | ||
with open(*filename, file="file.txt", mode="w") as f: | ||
f.write(x) | ||
|
||
# Loops imply multiple writes | ||
with open("file.txt", "w") as f: | ||
while x < 0: | ||
f.write(foobar) | ||
|
||
with open("file.txt", "w") as f: | ||
for line in text: | ||
f.write(line) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.