WIP: Code Quality changes #2978

Closed
Drapersniper wants to merge 8 commits from cleanup into V3/develop
Drapersniper commented 2019-09-03 14:48:52 +02:00 (Migrated from github.com)

Type

  • Bugfix
  • Enhancement
  • New feature

Description of the changes

Still haven't touched the following (Note, errors its just me not having pytest locally)
image

### Type - [x] Bugfix - [ ] Enhancement - [ ] New feature ### Description of the changes Still haven't touched the following (Note, errors its just me not having pytest locally) ![image](https://user-images.githubusercontent.com/27962761/64174546-6b564700-ce51-11e9-9529-d6287bb44eb3.png)
mikeshardmind commented 2019-09-04 21:04:40 +02:00 (Migrated from github.com)

This has a lot of changes that are more noise than benefit.

There are also a lot of good changes. Typos in strings fixed up, as an example.

A lot of this seems motivated by Pycharm's inspections, which they can be useful when looking at problems, pycharm is often overzealous in its messages.

This also breaks i18n in a few places by changing something without having context on why it was done.

I think this PR is trying to do a bit too much at once, and that not all of what it is trying to do is going to be beneficial.

i18n details

The following pattern is intentional


_ = lambda s: s
some_module_level_string = _("Something")
_ = translator(...)

We don't want the module level string translated at definition, but we still want it to be picked up by tools which use _("a string") as a detection for strings intended for inclusion in messages.pot

Why not use noinspection statements?

Things such as #noinspection PyCharmError really should not be enforced in the code base, but in a local editor config, as this is a Pycharm specific pattern, and would require all contributors to be using Pycharm to maintain consistently.

Okay, but then why are # pylint: disable=... statements fair game?

There are a couple places where we do use # pylint: disable=pylint-error-name however, this is with using pylint as part of the CI process to help quickly prevent simple typo based errors from being overlooked.

pylint runs as a lightweight CLI tool, and the project config for pylint is already preventing anything less serious than an error to be shown.

Anyone can add this without needing to be using a specific editor to even know they would need to do so, and we've taken steps to ensure that the majority of the time, it shouldn't be needed.

This has a lot of changes that are more noise than benefit. There are also a lot of good changes. Typos in strings fixed up, as an example. A lot of this seems motivated by Pycharm's inspections, which they can be useful when looking at problems, pycharm is often overzealous in its messages. This also breaks i18n in a few places by changing something without having context on why it was done. I think this PR is trying to do a bit too much at once, and that not all of what it is trying to do is going to be beneficial. ### i18n details The following pattern is intentional ```py _ = lambda s: s some_module_level_string = _("Something") _ = translator(...) ``` We don't want the module level string translated at definition, but we still want it to be picked up by tools which use `_("a string")` as a detection for strings intended for inclusion in messages.pot ### Why not use noinspection statements? Things such as `#noinspection PyCharmError` really should not be enforced in the code base, but in a local editor config, as this is a Pycharm specific pattern, and would require all contributors to be using Pycharm to maintain consistently. Okay, but then why are `# pylint: disable=...` statements fair game? There are a couple places where we do use `# pylint: disable=pylint-error-name` however, this is with using pylint as part of the CI process to help quickly prevent simple typo based errors from being overlooked. pylint runs as a lightweight CLI tool, and the project config for pylint is already preventing anything less serious than an error to be shown. Anyone can add this without needing to be using a specific editor to even know they would need to do so, and we've taken steps to ensure that the majority of the time, it shouldn't be needed.
Drapersniper commented 2019-09-04 21:31:10 +02:00 (Migrated from github.com)

Okay cool I’ll revert the pycharm specific things and translations, sounds like I misunderstood how translations work here, though if we passed the string to the translation function they would be translated regardless.

Okay cool I’ll revert the pycharm specific things and translations, sounds like I misunderstood how translations work here, though if we passed the string to the translation function they would be translated regardless.
Drapersniper commented 2019-09-04 22:36:39 +02:00 (Migrated from github.com)

Closing becayse Github hates me ..

Closing becayse Github hates me ..
Tobotimus commented 2019-09-04 22:44:50 +02:00 (Migrated from github.com)

@Drapersniper the main tips for i18n are:

  • Only ever pass plain or raw string literals to a function named _(). No variables/constants, no f-strings, no bytestrings, and .format() the string outside the function call. If you need to pass a variable/constant to a translator function, the function should be given an alias other than _, so that redgettext (the script which extracts strings and makes the message catalogues) can warn about places where a mistake might have been made (e.g. _("format string {}".format(abc)) is a common accidental mistake)
  • Strings must be translated at runtime - this means strings defined at the module level need to be passed into the translator function later, usually during the command logic. However, because strings only get extracted by redgettext if the string literal is enclosed by _(), you need to create a dummy function (which just returns the string provided) called _() to mark the strings for extraction, without translating it.
@Drapersniper the main tips for i18n are: - Only ever pass plain or raw string literals to a function named `_()`. No variables/constants, no f-strings, no bytestrings, and `.format()` the string *outside* the function call. If you need to pass a variable/constant to a translator function, the function should be given an alias other than `_`, so that redgettext (the script which extracts strings and makes the message catalogues) can warn about places where a mistake might have been made (e.g. `_("format string {}".format(abc))` is a common accidental mistake) - Strings must be translated at runtime - this means strings defined at the module level need to be passed into the translator function later, usually during the command logic. However, because strings only get extracted by redgettext if the *string literal* is enclosed by `_()`, you need to create a dummy function (which just returns the string provided) called `_()` to mark the strings for extraction, without translating it.

Pull request closed

Sign in to join this conversation.
No reviewers
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!2978
No description provided.