python / python/mypy

Instance attributes specified in protocols should not be usable as class attributes in inheriting concrete classes

Open
#9,118 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Are you reporting a bug, or opening a feature request?

Bug

Minimal repro
from typing import Protocol

class SomeProtocol(Protocol):
    y : int
    x : int = 3

class Class(SomeProtocol):
    pass

def test1() -> None:
    y: int = Class.y  # runtime error. mypy should catch it?

def test2() -> None:
    x: int = Class.x  # runs fine, but should probably be a mypy error?
What is the actual behavior/output?

mypy reports no errors.

What is the behavior/output you expect?

I would expect mypy to complain that Class does not have class attributes x nor y (they are not marked with ClassVar).

What are the versions of mypy and Python you are using?

mypy: 0.782
python: 3.8.3

What are the mypy flags you are using? (For example --strict-optional)

No flags

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how mypy handles attribute access on concrete subclasses of Protocol; the issue names no source file or existing test. Add a regression test based on the supplied SomeProtocol/Class example, and consider it done when accesses to both x and y are rejected because they lack ClassVar.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.