-
Hi, I am trying to create a import typing
from beartype.typing import Annotated
from beartype.vale import Is
class Literal(typing.Literal):
def __class_getitem__(cls, X: tuple[str]) -> Annotated[str, Is]:
return Annotated[str, Is[lambda x: x.lower() in X]] ... but mypy complains. For example, when having a function like this def func(item: Literal["a", "b"] = "a"): ... it raises this error: Any idea how to fix this? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Fascinating... and exceedingly clever, too. That's just what we've all come to expect from @tvdboom. Surprisingly, this now is beginning to seem like more of a mypy issue rather than a @beartype issue. I blame them! Mypy is bad and should feel bad. Specifically, mypy fails to extend the same semantics applied to standard But... it doesn't. So, that's a mypy bug. Right? I think? I hope? Please let this not be @beartype's responsibility. Odin, hear our cry! 👻 |
Beta Was this translation helpful? Give feedback.
Fascinating... and exceedingly clever, too. That's just what we've all come to expect from @tvdboom.
Surprisingly, this now is beginning to seem like more of a mypy issue rather than a @beartype issue. I blame them! Mypy is bad and should feel bad.
Specifically, mypy fails to extend the same semantics applied to standard
typing
classes to subclasses of those standardtyping
classes. If mypy accepts a function annotated bytyping.Literal[A, B]
for any valid literalsA
andB
, then mypy should also accept the same function annotated byYourLiteral[A, B]
for any subclassclass YourLiteral(typing.Literal):
.But... it doesn't. So, that's a mypy bug. Right? I think? I hope? Please let this not be …