Generate an optional error if __init__ has no annotation but class has annotations
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
If __init__ has no annotation, it's likely that some attributes have implicit Any types, which seriously compromises type checking. This is a common mistake. Example where this is a problem:
class C:
def __init__(self):
self.a = [] # Inferred type for 'a' is Any, since no annotation
def method(self) -> str:
return self.a # No error, since Any is compatible with str
Add an optional check to generate an error if __init__ has no type annotation but some attribute or method defined in the class body has a type annotation. The check would be enabled using --enable-error-code <code>.
Only look at definitions within the class body and ignore inherited __init__ and other inherited members.
This can be a little tricky to support in the daemon, since we'll need to add a dependency from every attribute to __init__ when the error code is enabled, if __init__ has no annotation (assuming this property is checked as part of checking __init__), and we need to keep track of whether each attribute has an annotation or not -- I'm not sure if the daemon keeps track of this or not.
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 by tracing how mypy checks an unannotated init and how the daemon tracks dependencies between class definitions. The optional error should activate through --enable-error-code, inspect only attributes and methods defined in the class body, ignore inherited members, and report when the class has annotations but init lacks one.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100