Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Better error message for rejected protocol subtyping due to mutability #5752

Closed as not planned
@llchan

Description

@llchan

It appears that Union types for Protocol attributes aren't recognized.

Repro example:

from typing import Type, Union
from typing_extensions import Protocol


class HasFoo(Protocol):
    # foo: str            # works
    foo: Union[str, int]  # fails


def dec(cls: Type[HasFoo]) -> Type[HasFoo]:
    return cls


@dec
class Thing:
    foo = 'abc'

Failing output:
Argument 1 to "dec" has incompatible type "Type[Thing]"; expected "Type[HasFoo]"

Misc info:
Tested with both mypy 0.630 and mypy master (e12be3b), run with --strict

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions