Skip to content

Suggest making a protocol member read-only on invariance conflict#21723

Open
apoorva-01 wants to merge 1 commit into
python:masterfrom
apoorva-01:fix-6002-protocol-readonly-note
Open

Suggest making a protocol member read-only on invariance conflict#21723
apoorva-01 wants to merge 1 commit into
python:masterfrom
apoorva-01:fix-6002-protocol-readonly-note

Conversation

@apoorva-01

Copy link
Copy Markdown

Fixes #6002

A mutable protocol attr with a narrower concrete type only fails because of invariance, but the error never says that. Adds a note right after "expected X, got Y":

Consider making "name" in "Proto" read-only, e.g. by using a read-only property, since a mutable protocol member is invariant -- see <docs link>

Only when read-only would actually fix it, so a real int/str mismatch stays quiet. Same idea as the covariant-collection note, plus the docs link ilevkivskyi wanted.

When a mutable protocol attribute is rejected only because it's invariant and
the concrete type is narrower, point at making it read-only, like the existing
covariant-collection note.

Fixes python#6002.
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest making protocol attribute read-only if invariance causes conflict

1 participant