Skip to content

Conversation

@chirizxc
Copy link
Contributor

Summary

Fixes #20134

Test Plan

cargo nextest run flake8_use_pathlib

@github-actions
Copy link
Contributor

github-actions bot commented Aug 28, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@chirizxc chirizxc changed the title [flake8-use-pathlib] Fix PTH101, PTH104, PTH105, PTH121 fixes [flake8-use-pathlib] Fix PTH101, PTH104, PTH105, PTH121 fixes Aug 28, 2025
@chirizxc
Copy link
Contributor Author

I don't know how to add a co-author to a PR, maybe only maintainers can do that?

@chirizxc
Copy link
Contributor Author

chirizxc commented Sep 2, 2025

i use this cases:

import os
from pathlib import Path

# os.chmod(mode=0o600, follow_symlinks=True, path="pth1_link")
os.chmod(mode=0o600, follow_symlinks=True, path="pth1_link")
Path("pth1_link").chmod(mode=0o600)

# os.chmod("pth1_link", mode=0o600, follow_symlinks=      False                    )
os.chmod("pth1_link", mode=0o600, follow_symlinks=      False                    )
Path("pth1_link").chmod(mode=0o600, follow_symlinks=False)

# os.chmod(path="pth1_link", mode=0o600, follow_symlinks=True)
os.chmod(path="pth1_link", mode=0o600, follow_symlinks=True)
Path("pth1_link").chmod(mode=0o600)

# os.chmod("pth1_link", follow_symlinks=True, mode=0o600)
os.chmod("pth1_link", follow_symlinks=True, mode=0o600)
Path("pth1_link").chmod(mode=0o600)

# os.chmod("pth1_link", follow_symlinks=False, mode=0o600, dir_fd=None)
os.chmod("pth1_link", follow_symlinks=False, mode=0o600, dir_fd=None)
Path("pth1_link").chmod(follow_symlinks=False, mode=0o600)

# os.chmod("pth1_link", 0o600)
os.chmod("pth1_link", 0o600)
Path("pth1_link").chmod(0o600)

# os.chmod("pth1_link", 0o600, dir_fd=None)
os.chmod("pth1_link", 0o600, dir_fd=None)
Path("pth1_link").chmod(0o600)

# os.chmod("pth1_link", 0o600, follow_symlinks=False)
os.chmod("pth1_link", 0o600, follow_symlinks=False)
Path("pth1_link").chmod(0o600, follow_symlinks=False)

# os.chmod("pth1_link", 0o600, dir_fd=None, follow_symlinks=False)
os.chmod("pth1_link", 0o600, dir_fd=None, follow_symlinks=False)
Path("pth1_link").chmod(0o600, follow_symlinks=False)

# Only diagnostic
os.chmod("pth1_link", 0o600, follow_symlinks="False")
os.chmod("pth1_file", 0o700, None, True, 1, *[1], **{"x": 1}, foo=1)
os.chmod()
os.chmod("pth1_link", follow_symlinks=True)
os.chmod("pth1_link", dir_fd=None)

@ntBre
Copy link
Contributor

ntBre commented Sep 3, 2025

I don't know how to add a co-author to a PR, maybe only maintainers can do that?

You can add a co-author in git with a special line like this in a commit message:

Co-authored-by: NAME <[email protected]>

But I can also add that when merging, no worries!

I'll try to give this a review soon.

@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations preview Related to preview mode features labels Sep 3, 2025
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! Just a few comments/suggestions

})
}

pub(crate) fn is_optional_bool_literal(args: &Arguments, name: &str, pos: usize) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like we're often calling this either before or after a separate call to args.find_argument_value(name, pos). Would it make sense just to pass in the Expr from that? Alternatively, maybe this could return Some(&Expr) or Some(&ExprBooleanLiteral) instead of a bool, just to avoid calling find_argument_value multiple times.

I don't think it's too big of a deal, just an idea.

@chirizxc
Copy link
Contributor Author

I think this good now

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, this is looking good! Two more small things (and a conflict from the other PTH PR, it looks like) and then I think this is good to go.

Comment on lines 131 to 134
Some("mode") => Some(format!("mode={}", locator.slice(&kw.value))),
Some("follow_symlinks") => {
Some(format!("follow_symlinks={}", locator.slice(&kw.value)))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we use the trick like in the other PR where we slice out the whole kw?

Suggested change
Some("mode") => Some(format!("mode={}", locator.slice(&kw.value))),
Some("follow_symlinks") => {
Some(format!("follow_symlinks={}", locator.slice(&kw.value)))
}
Some("mode" | "follow_symlinks") => locator.slice(kw),

then we can avoid the to_string call for positional args too.

Copy link
Contributor Author

@chirizxc chirizxc Sep 17, 2025

Choose a reason for hiding this comment

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

error[E0308]: `match` arms have incompatible types                                                                                                   
   --> crates\ruff_linter\src\rules\flake8_use_pathlib\rules\os_chmod.rs:132:22
    |
130 |               ArgOrKeyword::Keyword(kw) => match kw.arg.as_deref() {
    |  __________________________________________-
131 | |                 Some("mode" | "follow_symlinks") => locator.slice(kw),
    | |                                                     ----------------- this is found to be of type `&str`
132 | |                 _ => None,
    | |                      ^^^^ expected `&str`, found `Option<_>`
133 | |             },
    | |_____________- `match` arms have incompatible types
    |
    = note: expected reference `&str`
                    found enum `std::option::Option<_>

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh oops, I forgot to wrap it in Some, sorry! I think we can still remove to_string as long as we remove it from the ArgOrKeyword::Arg arm too.

@chirizxc
Copy link
Contributor Author

I couldn't find another e-mail on github so I used <[email protected]>

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.

Thank you! I just pushed one commit adding back the PTH211 test case. It seemed fine to keep it and a bit unrelated to the other changes here, from what I could tell.

@ntBre ntBre enabled auto-merge (squash) September 18, 2025 14:14
@ntBre ntBre merged commit 144373f into astral-sh:main Sep 18, 2025
34 checks passed
@chirizxc chirizxc deleted the pth's-fixes branch September 18, 2025 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fixes Related to suggested fixes for violations preview Related to preview mode features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PTH101, PTH104, PTH105, and PTH121 ignore extra arguments

2 participants