Skip to content

False negatives on protocols with ClassVar attributes #4339

@AlexWaygood

Description

@AlexWaygood

Describe the bug

The following snippet of code should cause a type checker to emit an error. However, pyright sees no issue with it:

from typing import ClassVar, Protocol

class ClassVarProto(Protocol):
    x: ClassVar[str]

class Foo:
    x: ClassVar[str] = "x"

def expects_ClassVarProto(arg: ClassVarProto) -> None: ...

expects_ClassVarProto(Foo())  # should succeed
expects_ClassVarProto(Foo)  # should fail (the object passed in has type `type[ClassVarProto]`, not `ClassVarProto`)

Mypy correctly emits an error on the second call:

error: Argument 1 to "expects_ClassVarProto" has incompatible type "Type[Foo]"; expected "ClassVarProto"  [arg-type]
note: ClassVar protocol member ClassVarProto.x can never be matched by a class object

I've reproduced this on pyright 1.1.284. I discovered the bug in my typeshed PR python/typeshed#9362: several tests I added for that PR pass on mypy, but fail on pyright. (FYI I also discovered an edge case in that PR where pyright appears to have the correct behaviour, but mypy is buggy.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    as designedNot a bug, working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions