Skip to content

Commit 7317225

Browse files
Allow ConfigSet.add_config to receive parameterized generics for of_type. (#3288)
* Allow use of list[str] inside plugins Fixes: #3287 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 406f808 commit 7317225

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

docs/changelog/3288.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow ``ConfigSet.add_config`` to receive parameterized generics for ``of_type``.

src/tox/config/loader/convert.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import typing
34
from abc import ABC, abstractmethod
45
from collections import OrderedDict
56
from pathlib import Path
@@ -39,6 +40,10 @@ def to(self, raw: T, of_type: type[V], factory: Factory[V]) -> V: # noqa: PLR09
3940
return self.to_env_list(raw) # type: ignore[return-value]
4041
if issubclass(of_type, str):
4142
return self.to_str(raw) # type: ignore[return-value]
43+
# python does not allow use of parametrized generics with isinstance,
44+
# so we need to check for them.
45+
if hasattr(typing, "GenericAlias") and isinstance(of_type, typing.GenericAlias):
46+
return list(self.to_list(raw, of_type=of_type)) # type: ignore[return-value]
4247
if isinstance(raw, of_type): # already target type no need to transform it
4348
# do it this late to allow normalization - e.g. string strip
4449
return raw

tests/config/loader/test_str_convert.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ def test_str_convert_ok(raw: str, value: Any, of_type: type[Any]) -> None:
5757
assert result == value
5858

5959

60+
# Tests that can work only with py39 or newer due to type not being subscriptible before
61+
if sys.version_info >= (3, 9):
62+
63+
@pytest.mark.parametrize(
64+
("raw", "value", "of_type"),
65+
[
66+
("", None, Optional[list[str]]),
67+
("1,2", ["1", "2"], Optional[list[str]]),
68+
],
69+
)
70+
def test_str_convert_ok_py39(raw: str, value: Any, of_type: type[Any]) -> None:
71+
result = StrConvert().to(raw, of_type, None)
72+
assert result == value
73+
74+
6075
@pytest.mark.parametrize(
6176
("raw", "of_type", "exc_type", "msg"),
6277
[

0 commit comments

Comments
 (0)