Skip to content

Enum value lookup accepts incompatible argument types without a diagnostic #5398

Open

Description

@Ravencentric

Describe the Bug

Pyrefly accepts None in Enum() where it shouldn't. The same call raises ValueError at runtime.

MRE

import enum
from typing import TYPE_CHECKING, Self

class Category(enum.Enum):
    if TYPE_CHECKING:
        @property
        def value(self) -> str: ...

        def __init__(self, value: str) -> None:
            self._id: str
    else:

        def __init__(self, value: str, id: str) -> None:
            self._value_ = value
            self._id = id

    GAMES = "GAMES", "0_0"
    MUSIC = "MUSIC", "1_1"

    @property
    def id(self) -> str:
        return self._id

    @classmethod
    def _missing_(cls, value: object) -> Self:
        msg = f"'{value}' is not a valid {cls.__name__}"

        if isinstance(value, str):
            value = value.casefold().strip()
            for member in cls:
                if (value == member.value.casefold()) or (value == member.name.casefold()) or (value == member.id):
                    return member
            raise ValueError(msg)
        raise ValueError(msg)

Category("GAMES")
Category(None) # runtime error

mypy gets this right:

main.py:37: error: Argument 1 to "Category" has incompatible type "None"; expected "str"  [arg-type]

playground

Expected behavior

A type error for Category(None), since None is incompatible.

Actual behavior

Pyrefly reports no errors.

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.lZZba8IwGIb_StCLKmirF7vxakPEyXAIboOBULo2dgF7oK0DEf_7vkPaNa1VJlRC8ubL6f2epAuZOvNkfIx2cLJXCPr2uVm48-fF_GX1umTMoRI_zrO5V8gwyU4DDGIv4A_cVWbn3uxfNeDvMc2SFHh3qleip8iKlFhkLPDrTNi2XQ5bl15PaXT4nyVrXaqsdlXAENGNbPn_DDASOsS9kagLuF1nWMdsoF0F9Qksn9aLLXmBSrveCIoTdwKu0Ir1-3Y1JwWVWDF1p6woVa1dxlUp5la1vfVZaR6UEzNi0Ykz-Orh3EjlgJnQHfgHuCjaYEXXGINEeQgz3-961pnUFwuBBhkhPOwO-3GGUHbJiou5Jm2tLrQ1ttgguA3Xqtwnh2AwtEGtUmBNQ4_ciWT0JQE_sYBptCLq8XlU5BjL-a6ojTAUEKqlwiXdFamgvZDGCbGyKWJaf2A4wvUAttpYYqcAvyqZK9tRU1XNl1JfZMe4UJFkhLYR2RcbrhCaNvA4ALaGR3iCADPtXdwHT0jxXRRpPnOcsnuShY6MnSDxc8fo4dCpeD-eooeNGU4khPQcwl5n8wOiNM1kLguqwoP3CxPY2sJjfHBlKpDjQPoJvZjQqB7woRPqFjPduoV0C4kOipuU_gU

(Only applicable for extension issues) IDE Information

No response

Activity

  1. mangeshraut712 commented on Oct 27, 2026

    @mangeshraut712
    Contributor

    #claim

  2. github-actions commented on Oct 27, 2026

    @github-actions

    @mangeshraut712 you've claimed this issue — it's now assigned to you. Thanks for picking it up! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions