|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import contextlib |
3 | 4 | import functools |
4 | 5 | import importlib.util |
5 | 6 | import ipaddress |
|
13 | 14 | from collections import Counter, UserString |
14 | 15 | from collections.abc import Callable, Collection, Iterator, Mapping, MutableMapping |
15 | 16 | from types import SimpleNamespace |
16 | | -from typing import Any, Generic, NamedTuple, TypeVar, Union, overload |
| 17 | +from typing import Any, Generic, NamedTuple, TypeVar, overload |
17 | 18 | from urllib.parse import quote as urlquote |
18 | 19 | from urllib.parse import urlsplit, urlunsplit |
19 | 20 |
|
@@ -93,7 +94,7 @@ def pre_validation(self, config: Config, key_name: str): |
93 | 94 | def run_validation(self, value: object) -> SomeConfig: |
94 | 95 | config = self.config_class(config_file_path=self._config_file_path) |
95 | 96 | try: |
96 | | - config.load_dict(value) # type: ignore |
| 97 | + config.load_dict(value) # type: ignore[arg-type] |
97 | 98 | failed, warnings = config.validate() |
98 | 99 | except ConfigurationError as e: |
99 | 100 | raise ValidationError(str(e)) |
@@ -204,15 +205,13 @@ def run_validation(self, value: object) -> list[T]: |
204 | 205 | return value |
205 | 206 |
|
206 | 207 | fake_config = LegacyConfig(()) |
207 | | - try: |
| 208 | + with contextlib.suppress(AttributeError): |
208 | 209 | fake_config.config_file_path = self._config.config_file_path |
209 | | - except AttributeError: |
210 | | - pass |
211 | 210 |
|
212 | 211 | # Emulate a config-like environment for pre_validation and post_validation. |
213 | 212 | parent_key_name = getattr(self, '_key_name', '') |
214 | 213 | fake_keys = [f'{parent_key_name}[{i}]' for i in range(len(value))] |
215 | | - fake_config.data = dict(zip(fake_keys, value)) |
| 214 | + fake_config.data = dict(zip(fake_keys, value, strict=True)) |
216 | 215 |
|
217 | 216 | self.option_type.warnings = self.warnings |
218 | 217 | for key_name in fake_config: |
@@ -259,10 +258,8 @@ def run_validation(self, value: object) -> dict[str, T]: |
259 | 258 | return value |
260 | 259 |
|
261 | 260 | fake_config = LegacyConfig(()) |
262 | | - try: |
| 261 | + with contextlib.suppress(AttributeError): |
263 | 262 | fake_config.config_file_path = self._config.config_file_path |
264 | | - except AttributeError: |
265 | | - pass |
266 | 263 |
|
267 | 264 | # Emulate a config-like environment for pre_validation and post_validation. |
268 | 265 | fake_config.data = value |
@@ -362,7 +359,7 @@ def __init__(self, choices: Collection[T], default: T | None = None, **kwargs) - |
362 | 359 | def run_validation(self, value: object) -> T: |
363 | 360 | if value not in self.choices: |
364 | 361 | raise ValidationError(f"Expected one of: {self.choices} but received: {value!r}") |
365 | | - return value # type: ignore |
| 362 | + return value # type: ignore[return-value] |
366 | 363 |
|
367 | 364 |
|
368 | 365 | class Deprecated(BaseConfigOption): |
@@ -508,7 +505,7 @@ def run_validation(self, value: object) -> str: |
508 | 505 | raise ValidationError("The URL isn't valid, it should include the http:// (scheme)") |
509 | 506 |
|
510 | 507 |
|
511 | | -class Optional(Generic[T], BaseConfigOption[Union[T, None]]): |
| 508 | +class Optional(Generic[T], BaseConfigOption[T | None]): |
512 | 509 | """ |
513 | 510 | Wraps a field and makes a None value possible for it when no value is set. |
514 | 511 |
|
@@ -539,7 +536,7 @@ def run_validation(self, value: object) -> T | None: |
539 | 536 | return self.option.validate(value) |
540 | 537 |
|
541 | 538 | def post_validation(self, config: Config, key_name: str): |
542 | | - result = self.option.post_validation(config, key_name) # type: ignore |
| 539 | + result = self.option.post_validation(config, key_name) # type: ignore[func-returns-value] |
543 | 540 | self.warnings = self.option.warnings |
544 | 541 | return result |
545 | 542 |
|
@@ -940,7 +937,7 @@ def __fspath__(self): |
940 | 937 | return self.path |
941 | 938 |
|
942 | 939 |
|
943 | | -class ExtraScript(BaseConfigOption[Union[ExtraScriptValue, str]]): |
| 940 | +class ExtraScript(BaseConfigOption[ExtraScriptValue | str]): |
944 | 941 | def __init__(self): |
945 | 942 | super().__init__() |
946 | 943 | self.option_type = SubConfig[ExtraScriptValue]() |
|
0 commit comments