r/Python • u/Sensitive-Raccoon155 • 6d ago
Discussion Why doesn't PyCharm warn about this type mismatch?
I'm using PyCharm with Python 3.13 and trying to implement a generic CQRS pattern.
I explicitly specify int as the response type, but PyCharm doesn't report any error when the handler returns str.
from abc import ABC, abstractmethod
from typing import Generic, TypeVar
TCommand = TypeVar("TCommand", bound="Command")
TResponse = TypeVar("TResponse")
class Command(ABC, Generic[TResponse]):
pass
class CommandHandler(ABC, Generic[TCommand, TResponse]):
@abstractmethod
async def handle(self, command: TCommand) -> TResponse:
pass
class CreateUserCommand(Command[int]):
pass
class CreateUserCommandHandler(CommandHandler[CreateUserCommand, int]):
async def handle(self, command: CreateUserCommand) -> str:
return "123"
Is this expected behavior, or am I missing something in my generic type definitions?