Hi all, first time posting here.
I’m currently trying to enforce typing checks such that I can only pass the “choices” parameter in the following snippet of code if the “type_” parameter is one of “int”, “str” or “float”. Unfortunately, when I pass “bool” typing checks succeed as -from what I’ve been able to gather- “typing.Type” is covariant, and since “bool” subclasses “int”, it is then interpreted as such.
T = TypeVar("T", int, str, float)
class Choice(Generic[T]):
value: T
def __init__(self, name: str, value: T) -> None: ...
def _to_dict(self) -> Dict[str, Any]: ...
class Parameter:
@overload
def __init__(
self,
type_: Type[bool],
name: str,
description: str,
required: bool = True,
) -> None: ...
@overload
def __init__(
self,
type_: Type[T],
name: str,
description: str,
required: bool = True,
choices: Optional[List[Choice[T]]] = None,
) -> None: ...
def __init__(
self,
type_: Union[Type[bool], Type[T]],
name: str,
description: str,
required: bool = True,
choices=None,
) -> None: ...
Here’s an example that’s passing typechecks when I wouldn’t want it to:
Parameter(
type_=bool,
name="everyone",
description='Whether to mention everyone',
choices=[]
)
I’ve been searching for hours, but I’ve been struggling to find any solution or workaround for this specific issue. Would anyone here happen to know how I could possibly fix this?