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

Adopting ruff formatter #7930

Merged
merged 17 commits into from Oct 26, 2023
Merged

Adopting ruff formatter #7930

merged 17 commits into from Oct 26, 2023

Conversation

Luca-Blight
Copy link
Contributor

@Luca-Blight Luca-Blight commented Oct 25, 2023

Change Summary

Ruff is now the sole linter 👍
Files Altered: ['Makefile', 'docs/contributing.md', 'pyproject.toml', 'pdm.lock' ]

Notes:

For pyproject.toml file, I removed the last element('E501') of line 180 given black's removal but I'm not totally sure what this line is for.

Related Issue Number

Fix #7914

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@Luca-Blight
Copy link
Contributor Author

I suppose I could run Ruff local to fix the linting issues...what do you think @sydney-runkle?

@Luca-Blight Luca-Blight changed the title Removal of Black WIP Removal of Black Oct 25, 2023
@sydney-runkle
Copy link
Member

Hi @Luca-Blight,

Looks good! Yeah, go ahead and run the linter locally to fix the failing checks. I'll take another look after you do that.

Thanks 🌟

Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

@Luca-Blight,

Thanks for running the linter locally. I suggested a couple of very minor changes 👍

pyproject.toml Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
Makefile Show resolved Hide resolved
Comment on lines -370 to 373
f"A non-annotated attribute was detected: `{var_name} = {value!r}`. All model fields require a "
f"type annotation; if `{var_name}` is not meant to be a field, you may be able to resolve this "
f'A non-annotated attribute was detected: `{var_name} = {value!r}`. All model fields require a '
f'type annotation; if `{var_name}` is not meant to be a field, you may be able to resolve this '
f"error by annotating it as a `ClassVar` or updating `model_config['ignored_types']`.",
code='model-field-missing-annotation',
Copy link
Member

Choose a reason for hiding this comment

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

This change also seems odd to me, as it's not changing the last line. I wonder what happens when we use:

[tool.ruff.flake8-quotes]
multiline-quotes = 'double'

In the pyproject.toml

Copy link
Member

Choose a reason for hiding this comment

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

Actually, it should be 'double' by default, so I don't think that'll change anything. This modification still strikes me as odd though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea good eye there. Let me try the change out and see what happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Btw, your diligence is impressive and I appreciate it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like

[tool.ruff.flake8-quotes]
multiline-quotes = 'double'

is already implemented on line 179:

[tool.ruff]
line-length = 120
extend-select = ['Q', 'RUF100', 'C90', 'UP', 'I', 'D', 'T']
extend-ignore = ['D105', 'D107', 'D205', 'D415']
flake8-quotes = {inline-quotes = 'single', multiline-quotes = 'double'}
mccabe = { max-complexity = 14 }
isort = { known-first-party = ['pydantic', 'tests'] }
target-version = "py37"
extend-exclude = ['pydantic/v1', 'tests/mypy/outputs']

Copy link
Member

Choose a reason for hiding this comment

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

Good catch. I'll leave this as unresolved for now given that I'm still curious about the change the ruff formatter made, but I don't think it's a problem in terms of the flake8-quotes configuration.

Choose a reason for hiding this comment

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

This strikes me as a bug -- we should be using double quotes for multi-line strings regardless of the formatter quote setting. Will look into it shortly and report back.

Choose a reason for hiding this comment

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

The last line doesn't get changed because changing the quote style would require escaping the '. See my other comment below.

Choose a reason for hiding this comment

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

Sorry, as usual, Micha is right -- this is an implicit concatenation, not a multiline string. So it's trying to use single quotes for each segment, and then skipping those that contain a single quote.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see. This happened a few times with implicit concatenations, and I thought the pattern was odd at first glance bc it always seemed to be skipping the last line, but that seems to always be where we have a single quote based on the nature of many of our strings. Thanks!

match="Private attributes must not use dunder names;" " use a single underscore prefix instead of '__foo__'.",
match='Private attributes must not use dunder names;' " use a single underscore prefix instead of '__foo__'.",
Copy link
Member

Choose a reason for hiding this comment

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

Clearly it's having a hard time with this as well 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea that is weird. I wonder what it's tripping on...

Choose a reason for hiding this comment

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

So, what's happening here is that it's trying to convert each segment to single quotes (as per quote-style = 'single'). It's succeeding for the first segment in this implicit concatenation, but it avoids changing the second segment, because that segment itself contains a single quote (and so that single quote would need to be escaped).

Arguably we should be making that decision across the entire implicit concatenation (i.e., looking across all segments) to avoid these mixed quotes. I'll file an issue and we'll discuss.

Choose a reason for hiding this comment

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

Ruff changes the quotes of the first string but not of the second because it would require escaping the '. Is it unexpected that Ruff uses different quotes for different parts of the implicit concatenated string? Would you prefer if it would change the quotes consistently for the entire implicit concatenated string?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, thanks for the explanation. Clearly I missed the pattern of escaping ' that was at the root of many of my questions.

I do feel like changing quotes consistently for an entire implicit concatenated string might look a bit cleaner, but I'm not adamant about the change. Excited to hear what others think.

@charliermarsh, thanks for filing an issue 👍

Copy link
Member

Choose a reason for hiding this comment

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

@charliermarsh could you link the issue here? I may have just missed it, but didn't see it at a first glance

Choose a reason for hiding this comment

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

Apologies -- just filed it here: astral-sh/ruff#8265

@sydney-runkle sydney-runkle changed the title Removal of Black Adopting ruff formatter Oct 26, 2023
@sydney-runkle
Copy link
Member

@Luca-Blight,

Thanks for your work on this 💯. Hopefully we can resolve the few odd formatting patterns that we're seeing tomorrow and get this across the line 😄.

@sydney-runkle
Copy link
Member

@charliermarsh @MichaReiser,

Thanks for your super prompt support. Looks like we'll have this across the line in no time 🚀 !

Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

LGTM 👍

The only thing I'm still seeing is the implicit concatenation changes that we discussed already, which I don't think we have to address in this PR, but can continue discussion with ruff and change this down the line.

@Luca-Blight, great work!

@Luca-Blight
Copy link
Contributor Author

LGTM 👍

The only thing I'm still seeing is the implicit concatenation changes that we discussed already, which I don't think we have to address in this PR, but can continue discussion with ruff and change this down the line.

@Luca-Blight, great work!

I noticed some letter swapping in some of the files such as rf -> fr and br -> rb.
There are several examples in the test files and color.py. Do you see that as an issue?

@sydney-runkle
Copy link
Member

I noticed some letter swapping in some of the files such as rf -> fr and br -> rb. There are several examples in the test files and color.py. Do you see that as an issue?

Yeah I saw that as well. I don't think it's an issue. @adriangb or @dmontagu, do you think otherwise?

@adriangb
Copy link
Member

I see it's changing rf'a string' to fr'a string'. I agree it doesn't matter.

@@ -284,7 +284,7 @@ class Model(BaseModel):
assert Model.model_validate_json('{"a": 2}', context={'c': 2}).model_dump() == {'a': 2}
# insert_assert(log)
assert log == [
'json enter input={"a": 2} kwargs={\'strict\': None, \'context\': {\'c\': 2}}',
"json enter input={\"a\": 2} kwargs={'strict': None, 'context': {'c': 2}}",
Copy link
Contributor

Choose a reason for hiding this comment

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

@charliermarsh if you are looking for feedback, I'd think that it would be preferable to use single quotes on the outer level when single quotes are preferred and the string contains both single and double quotes. But I definitely don't care enough to hold off on merging just for this issue 😄

Choose a reason for hiding this comment

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

Yeah I agree — this one struck me as odd. Gonna look into it.

Choose a reason for hiding this comment

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

@sydney-runkle sydney-runkle merged commit 419398d into pydantic:main Oct 26, 2023
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use ruff for formatting
6 participants