lib.Types.LoadTypes: Loosen type_filter type
In LoadTypes' constructor, allow the type_filter parameter to be of type Sequence[type[Any]] instead of list[type[T]]. a) Sequence is more generic than list, and b) with T instead of Any, trying to instantiate with an abstract class has mypy complain:
# E: Only concrete class can be given where "type[MyClass]" is expected [type-abstract]- type_filter: list[type[T]] = [], + type_filter: Sequence[type[Any]] | None = None,
Not that this makes mypy complain that it needs an annotation at the places where LoadTypes is used.
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
a3279a3b00
commit
fd008b0a73
2 changed files with 7 additions and 4 deletions
|
|
@ -137,7 +137,7 @@ class App: # export
|
|||
|
||||
log(DEBUG, '-------------- Running: >' + ' '.join(sys.argv) + '<')
|
||||
|
||||
cmd_classes = LoadTypes(
|
||||
cmd_classes: LoadTypes[AbstractCmd] = LoadTypes(
|
||||
modules if modules else ['__main__'],
|
||||
type_name_filter = name_filter,
|
||||
type_filter = [AbstractCmd], # type: ignore[type-abstract]
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ from .log import OFF, log, parse_log_level
|
|||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Iterator
|
||||
from typing import Any
|
||||
from typing import Any, Sequence
|
||||
|
||||
T = TypeVar('T')
|
||||
|
||||
|
|
@ -45,7 +45,7 @@ class LoadTypes(Types[T]): # export
|
|||
self,
|
||||
mod_names: list[str],
|
||||
type_name_filter: str | None = None,
|
||||
type_filter: list[type[T]] = [],
|
||||
type_filter: Sequence[type[Any]] | None = None,
|
||||
debug_level = None,
|
||||
):
|
||||
if debug_level is None:
|
||||
|
|
@ -65,9 +65,12 @@ class LoadTypes(Types[T]): # export
|
|||
log(self.__debug_level, *args, **kwargs)
|
||||
|
||||
def _stringify(self):
|
||||
tf = 'None' if self.__type_filter is None else (
|
||||
', '.join([str(f) for f in self.__type_filter])
|
||||
)
|
||||
return [
|
||||
'type_name_filter: ' + str(self.__type_name_filter),
|
||||
'type_filter: ' + ', '.join([str(f) for f in self.__type_filter]),
|
||||
'type_filter: ' + tf,
|
||||
'mod_names: ' + ', '.join(self.__mod_names),
|
||||
]
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue