-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
(🐞) Enum.name
is Any
in 3.11 / support enum.property
#12483
Comments
Enum.name
is Any
in 3.11 / support enum.property
Enum.name
/Enum.value
is Any
in 3.11 / support enum.property
Enum.name
/Enum.value
is Any
in 3.11 / support enum.property
Enum.name
is Any
in 3.11 / support enum.property
To paraphase in a way others won't need to dig throuhg typeshed, typeshed does this:
|
And |
It might be worth pointing out that mypy doesn't get the behaviour precisely correct on Python <=3.10 either: from enum import Enum
class Foo(Enum):
BAR = 'bar'
BAZ = 'baz'
reveal_type(Foo.name) # No error from mypy, raises AttributeError at runtime
reveal_type(Foo.value) # No error from mypy, raises AttributeError at runtime On Python <= 3.10, >>> from enum import Enum
>>> class Dictionary(Enum):
... name = 'a word used to identify something'
... animal = 'a living creature'
...
>>> Dictionary.name
<Dictionary.name: 'a word used to identify something'>
>>> Dictionary.name.name
'name' In Python 3.11, >>> import enum
>>> class CustomEnum(enum.Enum):
... @enum.property
... def foo(self):
... return 'MAGIC'
...
>>> class MyEnum(CustomEnum):
... foo = 'foo'
...
>>> MyEnum.foo
<MyEnum.foo: 'foo'>
>>> MyEnum.foo.foo
'MAGIC' |
Here's the dedicated issue regarding the incorrect behaviour on <=3.10: #4654 |
mypy has a bug when running on/for 3.11 which results in incorrect analysis of the codebase, specifically due to enum.Enum's self.name See: python/typeshed#7564 python/mypy#12483
mypy has a bug when running on/for 3.11 which results in incorrect analysis of the codebase, specifically due to enum.Enum's self.name See: python/typeshed#7564 python/mypy#12483
Hope this is proper place to report, as I have experienced another issue related to With Python 3.10 following code checked OK, from enum import Enum, unique
@unique
class SortEnum(Enum):
name = "name"
name_desc = "-name"
But with Python 3.11 it throwing,
As a quick workaround it might be possible to rename |
Fixes #12483 Fixes python/typeshed#7564 Ref #12841 The fix is straightforward. I can't use a unit test for this because there are some builtins fixtures that don't have tuple, so I can't do version check.
mypy has a bug when running on/for 3.11 which results in incorrect analysis of the codebase, specifically due to enum.Enum's self.name See: python/typeshed#7564 python/mypy#12483
from enum import Enum, auto
class E(Enum):
name = auto() $ mypy /tmp/foo.py
/tmp/foo.py:5: error: Incompatible types in assignment (expression has type "auto", base class "Enum" defined the type as "str") [assignment]
Found 1 error in 1 file (checked 1 source file) (Not a problem for me btw — I just happened to notice.) $ python --version
Python 3.11.1
$ mypy --version
mypy 1.0.0 (compiled: yes) |
Fix some mypy errors in the reggen library. It looks like the version of MyPy that we're using in CI at the moment isn't new enough to notice the problems, so we won't really see much change from this commit. However, these changes fix things with 1.0.1 (the latest release), which spat out lots of messages without them. Getting things happy again is mostly a case of adding some types to guide the tool so that it knows what's going on. There's also a small re-write in gen_selfdoc.py: there, the doc_tbl_head() function used an optional int as a way of encoding a bool. This seemed a bit silly (and hard to encode!), so I've switched it to use True/False instead. Finally, we fix a type error in register.py, where the code was missing some parentheses and testing a function (rather than its return value) against None. One slightly odd thing is the change to access.py. There, the problem was that self.value[1].name wasn't known to be a string. It turns out that this depends on the Python version(!) and is tracked in python/mypy#12483. Using a do-nothing cast with str() silences things. Signed-off-by: Rupert Swarbrick <[email protected]>
Fix some mypy errors in the reggen library. It looks like the version of MyPy that we're using in CI at the moment isn't new enough to notice the problems, so we won't really see much change from this commit. However, these changes fix things with 1.0.1 (the latest release), which spat out lots of messages without them. Getting things happy again is mostly a case of adding some types to guide the tool so that it knows what's going on. There's also a small re-write in gen_selfdoc.py: there, the doc_tbl_head() function used an optional int as a way of encoding a bool. This seemed a bit silly (and hard to encode!), so I've switched it to use True/False instead. Finally, we fix a type error in register.py, where the code was missing some parentheses and testing a function (rather than its return value) against None. One slightly odd thing is the change to access.py. There, the problem was that self.value[1].name wasn't known to be a string. It turns out that this depends on the Python version(!) and is tracked in python/mypy#12483. Using a do-nothing cast with str() silences things. Signed-off-by: Rupert Swarbrick <[email protected]>
Fix some mypy errors in the reggen library. It looks like the version of MyPy that we're using in CI at the moment isn't new enough to notice the problems, so we won't really see much change from this commit. However, these changes fix things with 1.0.1 (the latest release), which spat out lots of messages without them. Getting things happy again is mostly a case of adding some types to guide the tool so that it knows what's going on. There's also a small re-write in gen_selfdoc.py: there, the doc_tbl_head() function used an optional int as a way of encoding a bool. This seemed a bit silly (and hard to encode!), so I've switched it to use True/False instead. Finally, we fix a type error in register.py, where the code was missing some parentheses and testing a function (rather than its return value) against None. One slightly odd thing is the change to access.py. There, the problem was that self.value[1].name wasn't known to be a string. It turns out that this depends on the Python version(!) and is tracked in python/mypy#12483. Using a do-nothing cast with str() silences things. Signed-off-by: Rupert Swarbrick <[email protected]>
in 3.11
name
is decorated withenum.property
which is a subtype oftypes.DynamicClassAttribute
(aka,builtins.property
)from python/typeshed#7564
Related #12220
The text was updated successfully, but these errors were encountered: