Code Quality changes #2980

Closed
Drapersniper wants to merge 34 commits from codehealth into V3/develop
Drapersniper commented 2019-09-04 22:36:30 +02:00 (Migrated from github.com)

Type

  • Bugfix
  • Enhancement
  • New feature

Description of the changes

It was easier making a new PR than reverting changes ...
This is a variant of #2978 but it doesn't have the PyCharm inspections hints, and it doesn't break l18n 👀
This one also catches a few typos and mispelling that the original did not.

This PR essentially organises imports alphabetically into 3 groups.

Built-ins
3rd Party
Package

It also removes unused variables, convert lamda assignment to def's to PEP8 stops complaining and some other minor tweaks

### Type - [x] Bugfix - [ ] Enhancement - [ ] New feature ### Description of the changes It was easier making a new PR than reverting changes ... This is a variant of #2978 but it doesn't have the PyCharm inspections hints, and it doesn't break l18n 👀 This one also catches a few typos and mispelling that the original did not. This PR essentially organises imports alphabetically into 3 groups. Built-ins 3rd Party Package It also removes unused variables, convert lamda assignment to def's to PEP8 stops complaining and some other minor tweaks
palmtree5 (Migrated from github.com) reviewed 2019-09-04 22:36:30 +02:00
tekulvw (Migrated from github.com) reviewed 2019-09-04 22:36:30 +02:00
Twentysix26 (Migrated from github.com) reviewed 2019-09-04 22:36:30 +02:00
Tobotimus (Migrated from github.com) reviewed 2019-09-04 22:36:30 +02:00
mikeshardmind commented 2019-09-04 22:57:50 +02:00 (Migrated from github.com)

For other people coming across some of this later, as some discussion has happened in discord.

We should avoid using _ as a placeholder var name anywhere in red's codebase due to interactions with gettext and i18n use.

It's fine to rename something to _loop_counter or something else which indicates to tools that the var name isn't important.

For other people coming across some of this later, as some discussion has happened in discord. We should avoid using `_` as a placeholder var name anywhere in red's codebase due to interactions with gettext and i18n use. It's fine to rename something to `_loop_counter` or something else which indicates to tools that the var name isn't important.
mikeshardmind (Migrated from github.com) requested changes 2019-09-05 00:06:20 +02:00
mikeshardmind (Migrated from github.com) left a comment

Some of these changes aren't ready to go.

I'm still mixed on accepting the import sorting without also making it a requirement going forward.
Doing this as is just increases the size of diffs every so often as we resort.

There's a lot of whitespace changes here which don't seem like positives.

Still seems like too much of this is being done to satisfy tools we haven't and probably won't standardize on.

Some of these changes aren't ready to go. I'm still mixed on accepting the import sorting without also making it a requirement going forward. Doing this as is just increases the size of diffs every so often as we resort. There's a lot of whitespace changes here which don't seem like positives. Still seems like too much of this is being done to satisfy tools we haven't and probably won't standardize on.
mikeshardmind (Migrated from github.com) commented 2019-09-05 00:01:19 +02:00

Why?

Why?
mikeshardmind (Migrated from github.com) commented 2019-09-05 00:02:06 +02:00

why? (and also, dont use \ line continuations here)

why? (and also, dont use `\` line continuations here)
mikeshardmind (Migrated from github.com) commented 2019-09-04 23:23:35 +02:00

This adds an unneeded overhead to each case of it. Better to fix the typhehint on ctx.send see #2520 for more details on things needed around this.

This adds an unneeded overhead to each case of it. Better to fix the typhehint on `ctx.send` see #2520 for more details on things needed around this.
mikeshardmind (Migrated from github.com) commented 2019-09-04 23:24:42 +02:00

Why is this PR adjusting whitespace in comments outside of a line-length goal?

Why is this PR adjusting whitespace in comments outside of a line-length goal?
@ -360,9 +359,8 @@ class Warnings(commands.Cog):
warning(_("You are not allowed to check warnings for other users!"))
)
mikeshardmind (Migrated from github.com) commented 2019-09-04 23:59:49 +02:00

No. This should be changed to use hasattr rather than try/except if we aren't using userid below

No. This should be changed to use `hasattr` rather than try/except if we aren't using `userid` below
mikeshardmind (Migrated from github.com) commented 2019-09-04 23:54:13 +02:00

Why?

Why?
Tobotimus (Migrated from github.com) reviewed 2019-09-05 01:42:43 +02:00
Tobotimus (Migrated from github.com) commented 2019-09-05 01:42:43 +02:00

\* down in the docstring below is an invalid escape character, and is technically deprecated since Python 3.6. Sometimes people escape **kwargs in docstrings with \*\*kwargs (same thing with *args -> \*args), thinking that sphinx will start bold/emphasis formatting. But sphinx actually handles this, and suggests omitting the backslashes for varargs: https://www.sphinx-doc.org/en/master/usage/extensions/example_numpy.html

So, TL;DR, don't use r""" docstrings containing \*\*kwargs, use plain unicode """ docstrings containing **kwargs.

`\*` down in the docstring below is an invalid escape character, and is technically deprecated since Python 3.6. Sometimes people escape `**kwargs` in docstrings with `\*\*kwargs` (same thing with `*args` -> `\*args`), thinking that sphinx will start bold/emphasis formatting. But sphinx actually handles this, and suggests omitting the backslashes for varargs: https://www.sphinx-doc.org/en/master/usage/extensions/example_numpy.html So, **TL;DR**, don't use `r"""` docstrings containing `\*\*kwargs`, use plain unicode `"""` docstrings containing `**kwargs`.
Drapersniper (Migrated from github.com) reviewed 2019-09-05 01:49:05 +02:00
Drapersniper (Migrated from github.com) commented 2019-09-05 01:49:05 +02:00

This is due to line length being longer than 99 characters, one bellow it’s the same but the backlash bellow is so that when this its printed to a user it isn’t forced into a new line

This is due to line length being longer than 99 characters, one bellow it’s the same but the backlash bellow is so that when this its printed to a user it isn’t forced into a new line
mikeshardmind (Migrated from github.com) reviewed 2019-09-05 01:51:53 +02:00
mikeshardmind (Migrated from github.com) commented 2019-09-05 01:51:52 +02:00

My "why" was more, why not fix the root issue here, just not expressing it as such.

My "why" was more, why not fix the root issue here, just not expressing it as such.
mikeshardmind (Migrated from github.com) reviewed 2019-09-05 01:57:05 +02:00
mikeshardmind (Migrated from github.com) commented 2019-09-05 01:57:05 +02:00

Didn't notice the line length on this one was that long, mildly surprised black didn't complain about it before given that it was already inside parens.

Didn't notice the line length on this one was that long, mildly surprised black didn't complain about it before given that it was already inside parens.
Drapersniper (Migrated from github.com) reviewed 2019-09-05 01:59:33 +02:00
Drapersniper (Migrated from github.com) commented 2019-09-05 01:59:33 +02:00

Same here like length was longer than 99, for some reason black didn’t complain for some of these but IDE flagged them as being too long (usually 100/110 characters

Same here like length was longer than 99, for some reason black didn’t complain for some of these but IDE flagged them as being too long (usually 100/110 characters
Tobotimus (Migrated from github.com) reviewed 2019-09-05 02:02:23 +02:00
Tobotimus (Migrated from github.com) commented 2019-09-05 02:02:23 +02:00

Yeah, this'll get rendered like this in the help command:

When toggled on, users are restricted to YouTube, SoundCloud,         Mixer, Vimeo, Twitch, and Bandcamp links.

I always get slightly annoyed with how docstrings end up looking in the help command - sticking to the 99-char line length but also considering where the line breaks will appear in the help embed is frustrating.

My solution would be to rewrap/unwrap each paragraph in the help formatter. Code-block help formatter should rewrap, whilst embed help formatter should unwrap. This means a single newline would usually treated as a space (we can make exceptions, possibly for numbered/unordered lists) and double newlines are preserved. Still not sure about this though.

Yeah, this'll get rendered like this in the help command: ``` When toggled on, users are restricted to YouTube, SoundCloud, Mixer, Vimeo, Twitch, and Bandcamp links. ``` I always get slightly annoyed with how docstrings end up looking in the help command - sticking to the 99-char line length but also considering where the line breaks will appear in the help embed is frustrating. My solution would be to rewrap/unwrap each paragraph in the help formatter. Code-block help formatter should rewrap, whilst embed help formatter should unwrap. This means a single newline would usually treated as a space (we can make exceptions, possibly for numbered/unordered lists) and double newlines are preserved. Still not sure about this though.
mikeshardmind (Migrated from github.com) reviewed 2019-09-05 02:18:16 +02:00
mikeshardmind (Migrated from github.com) commented 2019-09-05 02:18:16 +02:00

Let's keep the docstring free of line continuations here.

I'll open an issue + handle this case for 3.2 in how we handle help.

Let's keep the docstring free of line continuations here. I'll open an issue + handle this case for 3.2 in how we handle help.
Drapersniper commented 2019-09-06 21:41:36 +02:00 (Migrated from github.com)

Just for clarity, this should be fully up to date with the review from Sinbad and Toby.

Just for clarity, this should be fully up to date with the review from Sinbad and Toby.
mikeshardmind (Migrated from github.com) requested changes 2019-09-07 01:27:45 +02:00
mikeshardmind (Migrated from github.com) left a comment

I'm noticing some added # isort: ignore statements here.

For this to be included like this, if we're going to sort imports, it really should be added to both the makefile recipe (for reformatting) and the style test. Otherwise, these comments have no place in our code base.

I've already stated my dislike of mixing isort usage with black in discord, but if you have a set of settings and automatable steps which will produce consistent results, that shouldn't be a blocker to this.

I am going to insist that this be automated to keep the diff sizes down rather than something that just gets changed every so often.

I'm noticing some added `# isort: ignore` statements here. For this to be included like this, if we're going to sort imports, it really should be added to both the makefile recipe (for reformatting) and the style test. Otherwise, these comments have no place in our code base. I've already stated my dislike of mixing isort usage with black in discord, but if you have a set of settings and automatable steps which will produce consistent results, that shouldn't be a blocker to this. I am going to insist that this be automated to keep the diff sizes down rather than something that just gets changed every so often.
Drapersniper commented 2019-09-07 01:31:14 +02:00 (Migrated from github.com)

I'm noticing some added # isort: ignore statements here.

For this to be included like this, if we're going to sort imports, it really should be added to both the makefile recipe (for reformatting) and the style test. Otherwise, these comments have no place in our code base.

I've already stated my dislike of mixing isort usage with black in discord, but if you have a set of settings and automatable steps which will produce consistent results, that shouldn't be a blocker to this.

I am going to insist that this be automated to keep the diff sizes down rather than something that just gets changed every so often.

ermm ... this was unintentional ... i missed that the isort stuff was on this branch, let me remove them for now, there's 1 PR pending on the isort repo that after it's merged we should be able to fully automate to where black and isort can be used together consistently

> I'm noticing some added `# isort: ignore` statements here. > > For this to be included like this, if we're going to sort imports, it really should be added to both the makefile recipe (for reformatting) and the style test. Otherwise, these comments have no place in our code base. > > I've already stated my dislike of mixing isort usage with black in discord, but if you have a set of settings and automatable steps which will produce consistent results, that shouldn't be a blocker to this. > > I am going to insist that this be automated to keep the diff sizes down rather than something that just gets changed every so often. ermm ... this was unintentional ... i missed that the isort stuff was on this branch, let me remove them for now, there's 1 PR pending on the isort repo that after it's merged we should be able to fully automate to where black and isort can be used together consistently
Drapersniper commented 2019-11-12 14:55:13 +01:00 (Migrated from github.com)

Okay so Implemented both iSort and Black into tests, this is using iSort settings reccomended by the official Black docs.

All test are passing and no compatibility issues.

I would say if we want go ahead and use iSort in the project for further formatting consistency this PR is ready and could be merged after 3.2 (to reduce the number of conflicts just priort to a major release)

Okay so Implemented both iSort and Black into tests, this is using iSort settings reccomended by the official Black docs. All test are passing and no compatibility issues. I would say if we want go ahead and use iSort in the project for further formatting consistency this PR is ready and could be merged after 3.2 (to reduce the number of conflicts just priort to a major release)
Kowlin (Migrated from github.com) requested changes 2019-11-13 16:45:09 +01:00
Kowlin (Migrated from github.com) commented 2019-11-13 16:44:04 +01:00

Lets not change deployment versions... they are currently working on black magic and I don't want anything breaking with them.

Lets not change deployment versions... they are currently working on black magic and I don't want anything breaking with them.
Kowlin (Migrated from github.com) commented 2019-11-13 16:44:46 +01:00

Same over here.

Same over here.
Kowlin (Migrated from github.com) commented 2019-11-13 16:44:16 +01:00

Same over here.

Same over here.
Kowlin (Migrated from github.com) commented 2019-11-13 16:44:59 +01:00

Same over here.

Same over here.
Drapersniper (Migrated from github.com) reviewed 2019-11-13 16:54:05 +01:00
Drapersniper (Migrated from github.com) commented 2019-11-13 16:54:04 +01:00

WUT ... I though I updated these to 3.7.5 .... sigh
noted i'll ensure its 3.7.5 and py37

WUT ... I though I updated these to 3.7.5 .... *sigh* noted i'll ensure its 3.7.5 and py37
Drapersniper (Migrated from github.com) reviewed 2019-11-13 16:56:51 +01:00
Drapersniper (Migrated from github.com) commented 2019-11-13 16:56:51 +01:00

oh wait im dumb .... shouldn't at least the TOXENV be py37, since the bot no longer supports python3.6 so running the test agaisnt 3.7 would be more realist and find bugs caused by an specific version of python?

oh wait im dumb .... shouldn't at least the TOXENV be py37, since the bot no longer supports python3.6 so running the test agaisnt 3.7 would be more realist and find bugs caused by an specific version of python?
mikeshardmind (Migrated from github.com) requested changes 2019-11-13 17:50:37 +01:00
mikeshardmind (Migrated from github.com) left a comment

I haven't reviewed this in full. I'm still skeptical of a lot of the changes here at a surface level.

I had a few specific comments, but also I don't think it's appropriate here to be adding
# -*- coding: utf-8 -*- to the top of each python file.

This is a python3.7+ project. Every python file is treated as utf-8 by default as of python3.
Encoding specifications are only neccessary when they differe from the expected, It's just noise here and doesn't even draw contrast in any files where we might need a different encoding because all of the files are utf-8.

I haven't reviewed this in full. I'm still skeptical of a lot of the changes here at a surface level. I had a few specific comments, but also I don't think it's appropriate here to be adding `# -*- coding: utf-8 -*-` to the top of each python file. This is a python3.7+ project. Every python file is treated as utf-8 by default as of python3. Encoding specifications are only neccessary when they differe from the expected, It's just noise here and doesn't even draw contrast in any files where we might need a different encoding because all of the files are utf-8.
@ -0,0 +1,15 @@
[settings]
mikeshardmind (Migrated from github.com) commented 2019-11-13 17:45:57 +01:00

All of these headings are just visual noise. If isort is being enforced, they don't do anything useful, isort will sort it out anyway.

All of these headings are just visual noise. If isort is being enforced, they don't do anything useful, isort will sort it out anyway.
mikeshardmind (Migrated from github.com) commented 2019-11-13 17:42:56 +01:00

Adding this pre-commit config like this is a strong No from me. It requires we maintain settings in multiple places. We have configurations for these already, we have tests that will fail if someone didn't comply, and we have an easy way to run anything which should be automated. IDEs will format with black using our settings already just by setting black as the formatting provider. pre-commit runs in an isolated environment each time, ignoring our central settings.

As an example of the trouble this can cause, this has args which mismatch the ones we are using for one of the tools here, and you just created it. The fewer places we duplicate settings, the better.

This just isn't suited to the project.

Adding this pre-commit config like this is a strong No from me. It requires we maintain settings in multiple places. We have configurations for these already, we have tests that will fail if someone didn't comply, and we have an easy way to run anything which should be automated. IDEs will format with black using our settings already just by setting black as the formatting provider. pre-commit runs in an isolated environment each time, ignoring our central settings. As an example of the trouble this can cause, this has args which mismatch the ones we are using for one of the tools here, and you just created it. The fewer places we duplicate settings, the better. This just isn't suited to the project.
Drapersniper (Migrated from github.com) reviewed 2019-11-13 17:59:12 +01:00
Drapersniper (Migrated from github.com) commented 2019-11-13 17:59:12 +01:00

Are we sure thats the case when I was running the pre-commit hook localy based on this file it was using the settings from the setup.cfg, the individual files and the .ini file
Reason I added here was something user could easily run to process all the formatting in 1 command, and ensure everything will pass the Style check while applying the extra style stuff thats black and isort compatible thats in the pre-commit hooks (like json formatting etc)

Are we sure thats the case when I was running the pre-commit hook localy based on this file it was using the settings from the setup.cfg, the individual files and the .ini file Reason I added here was something user could easily run to process all the formatting in 1 command, and ensure everything will pass the Style check while applying the extra style stuff thats black and isort compatible thats in the pre-commit hooks (like json formatting etc)
Drapersniper (Migrated from github.com) reviewed 2019-11-13 18:01:33 +01:00
@ -0,0 +1,15 @@
[settings]
Drapersniper (Migrated from github.com) commented 2019-11-13 18:01:32 +01:00

So yeah this came up on discord before, yes they are noise but ... it helps people who are less familiar with the ordering prior to running isort. So this is more educational than anything happy to remove but from the discord talks they would be here to explain the ordering so people learn it rather than just having a tools "randomly" sort things priort to commiting.

So yeah this came up on discord before, yes they are noise but ... it helps people who are less familiar with the ordering prior to running isort. So this is more educational than anything happy to remove but from the discord talks they would be here to explain the ordering so people learn it rather than just having a tools "randomly" sort things priort to commiting.
Drapersniper commented 2019-11-13 18:02:58 +01:00 (Migrated from github.com)

I haven't reviewed this in full. I'm still skeptical of a lot of the changes here at a surface level.

I had a few specific comments, but also I don't think it's appropriate here to be adding
# -*- coding: utf-8 -*- to the top of each python file.

This is a python3.7+ project. Every python file is treated as utf-8 by default as of python3.
Encoding specifications are only neccessary when they differe from the expected, It's just noise here and doesn't even draw contrast in any files where we might need a different encoding because all of the files are utf-8.

So this is the case for people with a real IDE, but in the past when using a non python IDE i did hit issues when editing on Windows and handling emojis and the emojis breaking

> I haven't reviewed this in full. I'm still skeptical of a lot of the changes here at a surface level. > > I had a few specific comments, but also I don't think it's appropriate here to be adding > `# -*- coding: utf-8 -*-` to the top of each python file. > > This is a python3.7+ project. Every python file is treated as utf-8 by default as of python3. > Encoding specifications are only neccessary when they differe from the expected, It's just noise here and doesn't even draw contrast in any files where we might need a different encoding because all of the files are utf-8. So this is the case for people with a real IDE, but in the past when using a non python IDE i did hit issues when editing on Windows and handling emojis and the emojis breaking
Kowlin commented 2019-11-13 18:04:07 +01:00 (Migrated from github.com)

That's the fault of your IDE and not the project. Visual studio code on Windows has fine emoji support.

That's the fault of your IDE and not the project. Visual studio code on Windows has fine emoji support.
mikeshardmind (Migrated from github.com) reviewed 2019-11-13 18:06:23 +01:00
mikeshardmind (Migrated from github.com) commented 2019-11-13 18:06:23 +01:00

make reformat already handles the style bit. And yes, pre-commit creates a virtualenv specifically for all of this, using the versions specified here, not in setup.cfg and using the args specified here over the ones in our configuration files, which makes this another place we would need to update multiple things rather than just let people be responsible for their own development environments and catch it at the tests.

`make reformat` already handles the style bit. And yes, pre-commit creates a virtualenv specifically for all of this, using the versions specified here, not in setup.cfg and using the args specified here over the ones in our configuration files, which makes this another place we would need to update multiple things rather than just let people be responsible for their own development environments and catch it at the tests.
mikeshardmind (Migrated from github.com) reviewed 2019-11-13 18:09:31 +01:00
@ -0,0 +1,15 @@
[settings]
mikeshardmind (Migrated from github.com) commented 2019-11-13 18:09:31 +01:00

Info like this belongs in contributing.md, not in every source code file. This isn't a critical comment that needs visibility everywhere something is in use.

Info like this belongs in contributing.md, not in every source code file. This isn't a critical comment that needs visibility everywhere something is in use.
mikeshardmind commented 2019-11-14 19:14:42 +01:00 (Migrated from github.com)

Marking this is blocked for 2 reasons.

  1. Let's wait on isort 5.0.0, it's supposed to be getting some extra stuff to make it cooperate with black a bit better.

  2. This still appears to be touching files that were generated, not written (localization messages) which I'm sure isn't in our interests to have multiple tools changing.

Marking this is blocked for 2 reasons. 1. Let's wait on isort 5.0.0, it's supposed to be getting some extra stuff to make it cooperate with black a bit better. 2. This still appears to be touching files that were generated, not written (localization messages) which I'm sure isn't in our interests to have multiple tools changing.

Pull request closed

Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!2980
No description provided.