Skip to content

mypy isn't accepting Mapping[str, object] as TypedDict [parent] #12932

@ShaneNolan

Description

@ShaneNolan

Bug Report
mypy isn't accepting Mapping[str, object] as TypedDict parent.

test.py:18: error: Argument 1 of "do_handle" is incompatible with supertype "IHandle"; supertype defines the argument type as "Mapping[str, object]"
test.py:18: note: This violates the Liskov substitution principle
test.py:18: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides

(A clear and concise description of what the bug is.)

To Reproduce

from abc import ABC, abstractmethod
from typing import Mapping, TypedDict

PayloadType = Mapping[str, object]

class ArgumentType(TypedDict):
    name: str

class ReturnType(TypedDict):
    result: bool

class IHandle(ABC):

    @abstractmethod
    def do_handle(self, name: PayloadType) -> PayloadType:
        ...

class Handler(IHandle):

    def do_handle(self, name: ArgumentType) -> ReturnType:
        return {
            'result': True,
        }

handler = Handler()
name: ArgumentType = {'name': 'example'}
handler.do_handle(name)

Expected Behavior

ArgumentType and ReturnType are both subtypes of TypedDict and TypedDict is a subtype of Mapping[str, object] therefore its not a violation.

Actual Behavior

ArguementType violates the liskov subsition principle whereas ReturnType doesn't?

Your Environment

  • Mypy version used: mypy 0.960 (compiled: yes)
  • Mypy command-line flags: mypy
  • Mypy configuration options from mypy.ini (and other config files): NA.
  • Python version used: Python 3.9.0
  • Operating system and version: Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions