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

Use Black for Formatting #94

Closed
wants to merge 2 commits into from
Closed

Use Black for Formatting #94

wants to merge 2 commits into from

Conversation

meawoppl
Copy link
Contributor

@meawoppl meawoppl commented May 5, 2022

A quick demo of how to make black auto-format everything. This also shifts format checking to gh infrastructure.

All I did to generate a bulk of the changes in this PR was:
black picamera2 which in turn output the reformatted code:
image

This has the side benefit of killing a lot of diff noise between PR's

Signed-off-by: Matthew Goodman matt@exclosure.io

@meawoppl
Copy link
Contributor Author

meawoppl commented May 5, 2022

Note that style check now takes about... 4 seconds:
image

@davidplowman
Copy link
Collaborator

Hi, thanks for posting this it looks interesting. Can you say how I could enforce this kind of style during development, for example by using the git pre-commit hook? Also, have you changed the style checking process on the test machine to complain if people don't stick to this style or not, I didn't quite understand that.

@meawoppl
Copy link
Contributor Author

meawoppl commented May 5, 2022

There are a number of options for this:

  • Before commiting run black picamera2 from the repository root
  • Install black and add black picamera2 to the .git/hooks/pre-commit
  • Install and use the pre-commit framework (which is what I do in my projects), which will manage installation of many potential pre-commit options (including black). It's nice because it comes with a raft of options that you can scale in as the code becomes more mature. Starting with formatting is easy, then adding lint, doc requirements, type-checking etc all just becomes a couple lines of config

Here is what that looks like in a project I am working on:
precommit-demo

@meawoppl
Copy link
Contributor Author

meawoppl commented May 5, 2022

Also, this PR is likely to be conflicted by basically any other change, so let me know when/if you want it and I can freshen it :)

@meawoppl
Copy link
Contributor Author

meawoppl commented May 5, 2022

And to answer your earlier question. This line will cause the CI to reject PR's that are not consistant with the formatting requirements.

@meawoppl meawoppl changed the title Formatting/checking based on black Use Black for Formatting May 5, 2022
@meawoppl
Copy link
Contributor Author

meawoppl commented May 5, 2022

As a totally incidental side benefit, this takes the style check responsibility off the pi you are using to run tests :)

@naushir
Copy link
Collaborator

naushir commented May 6, 2022

This change seems reasonable, but there are a couple of things that I can see we need to address:

  • The github action command probably wants to be black --check --diff picamera2 to show what formatting changes would be needed.
  • There does not seem to be a way to run black on a diff/patch as far as I can tell. This is not ideal, since if we chose to ignore a particular style dictated by the tool for a single PR, the tool will end up complaining about it for all subsequent PRs, which defeats the purpose of the pre-commit check.

@meawoppl
Copy link
Contributor Author

Hmmm. The flags are easy to switch out. Let me start there.

Following up on the second bullet point. Looking through the checker it seems that it completely checks all files/lines, then filters to lines contained in the diff only. I am more than a little confused as to how that can deal with multi-line expressions in a sane way, but that isn't the question here. black is named for the Henry Ford quote, its kinda hostile by design.
image

It might just not meet the needs of this group if y'all want to have exceptionally formatted sections.

FWIW, the not having redo PR's for code style issues is about 50% of the appeal, another 40% is not having to educate folks that contribute about the particular flavor of code style. 🤷‍♂️

Signed-off-by: Matthew Goodman <matt@exclosure.io>
@naushir
Copy link
Collaborator

naushir commented May 12, 2022

Apparently running on a diff has been a feature request for some time now (psf/black#830, psf/black#142, psf/black#245, psf/black#370, psf/black#511). Looks unlikely that this will be put into mainline.

There are a few wrapper-of-sorts that does seem to do this - https://github.com/akaihola/darker and https://github.com/wbolster/black-macchiato. Perhaps it's worth looking to see if those could be used instead? In particular, darker looks very promising and might be pretty much a drop-in replacement for the existing script.

Copy link
Collaborator

@naushir naushir left a comment

Choose a reason for hiding this comment

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

Here are some comments on the formatting produced by Black. Of course, these preferences are entirely personal to my tastes.

)
YUV2RGB_REC709 = np.array(
[[1.164, 1.164, 1.164], [0.0, -0.213, 2.112], [1.793, -0.533, 0.0]]
)

Copy link
Collaborator

@naushir naushir May 12, 2022

Choose a reason for hiding this comment

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

I think I prefer the original formatting. Or possibly all 3 matrices to look something like:

YUV2RGB_REC709 = np.array(
    [ [1.164,  1.164, 1.164],
      [0.0,   -0.213, 2.112],
      [1.793, -0.533, 0.0] ]
)

return simplejpeg.encode_jpeg(array, quality=self.q, colorspace=self.colour_space)
return simplejpeg.encode_jpeg(
array, quality=self.q, colorspace=self.colour_space
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure how I feel about the formatting here and in all the other places where the function arguments are put on the next line. The original reads easier to me.

self.lock = threading.Lock() # protects the functions and completed_requests fields
self.lock = (
threading.Lock()
) # protects the functions and completed_requests fields
self.have_event_loop = False
Copy link
Collaborator

@naushir naushir May 12, 2022

Choose a reason for hiding this comment

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

The original looks better and more concise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it was just trying to maintain the comment on the same line here...

if not isinstance(camera_config["colour_space"], libcamera._libcamera.ColorSpace):
if not isinstance(
camera_config["colour_space"], libcamera._libcamera.ColorSpace
):
raise RuntimeError("Colour space has incorrect type")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Prefer the original.

"YUYV",
"UYVY",
"VYUY",
)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the original single line version looks better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Concur.

pointer,
CFUNCTYPE,
c_bool,
)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the original single line version looks better.

EGL_ALPHA_SIZE,
0,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT,
EGL_NONE,
]
Copy link
Collaborator

Choose a reason for hiding this comment

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

The original looks better as you can associate the key-value pairs on one line. Ditto for the instances below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, that is kinda and uglee tranform. Maybe these want to be a dict then tranformed at the call site?

@meawoppl
Copy link
Contributor Author

meawoppl commented May 12, 2022

Seems like this isn't a real improvement here.

@meawoppl meawoppl closed this May 12, 2022
@meawoppl meawoppl mentioned this pull request Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants