Add remaining re-linting changes (part 2 of 2)
Marco Ricci

Marco Ricci commited on 2025-01-07 22:44:39
Zeige 6 geänderte Dateien mit 18 Einfügungen und 16 Löschungen.


The remaining re-linting changes pertain to new linting codes:

  * TC006/runtime-cast-value (formerly TCH006) suggests that cast calls
    use stringified type declarations.  Implemented.

  * B033/duplicate-value requires sets not to contain duplicate values.
    This is used in the `_types.js_truthiness` function to both test
    against and to document all of the falsy values except NaN.
    Ignored.

  * RUF043/pytest-raises-ambiguous-pattern requires patterns in the
    `match` argument of `pytest.raises` to be either raw strings, or
    passed through `re.escape`.  Implemented.

  * RUF046/unnecessary-cast-to-int forbids explicitly casting the result
    of `math.ceil` to int.  I believe `mypy`/`typeshed` originally had
    problems with this, but this no longer appears to be the case.
    Implemented.
... ...
@@ -163,7 +163,7 @@ class TranslatedString:
163 163
         if isinstance(
164 164
             template, (Label, InfoMsgTemplate, WarnMsgTemplate, ErrMsgTemplate)
165 165
         ):
166
-            template = cast(TranslatableString, template.value)
166
+            template = cast('TranslatableString', template.value)
167 167
         self.template = template
168 168
         self.kwargs = {**args_dict, **kwargs}
169 169
         self._rendered: str | None = None
... ...
@@ -10,6 +10,7 @@ import collections
10 10
 import enum
11 11
 import json
12 12
 import math
13
+import string
13 14
 from typing import TYPE_CHECKING
14 15
 
15 16
 from typing_extensions import (
... ...
@@ -195,11 +196,11 @@ def json_path(path: Sequence[str | int], /) -> str:
195 196
 
196 197
     def needs_longhand(x: str | int) -> bool:
197 198
         initial = (
198
-            frozenset('abcdefghijklmnopqrstuvwxyz')
199
-            | frozenset('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
199
+            frozenset(string.ascii_lowercase)
200
+            | frozenset(string.ascii_uppercase)
200 201
             | frozenset('_')
201 202
         )
202
-        chars = initial | frozenset('0123456789')
203
+        chars = initial | frozenset(string.digits)
203 204
         return not (
204 205
             isinstance(x, str)
205 206
             and x
... ...
@@ -413,7 +414,7 @@ def js_truthiness(value: Any, /) -> bool:  # noqa: ANN401
413 414
 
414 415
     """  # noqa: RUF002
415 416
     try:
416
-        if value in {None, False, 0, 0.0, ''}:
417
+        if value in {None, False, 0, 0.0, ''}:  # noqa: B033
417 418
             return False
418 419
     except TypeError:
419 420
         # All falsy values are hashable, so this can't be falsy.
... ...
@@ -2041,7 +2041,7 @@ def _prompt_for_passphrase() -> str:
2041 2041
 
2042 2042
     """
2043 2043
     return cast(
2044
-        str,
2044
+        'str',
2045 2045
         click.prompt(
2046 2046
             'Passphrase',
2047 2047
             default='',
... ...
@@ -3042,7 +3042,7 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
3042 3042
         assert service is not None
3043 3043
         configuration = get_config()
3044 3044
         text = DEFAULT_NOTES_TEMPLATE + configuration['services'].get(
3045
-            service, cast(_types.VaultConfigServicesSettings, {})
3045
+            service, cast('_types.VaultConfigServicesSettings', {})
3046 3046
         ).get('notes', '')
3047 3047
         notes_value = click.edit(text=text)
3048 3048
         if notes_value is not None:
... ...
@@ -3081,7 +3081,7 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
3081 3081
             # TODO(the-13th-letter): keep track of auto-close; try
3082 3082
             # os.dup if feasible
3083 3083
             infile = cast(
3084
-                TextIO,
3084
+                'TextIO',
3085 3085
                 (
3086 3086
                     import_settings
3087 3087
                     if hasattr(import_settings, 'close')
... ...
@@ -3167,14 +3167,14 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
3167 3167
         try:
3168 3168
             _check_for_misleading_passphrase(
3169 3169
                 ('global',),
3170
-                cast(dict[str, Any], maybe_config.get('global', {})),
3170
+                cast('dict[str, Any]', maybe_config.get('global', {})),
3171 3171
                 main_config=user_config,
3172 3172
                 ctx=ctx,
3173 3173
             )
3174 3174
             for key, value in maybe_config['services'].items():
3175 3175
                 _check_for_misleading_passphrase(
3176 3176
                     ('services', key),
3177
-                    cast(dict[str, Any], value),
3177
+                    cast('dict[str, Any]', value),
3178 3178
                     main_config=user_config,
3179 3179
                     ctx=ctx,
3180 3180
                 )
... ...
@@ -3243,7 +3243,7 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
3243 3243
             # TODO(the-13th-letter): keep track of auto-close; try
3244 3244
             # os.dup if feasible
3245 3245
             outfile = cast(
3246
-                TextIO,
3246
+                'TextIO',
3247 3247
                 (
3248 3248
                     export_settings
3249 3249
                     if hasattr(export_settings, 'close')
... ...
@@ -3308,10 +3308,10 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
3308 3308
                 if k in service_keys and v is not None
3309 3309
             },
3310 3310
             cast(
3311
-                dict[str, Any],
3311
+                'dict[str, Any]',
3312 3312
                 configuration['services'].get(service, {}) if service else {},
3313 3313
             ),
3314
-            cast(dict[str, Any], configuration.get('global', {})),
3314
+            cast('dict[str, Any]', configuration.get('global', {})),
3315 3315
         )
3316 3316
         if not store_config_only and not service:
3317 3317
             err_msg = _msg.TranslatedString(
... ...
@@ -253,7 +253,7 @@ class Vault:
253 253
             raise ValueError(msg)
254 254
         # Ensure the bound is strictly positive.
255 255
         entropy_bound = max(1, self._entropy())
256
-        return int(math.ceil(safety_factor * entropy_bound / 8))
256
+        return math.ceil(safety_factor * entropy_bound / 8)
257 257
 
258 258
     @staticmethod
259 259
     def _get_binary_string(s: bytes | bytearray | str, /) -> bytes:
... ...
@@ -97,7 +97,7 @@ class Test001ExporterUtils:
97 97
     ) -> None:
98 98
         monkeypatch.setattr(os.path, 'expanduser', lambda x: x)
99 99
         with pytest.raises(
100
-            RuntimeError, match='[Cc]annot determine home directory'
100
+            RuntimeError, match=r'[Cc]annot determine home directory'
101 101
         ):
102 102
             exporter.get_vault_path()
103 103
 
... ...
@@ -659,7 +659,8 @@ class TestAgentInteraction:
659 659
             client = stack.enter_context(ssh_agent.SSHAgentClient())
660 660
             monkeypatch2.setattr(client, 'request', request)
661 661
             with pytest.raises(
662
-                RuntimeError, match='Malformed response|does not match request'
662
+                RuntimeError,
663
+                match=r'Malformed response|does not match request',
663 664
             ):
664 665
                 client.query_extensions()
665 666
 
666 667