Skip to content
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

Narrowing enums causes .value to be typed as Any #14092

Open
kmurphy4 opened this issue Nov 14, 2022 · 5 comments · May be fixed by #15636
Open

Narrowing enums causes .value to be typed as Any #14092

kmurphy4 opened this issue Nov 14, 2022 · 5 comments · May be fixed by #15636
Labels
bug mypy got something wrong topic-enum

Comments

@kmurphy4
Copy link

Bug Report

Apologies if this has already been reported, but I couldn't find an existing issue for it.

The issue I'm seeing is related to accessing the .value of an Enum after "narrowing" it with the is operator. It seems like the narrowing (to Union[Literal[...]]) works as expected, but then it fails to realize that the .value of all the variants have the same type (and so falls back to Any).

This only affects runs with --warn-return-any enabled:

$ mypy --config-file /dev/null --warn-return-any enum_narrowing.py

To Reproduce

It's probably easiest to see in this example:

import enum
from typing import Tuple

RGB = Tuple[int, int, int]

class Color(enum.Enum):
    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)

    def red(self) -> int:
        if self is Color.WHITE:
            return 255
        return self.value[0]  # error: Returning Any from function declared to return "int"

(playground URL: https://mypy-play.net/?mypy=latest&python=3.8&flags=warn-return-any&gist=b2a6df92fb37717e5c9d7542b6e88fec)

Expected Behavior

This should deduce that self.value has type RGB and succeed.

Actual Behavior

If I add some reveal_type(self) and reveal_type(self.value)s before/after the narrowing, I get this output:

enum_narrowing.py:11: note: Revealed type is "enum_narrowing.Color"
enum_narrowing.py:12: note: Revealed type is "Tuple[Literal[255]?, Literal[255]?, Literal[255]?]"
enum_narrowing.py:15: note: Revealed type is "Literal[enum_narrowing.Color.BLACK]"
enum_narrowing.py:16: note: Revealed type is "Any"

Your Environment

$ python --version
Python 3.8.0
$ mypy --version
mypy 0.991 (compiled: yes)
@kmurphy4 kmurphy4 added the bug mypy got something wrong label Nov 14, 2022
@A5rocks
Copy link
Contributor

A5rocks commented Nov 15, 2022

enum_narrowing.py:12: note: Revealed type is "Tuple[Literal[255]?, Literal[255]?, Literal[255]?]"

This seems wrong too (another bug?) though I forgot what ? means.

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Jul 10, 2023

@A5rocks the question mark at the end reflects this context-sensitive nature., basically means it's inferred to be literal, and not declared with Literal.

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Jul 10, 2023

Another example:

from enum import Enum

class E(Enum):
    A = 1
    B = 2

e: E

print(e.value)  # no error
if e is E.A:
    print(e.value)  # Expression has type "Any"  [misc]

@A5rocks
Copy link
Contributor

A5rocks commented Jul 11, 2023

from enum import Enum
from typing import Literal

class E(Enum):
    A = 1

x: Literal[E.A]
reveal_type(x.value)  # N: Revealed type is "Any"

Turns out there's a bigger issue here :/

@A5rocks A5rocks linked a pull request Jul 11, 2023 that will close this issue
@A5rocks
Copy link
Contributor

A5rocks commented Jul 11, 2023

Thanks for making this pop back up in my notifications @KotlinIsland! Turns out it was simpler than I expected :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-enum
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants