Skip to content

Auto-format Python sources with black, fix all issues reported by flake8#1010

Merged
nucleogenic merged 6 commits intodevelopfrom
nucleogenic-python-black-autoformat
Nov 30, 2022
Merged

Auto-format Python sources with black, fix all issues reported by flake8#1010
nucleogenic merged 6 commits intodevelopfrom
nucleogenic-python-black-autoformat

Conversation

@nucleogenic
Copy link
Copy Markdown
Member

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 the python directory.

Checks in GitHub Actions will be updated in PR #1009.

@nucleogenic nucleogenic force-pushed the nucleogenic-python-black-autoformat branch from 3cf8844 to feccfb4 Compare November 29, 2022 23:07
Copy link
Copy Markdown
Member

@rdmark rdmark left a comment

Choose a reason for hiding this comment

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

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])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I find it really hard to read the code like this. Any chance we can tweak this rule (if there is one)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Comment on lines +116 to +130
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})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nitpick: I don't like how each method call uses different formatting. Would be easier to read if they were all consistent.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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},
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Comment on lines +12 to +42
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"),
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Another case where the formatting is getting a bit messy.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

help="Run in development mode"
)
)
parser.add_argument("--dev-mode", action="store_true", help="Run in development mode")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wish the formatter hadn't removed the indentation here either.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sorted with trailing comma.

@nucleogenic
Copy link
Copy Markdown
Member Author

@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.

@rdmark
Copy link
Copy Markdown
Member

rdmark commented Nov 30, 2022

@nucleogenic Please go ahead with merging this at any time. I don't think further code review is required.

@nucleogenic nucleogenic merged commit 315ef9f into develop Nov 30, 2022
@nucleogenic nucleogenic deleted the nucleogenic-python-black-autoformat branch November 30, 2022 05:19
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.

2 participants