[Done] Add Black Code Formatter

I have added the file pyproject.toml to the root folder, I have copied the default configuration from the docs and modified two settings (I like single quotes and longer lines).

Then I have executed ruff format, and the code was formatted as expected.

Then I committed, pushed, refreshed the IDE, pressed Alt+Shift+F, and the code was formatted back as it was earlier (with double quotes and shorter lines).

Am I missing something?

image

Yeah, using a ruff.toml I am having the same experience as @stefano.menci , again I am using the exact toml from the docs found here

with the max line length changed to way longer than the default of 88.

Its still truncating 93 length lines.

Edit: I tried turning off targeted versions and requested linters like here:
image

I also completely deleted all site data for anvil.works and re-logged and everything, and still no application of the .toml file in the root dir.

Looks like the ruff.toml has a bug but .ruff.toml and pyproject.toml should work. So if they’re not I can take a look (I’m away right now but expect to be able to look at this early next week).

Edit: yes it looks like there’s a bug. I’ll let you know when that gets fixed. (Things load in a slightly different order locally and it looks like the first config is winning)

2 Likes

the ruff.toml / pyproject.toml issue should now be fixed

1 Like

I opened the app I had added pyproject.toml yesterday, pressed Alt+Shift+F and the formatting used the default options, not mine.

EDIT
Just in case it wasn’t clear, it still doesn’t work :slight_smile:

1 Like

should now be fixed fixed
(the toml parser was misbehaving in production)

2 Likes

As far a I can tell, it works perfectly now.

This is where I always had the problem, @owen.campbell 's tip about windows now having openssh available lead me to these two links that helped me just install everything using an administrator powershell.
When you create the private/public key file, you will want to be running powershell as your windows user, not administrator though.

  1. Get started with OpenSSH for Windows | Microsoft Learn

  2. How to SSH into GitHub on Windows example

1 Like

as a nice feature you can add to your configs
i’d recommend turning on import sorting
which you can do by adding "I001" to your config

[tool.ruff.lint]
select = ["E", "F", "I001"]

[tool.ruff.lint.isort]
known-third-party = ["anvil"]

At first you’ll get warnings about the order of your imports
but these should be fixable
Click the fix imports button from the linter
and hello beautifully sorted imports

4 Likes

These days, I categorically sort my imports by source:

  1. Anvil
  2. Python standard library
  3. Third-party packages
  4. Client code
  5. Server code

(Some of my older modules don’t do that, but they should.) So that would be the first key to sort by. It seems unlikely that Ruff would sort like that.

Edit: Then again, you do have

Is that what this is for?

1 Like

It works now!

I like dictionaries to be aligned by value, but it looks like ruff (and black) don’t have this option.

This is how I do it in PyCharm:
image

This is how I will do it in Anvil from now on (manually align the values, then disable the auto-formatting for that dictionary):

d1 = {
    'a':   1,
    'abc': 2,
}  # fmt: skip
d2 = {
    'a': 1,
    'abc': 2,
}

yes - known-third-party will put anvil imports in the third party imports
so it’s own section in the sorted imports

and if you want to change the order you can do that with section-order
https://docs.astral.sh/ruff/settings/#lint_isort_section-order

1 Like

You might want to track this issue:

1 Like

I’ve started doing the same with my fromimport statements, i.e., lining up the import clauses.

Thanks for the suggestion.

I thumbed it up in github (but I’m not keeping my fingers crossed, after reading the reactions to the same request for black).

I have tried adding these options. They do not seem to affect the formatter, and that could be because I’m testing it with a very simple app, but I see an unexpected result. Well, unexpected to me… is this what is supposed to happen?

  • The auto-formatter doesn’t sort the imports
  • The linter shows a warning and sorts them

This:
image

Is organized into this:
image

yes - ruff doesn’t provide formatting for import sorting only lint warnings and fixes

see discussion:

1 Like

Just applied my own ruff.toml and it works great! I was especially happy to see the anvil ide start applying 4 space indent with Alt+Shift+F!

2 Likes

What’s the Mac shortcut for auto formatting?

For Mac it’s: Option + shift + f

1 Like