Skip to content

Suggest making protocol attribute read-only if invariance causes conflict #6002

@JukkaL

Description

@JukkaL

If a concrete attribute has a narrower type than protocol attribute, mypy will complain (adapted from #5998):

prog.py:59: error: Argument 1 to "f" has incompatible type "C"; expected "Proto"
prog.py:59: note: Following member(s) of "C" have conflicts:
prog.py:59: note:     name: expected "Optional[str]", got "str"

It could help if mypy would suggest making the protocol attribute read-only (property or final attribute), which is often the right thing to do.

We already generate a related suggestion to use a covariant collection type when using an invariant collection causes type incompatibility.

Metadata

Metadata

Assignees

No one assigned
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions