Repository: wemake-services/wemake-python-styleguide

Issue:
Allow custom short names
# Rule request

While some short names have been whitelisted in #1548, there are still cases when using short names is justified. For example, [albumenatations](https://albumentations.ai) library uses `A` as an alias in their examples. Or `x` and `y` are common names in machine learning. Disabling the entire WPS111 rule would be too broad, while putting noqa would pollute the code. There is an option to add allowed domain names (#1106), but this does not exclude them from the WPS111 rule.

## Thesis

<!-- What exactly do you propose? Please, provide code examples. -->

Make adding variable names to allowed domain names exclude them from the WPS111 rule.

## Reasoning

<!--Why do you think this is a good idea? Please, include some strong points and considerations.-->

There are still short names that are justified to be used in some domains. Like `x` and `y` in machine learning or `A` as an alias for the albumentations library.

<!--

  Love wemake-python-styleguide? ❤️
  Do you want your features to be implemented faster?

  Please consider supporting our collective:
👉  https://opencollective.com/wemake-python-styleguide/donate

-->


Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/wemake-services/wemake-python-styleguide/pull/3233?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=wemake-services) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 100.00%. Comparing base [(`d41d795`)](https://app.codecov.io/gh/wemake-services/wemake-python-styleguide/commit/d41d79589a7d838eb23c74b8f3eebdf5f480b97d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=wemake-services) to head [(`674104f`)](https://app.codecov.io/gh/wemake-services/wemake-python-styleguide/commit/674104f77904c8586bdcf62fee7435eb3f08f0fd?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=wemake-services).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##            master     #3233   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          348       348           
  Lines        11488     11452   -36     
  Branches       797       790    -7     
=========================================
- Hits         11488     11452   -36     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/wemake-services/wemake-python-styleguide/pull/3233?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=wemake-services).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=wemake-services).


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d76bf376..2ddf8724 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -131,6 +131,8 @@ Semantic versioning in our case means:
 - Adds a new rule to forbid extra syntax in `match ...` subjects, #3217
 - Adds new `--allowed-module-metadata` and `--forbidden-module-metadata`
   configuration options for `WPS410`, #3060
+- Now `--allowed-domain-names` also affect `WPS11`
+  to allow custom short variable names, #2554
 - Adds support to run `wemake-python-styleguide` as a `pre-commit` hook, #2588
 - GitHub Action can now use `cwd:` parameter to specify
   where your configuration file is, #2474
diff --git a/wemake_python_styleguide/violations/naming.py b/wemake_python_styleguide/violations/naming.py
index 933aed81..a9e5564d 100644
--- a/wemake_python_styleguide/violations/naming.py
+++ b/wemake_python_styleguide/violations/naming.py
@@ -366,6 +366,10 @@ class TooShortNameViolation(MaybeASTViolation):
         Default:
         :str:`wemake_python_styleguide.options.defaults.MIN_NAME_LENGTH`
 
+        Pass allowed short names with ``--allowed-domain-names``.
+        Default:
+        :str:`wemake_python_styleguide.options.defaults.ALLOWED_DOMAIN_NAMES`
+
     .. versionadded:: 0.1.0
     .. versionchanged:: 0.4.0
     .. versionchanged:: 0.12.0
diff --git a/wemake_python_styleguide/visitors/ast/naming/validation.py b/wemake_python_styleguide/visitors/ast/naming/validation.py
index ce0dc41d..9e03b63f 100644
--- a/wemake_python_styleguide/visitors/ast/naming/validation.py
+++ b/wemake_python_styleguide/visitors/ast/naming/validation.py
@@ -151,23 +151,30 @@ class _RegularNameValidator(_SimpleNameValidator):
         self._ensure_readable_name(node, name)
 
     def _ensure_length(self, node: ast.AST, name: str) -> None:
-        min_length = self._options.min_name_length
-        if logical.is_too_short_name(name, min_length=min_length):
+        if (
+            logical.is_too_short_name(
+                name,
+                min_length=self._options.min_name_length,
+            )
+            and name not in self._options.allowed_domain_names
+        ):
             self._error_callback(
                 naming.TooShortNameViolation(
                     node,
                     text=name,
-                    baseline=min_length,
+                    baseline=self._options.min_name_length,
                 ),
             )
 
-        max_length = self._options.max_name_length
-        if logical.is_too_long_name(name, max_length=max_length):
+        if logical.is_too_long_name(
+            name,
+            max_length=self._options.max_name_length,
+        ):
             self._error_callback(
                 naming.TooLongNameViolation(
                     node,
                     text=name,
-                    baseline=max_length,
+                    baseline=self._options.max_name_length,
                 ),
             )
 


Test patch:
diff --git a/tests/test_visitors/test_ast/test_naming/test_naming_rules/test_short.py b/tests/test_visitors/test_ast/test_naming/test_naming_rules/test_short.py
index 8c2ad0f1..b7a41c14 100644
--- a/tests/test_visitors/test_ast/test_naming/test_naming_rules/test_short.py
+++ b/tests/test_visitors/test_ast/test_naming/test_naming_rules/test_short.py
@@ -86,3 +86,23 @@ def test_naming_length_settings(
 
     assert_errors(visitor, [TooShortNameViolation])
     assert_error_text(visitor, short_name, option_values.min_name_length)
+
+
+def test_naming_domain_settings(
+    assert_errors,
+    parse_ast_tree,
+    own_naming_template,
+    options,
+    mode,
+):
+    """Ensures that customized domain names are allowed."""
+    short_name = 'a'
+    tree = parse_ast_tree(
+        mode(own_naming_template.format(short_name)),
+    )
+
+    option_values = options(allowed_domain_names=('a',))
+    visitor = WrongNameVisitor(option_values, tree=tree)
+    visitor.run()
+
+    assert_errors(visitor, [])