Skip to content

enum.IntFlag.name is typed as str|None #16177

@adaamz

Description

@adaamz

Hello,
I would like to ask/report bug whether it is possible to have (enum.IntFlag)().name as type None or if it is incorrectly typed as str|None.

Bug Report
Looks like (enum.IntFlag)().name is typed as str|None but I think it should be just str.

To Reproduce

import enum


class ListType(enum.IntFlag):
    ABC = enum.auto()
    DEF = enum.auto()
    GHI = enum.auto()
    
    def get_names(self) -> list[str]:
        to_return = []

        for e in ListType:
            reveal_type(e)   # note: Revealed type is "__main__.ListType"
            reveal_type(e.name)  # note: Revealed type is "Union[builtins.str, None]"
            to_return.append(e.name)

        return to_return  # error: Incompatible return value type (got "list[str | None]", expected "list[str]")  [return-value]

https://onlinegdb.com/Rsv6ryZzf
https://mypy-play.net/?mypy=latest&python=3.11&gist=61af435c15e57ce5221abada2221008a

Expected Behavior
I would expect that (enum.IntFlag)().name is always string.

Success: no issues found in 1 source file

Actual Behavior

main.py:19: error: Incompatible return value type (got "list[str | None]", expected "list[str]")  [return-value]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.5.1
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9, 3.10, 3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions