Skip to content

Protocol member expected instance variable, got class variable #10289

@smoofra

Description

@smoofra

Bug Report

This program ought to be valid, because all the protocol Foo is declaring is that the attribute foo is readable and returns an int. This requirement is met by Bar's class variable.

from typing import *

class Foo(Protocol):
    @property
    def foo(self) -> int:
        ...

class Bar:
    foo : ClassVar[int] = 42

x : Foo = Bar()

However, mypy says Protocol member Foo.foo expected instance variable, got class variablemypy.

if you just declare foo : int = 42, mypy accepts it, so clearly it understands that variables qualify as read-only properties, just not class variables.

Your Environment

  • Mypy version used: 0.812
  • Mypy configuration options from mypy.ini (and other config files):
    [mypy]
    ignore_missing_imports = True
    follow_imports = silent
    show_column_numbers = True
    check_untyped_defs = True
    
  • Python version used: 3.8.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions