Skip to content

Issue with generic Protocol #12183

Closed
Closed
@bloussou

Description

@bloussou
from dataclasses import dataclass
from typing import Generic, Protocol, TypeVar


import pandas as pd

_DataFrameBound = TypeVar("_DataFrameBound", bound=pd.DataFrame, covariant=True)


class _Method(Protocol[_DataFrameBound]):
    def __call__(self, dataframe: pd.DataFrame) -> _DataFrameBound:
        ...

@dataclass
class MyClass(Generic[_DataFrameBound]):
    _method: _Method[_DataFrameBound]

@dataclass
class MyClassDataFrame(MyClass[pd.DataFrame]):
    ...

def function(dataframe: pd.DataFrame) -> pd.DataFrame:
    return pd.DataFrame()

my_class = MyClassDataFrame(function)

is failing with

error: Argument 1 to "MyClassDataFrame" has incompatible type "Callable[[DataFrame], DataFrame]"; expected "_Method[_DataFrameBound]"  [arg-type]

and Pyright see it as a valid code so I think it should be seen as valid by mypy.

  • Mypy version used: 0.931
  • Mypy command-line flags: poetry run mypy --no-warn-unused-ignores --show-error-codes
  • Mypy configuration options from mypy.ini (and other config files): strict = true
  • Python version used: 3.8.10
  • Operating system and version: Ubuntu 21.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions