Skip to content

Subclasses of mailbox.Mailbox not recognized as such #14935

Closed

Description

@jwodder

Consider the following code:

from abc import ABC, abstractmethod
import mailbox


class MailboxFactory(ABC):
    @abstractmethod
    def open(self) -> mailbox.Mailbox: ...


class MboxFactory(MailboxFactory):
    def __init__(self, path: str) -> None:
        self.path = path

    def open(self) -> mailbox.mbox:
        return mailbox.mbox(self.path)

I'm 90% sure this should be acceptable, but running 1.18.2 on this code produces the error:

mailbox02.py:14: error: Return type "mbox" of "open" incompatible with return type "Mailbox[Message[str, str]]" in supertype "MailboxFactory"  [override]
Found 1 error in 1 file (checked 1 source file)

Similar errors happen for other Mailbox subclasses as well.

Activity

  1. srittau commented on Oct 30, 2025

    @srittau
    Collaborator

    The problem is that Mailbox is generic over the message type (_MessageT), which is invariant and defaults to Message[str, str]. Easy workaround is to use mailbox.Mailbox[Any], although we should probably change the default for Mailbox in typeshed for cases like this.

  2. srittau commented on Oct 30, 2025

    @srittau
    Collaborator

    In fact, we can make the type variable covariant in typeshed, which should fix this problem.

  3. added a commit that references this issue on Oct 30, 2025
    92144d2
  4. added a commit that references this issue on May 2, 2026
    7c83799
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions