Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protocol member expected instance variable, got class variable #10289

Closed
smoofra opened this issue Apr 6, 2021 · 0 comments · Fixed by #14081
Closed

Protocol member expected instance variable, got class variable #10289

smoofra opened this issue Apr 6, 2021 · 0 comments · Fixed by #14081
Labels
bug mypy got something wrong topic-protocols

Comments

@smoofra
Copy link

smoofra commented Apr 6, 2021

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
@smoofra smoofra added the bug mypy got something wrong label Apr 6, 2021
ilevkivskyi added a commit that referenced this issue Nov 16, 2022
Fixes #10289

Unless I am missing something, this indeed looks safe, so I am going to
allow this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-protocols
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants