Skip to content

reportIncompatibleVariableOverride when narrowing type from ClassVar[str] to ClassVar[Literal["bar"]] in subclasses #5998

Closed

Description

EDIT: could be related to/fixed by #5993?

Describe the bug

error: "SOME_VAR" overrides symbol of same name in class "KlsA"
    Variable is mutable so its type is invariant
      Override type "Literal['bar']" is not the same as base type "str" (reportIncompatibleVariableOverride)

The above error is received for the code provided below where I would expect no error as I narrow the type in a subclass.
Or, am I misunderstanding how I should be narrowing the type for a var that should be set by subclasses and not changed after that point?

from typing import ClassVar, Literal

class KlsA:
    SOME_VAR: ClassVar[str]
    
class KlsB(KlsA):  # error: "SOME_VAR" overrides symbol of same name in class "KlsA"
    SOME_VAR: ClassVar[Literal["bar"]] = "bar"

I checked the same snippet on https://mypy-play.net as a sanity check and it is passing there.

VS Code extension or command-line

command-line

$ npm exec -- pyright --version 
pyright 1.1.328
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions