typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

(🐞) `Enum.name` is `Any` in 3.11

Open KotlinIsland opened this issue 3 years ago • 7 comments

from enum import Enum

value: Enum

reveal_type(value)
reveal_type(value.name)
> mypy --python-version 3.10 test.py
test.py:5: note: Revealed type is "enum.Enum"
test.py:6: note: Revealed type is "str"
Success: no issues found in 1 source file

> mypy --python-version 3.11 test.py
test.py:5: note: Revealed type is "enum.Enum"
test.py:6: note: Revealed type is "Any"

Not 100% sure if this is a mypy issue or a typeshed issue,

KotlinIsland avatar Mar 29 '22 01:03 KotlinIsland

I guess mypy doesn't understand the _magic_enum_attr here: https://github.com/python/typeshed/blob/master/stdlib/enum.pyi#L144

JelleZijlstra avatar Mar 29 '22 01:03 JelleZijlstra

Is that intrinsic and needs to be special cased, or should it be handled by default?

KotlinIsland avatar Mar 29 '22 02:03 KotlinIsland

It's an alias for property in the stubs. So on 3.11 we are using a subclass of property (from mypy's perspective), but on 3.10 and lower we are just using an alias. I guess the subclass is what's causing trouble.

JelleZijlstra avatar Mar 29 '22 02:03 JelleZijlstra

Thanks, I'll raise it with mypy, should we close this?

KotlinIsland avatar Mar 29 '22 02:03 KotlinIsland

It does on main, maybe you have an old 3.11 checkout.

https://github.com/python/cpython/blob/788154919c2d843a0a995994bf2aed2d074761ec/Lib/enum.py#L150

JelleZijlstra avatar Mar 29 '22 02:03 JelleZijlstra

I had my 3.10 venv active 😳

KotlinIsland avatar Mar 29 '22 03:03 KotlinIsland

For Enum.name specifically, we can probably just pretend that it still uses types.DynamicClassAttribute in 3.11, as it does in <=3.10. It doesn't make much difference in the stubs, especially when mypy doesn't really support the intricacies of either types.DynamicClassAttribute or enum.property.

Sound good?

AlexWaygood avatar Mar 29 '22 08:03 AlexWaygood