-
Notifications
You must be signed in to change notification settings - Fork 536
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
Format code with Yapf #997
Conversation
And of course I'll fix flake8 which I just completely blew up :D |
archinstall/lib/configuration.py
Outdated
user_configuration_json = json.dumps( | ||
{ | ||
'config_version': | ||
storage['__version__'], # Tells us what version was used to generate the config |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the new line and indent here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be updated now with a new re-formatting
archinstall/lib/configuration.py
Outdated
if save: | ||
dest_path = pathlib.Path(storage.get('LOG_PATH', '.')) | ||
if (not dest_path.exists()) or not (dest_path.is_dir()): | ||
log( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is silly to split into 3 lines because of word wrapping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you mean the arguments in the log function, I believe it tries to separate them into a new line for readability.
There might be a setting for this, but it will most likely affect other places that will get less readable by it
archinstall/lib/hardware.py
Outdated
"intel-media-driver", | ||
"vulkan-intel", | ||
], | ||
"Nvidia (open-source)": ["mesa", "xf86-video-nouveau", "libva-mesa-driver"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Odd that it put these all on one line for no good reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you referring to the entries in the list?
There's multiple settings for this to define how the entries should be split up, so this can be tweaked
@Torxed any thoughts, feedback, concerns on this one :) |
This introduces spaces everywhere. And I'm pretty firm on that stance sadly. |
@Torxed that's a very easy fix, there's a setting for this in yapf :) |
Humm, I might like yapf if it's this configurable :) I'll have a look in a bit. |
@Torxed I've updated everything with tabs now :) The configuration is set in the |
I'll have a look on this one. I'll look it over pretty slowly because I'd like to see all aspects of changes that yapf does in order to understand what I'm committing to. So far I like how flexible it is and some of the changes I've touched, I think there was really only one change that I made manually to override it that stood out. |
Overall, I like what Yapf is doing. I've tried to be as pragmatic as possible and ignore some of my personal preferences.
One thing I was impressed by, was this change: The only thing I'd like to say, is that it took some time wrapping my head around why it did what it did. Found another one where it did some voodoo, and it comes up with it's own variable names? # It was:
new_uuid_set = (previous_partition_uuids ^ {partition.uuid for partition in self.blockdevice.partitions.values()})
# Became:
uuids = {partition.uuid for partition in self.blockdevice.partitions.values()}
new_uuid_set = (previous_partition_uuids ^ uuids) I hope it know to check for Interesting that it didn't like: while SysCommand(f"/usr/bin/umount -R {temporary_mountpoint}").exit_code != 0 and (iterations := iterations + 1) < 10: But instead prefered to do: while SysCommand(f"/usr/bin/umount -R {temporary_mountpoint}").exit_code != 0 and iterations < 10:
iterations += 1 I guess it's more readable, but I wonder if that's because people are just not used to the walrus operator yet or because it genuinely will never be readable hehe. |
@Torxed thanks for the great review of this one!
This can be set with the
The reason why this isn't consistent is that Yapf is merely reformatting lines that are too long and don't fit into the defined column space. I don't think there's a way to force it to everything regardless of length. Because in that case we'd have things like these
I've added a new setting to indent dictionary values which will end up with
The reason the values are put on a new line is because the comments are also considered as part of the code and it's trying to fit everything into the given column space.
I don't think this is doable. I understand your reasoning but I personally have never seen a project using
I don't think this can be tweaked in that detail unfortunately :(
I totally agree on this one, there will most definitely by weird cases that have to be rewritten manually
This has been formatted like this because there are surounding parenthesis. Normally on a
This is a very simple fix but just removing the
In this case there's no way to tell yapf about this, this is internal logic and it would be quite magical of a formatter to determine based on the logic what format to use :)
haha this was actually me doing some manual refactoring in this case. I think this is rather an illustration of how to refactor code to get better formatting results ;) So overrall Yapf is configurable to align more with personal preferences but it does have it's limitations. It cannot format dictionaries or lists individually but based on a rule set that applies to all of them equally. Which means that it will end up doing some things that one might not like as much. From that I have to say though that if a formatting turns out to be bad, then it's usually quite easy to refactor the code itself to gain simplicity and also the formatting will then work. I have pushed now an update with some setting tweaks, if you get some time have another look and see if it's getting better or worse now :) I like formatters because they unify the code and give it a global set of rules that is systematically applied. Depending on code complexity this will of course not always work perfectly, especially in a language that operates with spaces/tabs as part of it's language :D |
FYI just ignore the conflicts for now, if we should find a compromise on this and will actually introduce the formatter into the code then i'll start from a new master anyways :) |
I'm leaning towards maybe disabling the line-length as that appears to be the root of many "oddities". Thank you for taking the time to do this and to respond to my rants. |
I've asked for a "knob" (I think they call it) to configure type hints colon position. It's such a small thing but I would love for it to happen. Tracking it here: google/yapf#1003 I've also started experimenting with yapf in a smaller project of mine, to see what tweaks we can do to the configuration. |
Added a second upstream suggestion in order to be able to affect all sublevels of dictionaries the same way the top level is formatted. Tracking it here: google/yapf#1004 That one is a blocker for me as it makes dictionaries harder to read. Starting to like Yapf. |
@Torxed thanks for having another look ❤️ |
Pleasure is all mine! |
@Torxed I'll close this for now, maybe we can come back in the future to it :) |
I raised this as part of #872.
I'd like to start this as a discussion point here and to showcase Yapf.
I would like to make it clear that I'm not trying to enforce anything here but rather suggest something :)
Looking at the current code base there's quite a few code styles flying around, which is nothing bad per se, but it might be useful to have a more unified formatting at least. I generally don't like to have a very strict formatting applied (I"m looking at you Black but rather configurable rules that allows you to tweak things to ones liking, which I believe Yapf provides.
Yapf is a formatter that allows you to configure A LOT of settings, please check out the README in the yapf repo which lists all(?) possible configuration options. There are pre-configured profiles that one can use and override certain things manually.
The configuration is done in the
pyproject.toml
file. I've included an example now that uses the pep8 profile as a base and then overrides some configurations.To run the formatting, install
and then
NOTE: These installations would only be for development so we're not adding any real dependencies to archinstall