method `self` type appears to be unknown (Flag, Python 3.11)
Open
Nobody has claimed this yet.
bug
topic-enum
topic-self-types
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
In a method of a class derived from Flag, the type of self appears to be unknown.
To Reproduce
import enum
class Foo(enum.Flag):
A = 1
B = 2
def names(self):
return [v.name for v in list(self.__class__) if self & v]
Expected Behavior
no errors
Actual Behavior
$ mypy --check-untyped-defs flag.py
flag.py:8: error: "object" has no attribute "name" [attr-defined]
flag.py:8: error: Unsupported operand types for & ("Foo" and "object") [operator]
Though I can resolve the error by annotating self with typing_extensions.Self, I'm not sure that should be necessary.
Your Environment
- Mypy version used: 1.2.0
- Python version used: 3.11.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the flag.py reproducer and run it using mypy --check-untyped-defs to confirm the reported errors. Trace how mypy types list(self.__class__) and the Flag operations, then add a regression test showing that the method produces no errors without annotating self.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100