-
Notifications
You must be signed in to change notification settings - Fork 276
Closed
Labels
Description
Describe the Bug
Hi,
I'd expect the widen_graph function below to pass
the type check. However, the linter says
Returned type `Graph[SpecialNode]` is not assignable to declared return type `Graph[Node]`
Protocol `SpecialNode` requires attribute `type`
From my understanding Mapping should be covariant
in V and hence my Graph should be covariant
in N.
Weirdly enough, the example actually does pass the type check, if I specify my own Mapping protocol (example code can be found in the sandbox).
Is this intended behaviour or a bug?
from collections.abc import Mapping
from typing import Protocol, Self
class Node(Protocol):
@property
def name(self) -> str:...
class Graph[N: Node](Protocol):
def add_node(self, n: Node) -> Self: ...
def nodes(self) -> Mapping[str, N]: ...
class SpecialNode(Node, Protocol):
@property
def type(self) -> str: ...
def widen_graph(g: Graph[SpecialNode]) -> Graph[Node]:
return gEdit: Using TypeVar to mark N explicitly as covariant makes the type check pass as well.
Sandbox Link
(Only applicable for extension issues) IDE Information
No response
Reactions are currently unavailable