Skip to content

Invalid type for inverted IntFlag #13853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zoola969 opened this issue Apr 16, 2025 · 6 comments · May be fixed by #13854
Open

Invalid type for inverted IntFlag #13853

zoola969 opened this issue Apr 16, 2025 · 6 comments · May be fixed by #13854

Comments

@zoola969
Copy link

zoola969 commented Apr 16, 2025

Bug Report

A type of inverted IntFlag is revealed as int instead of IntFlag

To Reproduce

from enum import IntFlag


class F(IntFlag):
    A = 1
    B = 2


reveal_type(F.A | F.B)
reveal_type(~F.A)

Expected Behavior

Revealed type is "F"
Revealed type is "F"

Actual Behavior

Revealed type is "F"
Revealed type is "builtins.int"

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags:
  • Mypy configuration options from pyproject.toml:
[tool.mypy]
check_untyped_defs = true
enable_error_code = "explicit-override,unused-awaitable,ignore-without-code,truthy-bool,possibly-undefined,redundant-expr,redundant-self"
plugins = ["pydantic.mypy"]
pretty = true
show_error_context = true
strict = true
warn_unreachable = true

[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = true
warn_required_dynamic_aliases = true
  • Python version used:

3.13

@vidhyavijayan3
Copy link

Hi @zoola969

Can I work on this issue?

@brianschubert
Copy link
Contributor

@vidhyavijayan3 Please, feel free! You can check out CONTRIBUTING.md and the Developer Guides for contributing guidelines and tips on where to start. Thanks!

@erictraut
Copy link
Contributor

I think this is a bug in typeshed, not in mypy. The typeshed definition for IntFlag includes overrides for __or__, __and__, and __xor__, but it omits __invert__. To fix it, you could submit a PR to typeshed.

@vidhyavijayan3
Copy link

Hi @brianschubert @erictraut,

Thank you both for the helpful responses! I'll take a closer look at the IntFlag definition in typeshed and work on submitting a PR to add the missing invert method. I really appreciate the guidance and I'm looking forward to contributing!

@A5rocks
Copy link
Contributor

A5rocks commented Apr 18, 2025

@hauntsaninja it might make sense to transfer this to the typeshed issue tracker.

@vidhyavijayan3 I think you just need to add a method here:

typeshed/stdlib/enum.pyi

Lines 227 to 231 in 1b267b2

class IntEnum(int, _IntEnumBase):
_value_: int
@_magic_enum_attr
def value(self) -> int: ...
def __new__(cls, value: int) -> Self: ...

@hauntsaninja hauntsaninja transferred this issue from python/mypy Apr 18, 2025
@vidhyavijayan3
Copy link

Hi @A5rocks, thank you for the helpful pointer! I’ll go ahead and update the enum.pyi file in typeshed to add the missing invert method.

Also, thanks @hauntsaninja for transferring the issue. That definitely makes sense. Looking forward to contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants