Skip to content

Can't bind TypeVar to Protocol and other TypeVars #5939

@iddan

Description

@iddan
  • Are you reporting a bug, or opening a feature request? Bug
  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
import inspect
from typing import cast, Type, TypeVar, Callable
from typing_extensions import Protocol

ImplementedProtocol = TypeVar("ImplementedProtocol", bound=Protocol)
BaseClass = TypeVar("BaseClass", bound=Type)
Implementation = TypeVar("Implementation", bound=BaseClass)

def implement(protocol : ImplementedProtocol) -> Callable[[Implementation], Implementation]:
  def x(implementation: Implementation) -> Implementation:
    methods = inspect.getmembers(implementation, predicate=inspect.isfunction)
    mro = inspect.getmro(implementation)
    
    if len(mro) < 2:
        raise TypeError("implementation must extend a base class to enrich")

    base_class : BaseClass = inspect.getmro(implementation)[1]

    for name, function in methods:
      setattr(base_class, name, function)
    
    cast(ImplementedProtocol, base_class)
    
    return implementation

  return x

class Animal(Protocol):
  def name(self) -> str:
    pass

  def noise(self) -> str:
    pass

class Sheep:
  def __init__(self, name, noise):
    self._naked = False
    self._noise = noise
    self._name = name

  def is_naked(self) -> bool:
    return self._naked
  
  def sheer(self) -> None:
    self._naked = True

@implement(Animal)
class SheepAnimalExtension(Sheep):
    def name(self):
      return self._name

    def noise(self):
      return self._noise


sheep = Sheep(name="beee", noise="mee")
print(sheep.name())
  • What is the actual behavior/output?
traits/trait.py:6: error: Invalid type "traits.trait.BaseClass"
traits/trait.py:16: error: Invalid type "traits.trait.BaseClass"
traits/trait.py:21: error: Invalid type "protocol"
traits/usage.py:33: error: "Sheep" has no attribute "name"; maybe "_name"?
  • What is the behavior/output you expect? Sheep to be casted to SheepAnimalExtension
  • What are the versions of mypy and Python you are using? 0.641
    Do you see the same issue after installing mypy from Git master? yes
  • What are the mypy flags you are using? (For example --strict-optional) None
  • If mypy crashed with a traceback, please paste
    the full traceback below. no crash

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