3
3
import typing
4
4
from abc import ABC , abstractmethod
5
5
from collections import OrderedDict
6
+ from inspect import isclass
6
7
from pathlib import Path
7
8
from typing import Any , Callable , Dict , Generic , Iterator , List , Literal , Optional , Set , TypeVar , Union , cast
8
9
18
19
class Convert (ABC , Generic [T ]):
19
20
"""A class that converts a raw type to a given tox (python) type."""
20
21
21
- def to (self , raw : T , of_type : type [V ], factory : Factory [V ]) -> V : # noqa: PLR0911
22
+ def to (self , raw : T , of_type : type [V ], factory : Factory [V ]) -> V : # noqa: PLR0911, C901
22
23
"""
23
24
Convert given raw type to python type.
24
25
@@ -30,26 +31,26 @@ def to(self, raw: T, of_type: type[V], factory: Factory[V]) -> V: # noqa: PLR09
30
31
from_module = getattr (of_type , "__module__" , None )
31
32
if from_module in {"typing" , "typing_extensions" }:
32
33
return self ._to_typing (raw , of_type , factory )
33
- if issubclass (of_type , Path ):
34
- return self . to_path ( raw ) # type: ignore[return-value]
35
- if issubclass ( of_type , bool ):
36
- return self . to_bool ( raw ) # type: ignore[return-value]
37
- if issubclass ( of_type , Command ):
38
- return self . to_command ( raw ) # type: ignore[return-value]
39
- if issubclass ( of_type , EnvList ):
40
- return self . to_env_list ( raw ) # type: ignore[return-value]
41
- if issubclass ( of_type , str ):
42
- 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.
34
+ if isclass (of_type ):
35
+ if issubclass ( of_type , Path ):
36
+ return self . to_path ( raw ) # type: ignore[return-value]
37
+ if issubclass ( of_type , bool ):
38
+ return self . to_bool ( raw ) # type: ignore[return-value]
39
+ if issubclass ( of_type , Command ):
40
+ return self . to_command ( raw ) # type: ignore[return-value]
41
+ if issubclass ( of_type , EnvList ):
42
+ return self . to_env_list ( raw ) # type: ignore[return-value]
43
+ if issubclass ( of_type , str ):
44
+ return self . to_str ( raw ) # type: ignore[return-value]
45
+ # python does not allow use of parametrized generics with isinstance, so we need to check for them.
45
46
if hasattr (typing , "GenericAlias" ) and isinstance (of_type , typing .GenericAlias ):
46
47
return list (self .to_list (raw , of_type = of_type )) # type: ignore[return-value]
47
48
if isinstance (raw , of_type ): # already target type no need to transform it
48
49
# do it this late to allow normalization - e.g. string strip
49
- return raw
50
+ return raw # type: ignore[no-any-return]
50
51
if factory :
51
52
return factory (raw )
52
- return of_type (raw ) # type: ignore[call-arg ]
53
+ return of_type (raw ) # type: ignore[no-any-return ]
53
54
54
55
def _to_typing (self , raw : T , of_type : type [V ], factory : Factory [V ]) -> V : # noqa: C901
55
56
origin = getattr (of_type , "__origin__" , of_type .__class__ )
0 commit comments