Skip to content

Conversation

@11happy
Copy link
Contributor

@11happy 11happy commented Oct 26, 2025

Summary

This PR fixes #18409

Test Plan

I have added tests in FURB103.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 26, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+15 -0 violations, +0 -0 fixes in 4 projects; 51 projects unchanged)

PlasmaPy/PlasmaPy (+5 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview

+ .github/scripts/authors_in_cff.py:40:10: FURB101 `open` and `read` should be replaced by `Path(pathlib.Path("CITATION.cff")).read_text()`
+ .github/scripts/citation_updater.py:69:10: FURB103 [*] `Path.open()` followed by `write()` can be replaced by `citation_rst_file.write_text(citation_rst_text)`
+ docs/_author_list_from_cff.py:193:10: FURB103 [*] `Path.open()` followed by `write()` can be replaced by `pathlib.Path(rst_file).write_text(authors_rst, encoding="utf-8")`
+ docs/_global_substitutions.py:217:10: FURB103 [*] `Path.open()` followed by `write()` can be replaced by `pathlib.Path(rst_file).write_text(content, encoding="utf-8")`
+ tests/utils/data/test_downloader.py:191:10: FURB103 [*] `Path.open()` followed by `write()` can be replaced by `filepath.write_text("Not data")`

langchain-ai/langchain (+3 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview

+ libs/partners/chroma/langchain_chroma/vectorstores.py:489:14: FURB101 `open` and `read` should be replaced by `Path(Path(uri)).read_bytes()`
+ libs/standard-tests/langchain_tests/conftest.py:69:14: FURB101 `open` and `read` should be replaced by `Path(cassette_path).read_bytes()`
+ libs/standard-tests/langchain_tests/conftest.py:87:14: FURB103 [*] `Path.open()` followed by `write()` can be replaced by `cassette_path.write_bytes(data)`

scikit-build/scikit-build-core (+6 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview

+ src/scikit_build_core/ast/ast.py:88:10: FURB101 `open` and `read` should be replaced by `Path(Path(sys.argv[1])).read_text(encoding="utf-8-sig")`
+ src/scikit_build_core/ast/tokenizer.py:77:10: FURB101 `open` and `read` should be replaced by `Path(Path(sys.argv[1])).read_text(encoding="utf-8-sig")`
+ src/scikit_build_core/metadata/regex.py:58:10: FURB101 `open` and `read` should be replaced by `Path(Path(input_filename)).read_text(encoding="utf-8")`
+ tests/test_dynamic_metadata.py:274:10: FURB103 [*] `Path.open()` followed by `write()` can be replaced by `Path("__init__.py").write_text("__version__ = '0.1.0'")`
+ tests/test_dynamic_metadata.py:290:10: FURB103 [*] `Path.open()` followed by `write()` can be replaced by `Path("version.hpp")....`
+ tests/test_dynamic_metadata.py:326:10: FURB103 [*] `Path.open()` followed by `write()` can be replaced by `Path("version.hpp")....`

indico/indico (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview

+ indico/vendor/django_mail/message.py:369:14: FURB101 `open` and `read` should be replaced by `Path(path).read_bytes()`

Changes by rule (2 rules affected)

code total + violation - violation + fix - fix
FURB103 8 8 0 0 0
FURB101 7 7 0 0 0

@11happy
Copy link
Contributor Author

11happy commented Oct 29, 2025

@MichaReiser would appreciate your feedback on this whenever you get some time.

Thank you : )

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

I think we need to be a bit more careful with how we identify existing Path instances and hopefully we can deduplicate some code too.

@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features labels Oct 30, 2025
@amyreese
Copy link
Contributor

It might be helpful to reuse is_open_call_from_pathlib from ASYNC230:

fn is_open_call_from_pathlib(func: &Expr, semantic: &SemanticModel) -> bool {

@amyreese
Copy link
Contributor

Alternately PathlibPathChecker which can also match annotations:

impl TypeChecker for PathlibPathChecker {

@11happy
Copy link
Contributor Author

11happy commented Nov 2, 2025

the common helper has too many arguments causing the clippy error, should I break it down ?

@ntBre
Copy link
Contributor

ntBre commented Nov 3, 2025

the common helper has too many arguments causing the clippy error, should I break it down ?

I think it's okay just to add #[expect(clippy::too_many_arguments)] on the function.

}
let attr = func.as_attribute_expr()?;
let path_call = attr.value.as_call_expr()?;
let filename = path_call.arguments.args.first()?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we missing some of the argument validation code from find_file_open, especially these lines:

    if args.iter().any(Expr::is_starred_expr)
        || keywords.iter().any(|keyword| keyword.arg.is_none())
    {
        return None;
    }

I also think we're doing too much inspection on the attr.value. For example, I think is_open_call_from_pathlib should be able to identify a case like this:

p = Path("file.txt")
with p.open("r") as f:
    f.write("test")

but the current implementation expects a literal Path() call, not a name that resolves to a Path.

We might need to make the filename field optional on the FileOpen struct.

Copy link
Contributor Author

@11happy 11happy Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done , also the message is formatted accordingly but i feel its getting nested & dosent feel good , not sure although

Copy link
Contributor Author

@11happy 11happy Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also I have not added any comments yet , should i add similar comments like find_file_open

@MichaReiser
Copy link
Member

Would you mind rebasing your PR? Doing so will allow us to merge your PR without delayed once reviewed.

@11happy
Copy link
Contributor Author

11happy commented Nov 10, 2025

done rebased it & resolved merge conflicts : )
Thank you

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for rebasing and for your work on this! I pushed a commit fixing a few nits but then noticed a few more minor issues. The implementation feels a bit complicated to me now, I want to keep thinking about it.

@11happy
Copy link
Contributor Author

11happy commented Nov 12, 2025

Thanks for rebasing and for your work on this! I pushed a commit fixing a few nits but then noticed a few more minor issues. The implementation feels a bit complicated to me now, I want to keep thinking about it.

I agree with you implementation is complex ,felt the same, would appreciate any pointers to decomplicate/refactor it ?
Thank you : )

@ntBre
Copy link
Contributor

ntBre commented Nov 19, 2025

I just pushed a commit trying out an OpenArgument enum since the path_obj field and filename field were basically mutually exclusive. I think this makes things a little simpler, but I'm curious to get your thoughts on it too. I think the only remaining thing I don't love is the is_path_open check on the Violation struct. Maybe we could use the whole suggestion we generate for the fix in the error message?

I also fixed some new merge conflicts while I was here.

I also noticed that we haven't updated the code for FURB102, even though it's quite similar to FURB103. Do you want to do that in a follow-up PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FURB] Path write

4 participants