python: "Expression of type '(enum)' cannot be assigned to declared type 'Literal[(enum member)]'"


mypy’s stubgen generates something like this for enums:

import enum
from typing import ClassVar


class StatusCode(enum.IntEnum):
    ALREADY_EXISTS: ClassVar[StatusCode] = ...

But pyright will complain:

error: Expression of type "StatusCode" cannot be assigned to declared type "Literal[StatusCode.ALREADY_EXISTS]"
    "StatusCode" cannot be assigned to type "Literal[StatusCode.ALREADY_EXISTS]" (reportGeneralTypeIssues)

The solution is to just delete the annotation. pyright will infer the right type, as you can see with reveal_type():

import enum


class StatusCode(enum.IntEnum):
    ALREADY_EXISTS = ...

reveal_type(AdbcStatusCode.ALREADY_EXISTS)
# information: Type of "StatusCode.ALREADY_EXISTS" is "Literal[StatusCode.ALREADY_EXISTS]"