Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid D403 if first char cannot be uppercased #4283

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/ruff/resources/test/fixtures/pydocstyle/D403.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ def another_function():

def utf8_function():
"""éste docstring is capitalized."""

def uppercase_char_not_possible():
"""'args' is not capitalized."""
9 changes: 9 additions & 0 deletions crates/ruff/src/rules/pydocstyle/rules/capitalized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ pub fn capitalized(checker: &mut Checker, docstring: &Docstring) {
if first_char.is_uppercase() {
return;
};
if first_char
.to_uppercase()
.next()
Copy link
Member

Choose a reason for hiding this comment

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

Do we know why to_uppercase returns an iterator? When would it have 0, or more than 1 element?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because Unicode.

The problem is that there's absolutely no guarantee that converting the case of a single code point will result in a single code point. For example, "ß" uppercases to "SS".

We avoid checking further if it's not an ASCII alphabet whose code is just a few lines above this, but I think you already updated the code :)

.map_or(false, |uppercase_first_char| {
uppercase_first_char == first_char
})
{
return;
}
Comment on lines +62 to +70
Copy link
Member

@MichaReiser MichaReiser May 8, 2023

Choose a reason for hiding this comment

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

Nit: I wonder if we should change the check above to !first_char.is_lowercase() (maybe add a comment why we aren't using is_uppercase. I might be wrong but I would expect that every char that is lowercase can be converted to uppercase.

Edit:
I think your fix is safer and easier to understand.


let capitalized_word = first_char.to_uppercase().to_string() + first_word_chars.as_str();

Expand Down