Auto-format Python sources with black, fix all issues reported by flake8#1010
Auto-format Python sources with black, fix all issues reported by flake8#1010nucleogenic merged 6 commits intodevelopfrom
Conversation
3cf8844 to
feccfb4
Compare
rdmark
left a comment
There was a problem hiding this comment.
The end result is a overwhelming net positive for readability and maintainability. I found a small minority of cases where black condensed nested code into one liners with decreased readability. I wonder if there is some kind of tweak to the rules we can do here?
| for file in files | ||
| ] | ||
| ) | ||
| files_list.extend([(file, path.getsize(path.join(file_path, file))) for file in files]) |
There was a problem hiding this comment.
I find it really hard to read the code like this. Any chance we can tweak this rule (if there is one)?
There was a problem hiding this comment.
From what I've read, untangling comprehensions based on a "complexity" heuristic is still a WIP in Black.
I was able to get it to:
files_list.extend(
[(file, path.getsize(path.join(file_path, file))) for file in files],
)
.... which is a small improvement.
Failing that, a simpler refactor might be possible.
There was a problem hiding this comment.
Fair point, this mess should better be refactored anyways. Do we want to add a TODO? Totally optional in this PR but just a thought so that it isn't forgotten.
| menu.add_entry("Return", {"context": self.ACTION_MENU, "action": self.ACTION_RETURN}) | ||
| menu.add_entry( | ||
| "Attach/Insert", | ||
| {"context": self.ACTION_MENU, "action": self.ACTION_SLOT_ATTACHINSERT}, | ||
| ) | ||
| menu.add_entry( | ||
| "Detach/Eject", | ||
| {"context": self.ACTION_MENU, "action": self.ACTION_SLOT_DETACHEJECT}, | ||
| ) | ||
| menu.add_entry("Info", {"context": self.ACTION_MENU, "action": self.ACTION_SLOT_INFO}) | ||
| menu.add_entry( | ||
| "Load Profile", | ||
| {"context": self.ACTION_MENU, "action": self.ACTION_LOADPROFILE}, | ||
| ) | ||
| menu.add_entry("Shutdown", {"context": self.ACTION_MENU, "action": self.ACTION_SHUTDOWN}) |
There was a problem hiding this comment.
Nitpick: I don't like how each method call uses different formatting. Would be easier to read if they were all consistent.
There was a problem hiding this comment.
Fixed by adding a trailing comma before the final ), e.g.
menu.add_entry(
"Return",
{"context": self.ACTION_MENU, "action": self.ACTION_RETURN},
)
menu.add_entry(
"Attach/Insert",
{"context": self.ACTION_MENU, "action": self.ACTION_SLOT_ATTACHINSERT},
)
There was a problem hiding this comment.
Further commentary on this approach is here:
https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#pragmatism
python/web/src/return_code_mapper.py
Outdated
| ReturnCodes.DELETEFILE_SUCCESS: _("File deleted: %(file_path)s"), | ||
| ReturnCodes.DELETEFILE_FILE_NOT_FOUND: _("File to delete not found: %(file_path)s"), | ||
| ReturnCodes.RENAMEFILE_SUCCESS: _("File moved to: %(target_path)s"), | ||
| ReturnCodes.RENAMEFILE_UNABLE_TO_MOVE: _("Unable to move file to: %(target_path)s"), | ||
| ReturnCodes.DOWNLOADFILETOISO_SUCCESS: _( | ||
| 'Created CD-ROM ISO image with arguments "%(value)s"' | ||
| ), | ||
| ReturnCodes.DOWNLOADTODIR_SUCCESS: _("%(file_name)s downloaded to %(save_dir)s"), | ||
| ReturnCodes.WRITEFILE_SUCCESS: _("File created: %(target_path)s"), | ||
| ReturnCodes.WRITEFILE_COULD_NOT_WRITE: _("Could not create file: %(target_path)s"), | ||
| ReturnCodes.READCONFIG_SUCCESS: _("Loaded configurations from: %(file_name)s"), | ||
| ReturnCodes.READCONFIG_COULD_NOT_READ: _( | ||
| "Could not read configuration file: %(file_name)s" | ||
| ), | ||
| ReturnCodes.READCONFIG_INVALID_CONFIG_FILE_FORMAT: _("Invalid configuration file format"), | ||
| ReturnCodes.READDRIVEPROPS_SUCCESS: _("Read properties from file: %(file_path)s"), | ||
| ReturnCodes.READDRIVEPROPS_COULD_NOT_READ: _( | ||
| "Could not read properties from file: %(file_path)s" | ||
| ), | ||
| ReturnCodes.ATTACHIMAGE_COULD_NOT_ATTACH: _( | ||
| "Cannot insert an image for %(device_type)s into a %(current_device_type)s device" | ||
| ), | ||
| ReturnCodes.EXTRACTIMAGE_SUCCESS: _("Extracted %(count)s file(s)"), | ||
| ReturnCodes.EXTRACTIMAGE_NO_FILES_SPECIFIED: _( | ||
| "Unable to extract archive: No files were specified" | ||
| ), | ||
| ReturnCodes.EXTRACTIMAGE_NO_FILES_EXTRACTED: _( | ||
| "No files were extracted (existing files are skipped)" | ||
| ), | ||
| ReturnCodes.EXTRACTIMAGE_COMMAND_ERROR: _("Unable to extract archive: %(error)s"), | ||
| } |
There was a problem hiding this comment.
Another case where the formatting is getting a bit messy.
There was a problem hiding this comment.
These lines are being selectively wrapped due to the line length limit.
I've wrapped the block with # fmt: off and # fmt: on to skip it; I'm not sure of a a better approach right now.
There was a problem hiding this comment.
This was a minor issue anyways. Good to know about the # fmt blocks. This might be useful in the future as well when we want to force a particular layout for readability reasons.
python/web/src/web.py
Outdated
| help="Run in development mode" | ||
| ) | ||
| ) | ||
| parser.add_argument("--dev-mode", action="store_true", help="Run in development mode") |
There was a problem hiding this comment.
I wish the formatter hadn't removed the indentation here either.
There was a problem hiding this comment.
Sorted with trailing comma.
|
@rdmark Thanks for the comments. In short, I agree! I think we can disable the auto-formatting in some of these examples where it's particularly egregious. I'll see what that looks like and push an update. |
1c71b00 to
d8be315
Compare
|
@nucleogenic Please go ahead with merging this at any time. I don't think further code review is required. |
Auto-formatted all Python sources with
black.Fixed all issues reported by
flake8.Due to the config files moving up a level (
web->python), these commands should now be run from thepythondirectory.Checks in GitHub Actions will be updated in PR #1009.