-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Description
Consider the following (simplified) typeshed definition for email.feedparser.FeedParser:
class FeedParser:
def __init__(self, _factory: Callable[[], Message] = ...) -> None: ...
def close(self) -> Message: ...FeedParser allows a message factory to be passed in and will use it to construct the return value to close(). Unfortunately you have to cast the return value of close() to the correct type:
parser = FeedParser(MyMessage)
message = cast(MyMessage, parser.close())The following type definition fixes this problem:
M = TypeVar("M", bound=Message)
class FeedParser(Generic[M]):
def __init__(self, _factory: Callable[[], M] = ...) -> None: ...
def close(self) -> M: ...Now the example above does not require the cast to make mypy happy. But unfortunately using the default factory will not work anymore:
parser = FeedParser()
message = parser.close()This will cause mypy to complain error: Need type annotation for variable. Is there any solution for this conundrum?
discosultan, michaeloliverx, JaviFuentes94, itaisteinherz, mscuthbert and 3 more