Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Truthyness inference incorrect for someEnum types #17333

Closed
Labels
bugmypy got something wrong
@Daverball

Description

@Daverball

Bug Report

It looks like the enum plugin incorrectly assumes that__bool__ on enum literals will always returnLiteral[True], while this is the default behavior, it is not correct when someone explicitly overrides__bool__. Moreover this special casing is only applied to enum literals, but not the base enum type, which should behave the same as individual members.

To Reproduce

https://mypy-play.net/?mypy=master&python=3.12&gist=dc60c5c82ba6589e5f1d03654439b05e

importenumfromtypingimportAny,LiteralclassFoo(enum.Enum):X=0classBar(enum.Enum):X=0def__bool__(self)->Literal[False]:returnFalsedefa(x:Any|Literal[Foo.X]):reveal_type(x)ifx:reveal_type(x)else:reveal_type(x)defb(x:Any|Foo):reveal_type(x)ifx:reveal_type(x)else:reveal_type(x)# wrong, doesn't match literal behaviordefc(x:Any|Literal[Bar.X]):reveal_type(x)ifx:reveal_type(x)# wrong, it's backwardselse:reveal_type(x)# wrong, it's backwardsdefd(x:Any|Bar):reveal_type(x)ifx:reveal_type(x)else:reveal_type(x)

Your Environment

  • Mypy version used: Playground with master branch
  • Mypy command-line flags: Default
  • Mypy configuration options frommypy.ini (and other config files): Default
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp