Signature inference for special methods
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
We have the following
class A:
def __contains__(self, x: int) -> bool:
return None # incompatible return value type
class B:
def __contains__(self, x: int):
return None # no error?
Currently, in order to type-check the return type of special methods such as __contains__ or __len__, it is necessary to explicitly type-annotate them, even though it ought to be "obvious" i.e. bool and int respectively.
Suggestion: Add a configuration like automatic-signature-special-methods that will behave as if every special methods are annotated with the correct types. Together with check-untyped-defs (or maybe a lighter configuration like check-untyped-special-methods)
Note that currently Python official documentation has this statement, regarding type annotation for special methods (https://typing.python.org/en/latest/spec/annotations.html)
(Note that the return type of init ought to be annotated with -> None. The reason for this is subtle. If init assumed a return annotation of -> None, would that mean that an argument-less, un-annotated init method should still be type-checked? Rather than leaving this ambiguous or introducing an exception to the exception, we simply say that init ought to have a return annotation; the default behavior is thus the same as for other methods.)
The advantages are
- Avoid mistakes in annotating these methods https://github.com/sagemath/sage/issues/41500
- Less code to read, less mental overhead
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
No source files or tests are named in the issue. Start by tracing how mypy infers signatures for special methods and how check-untyped-defs is configured; compare the proposed automatic-signature-special-methods and check-untyped-special-methods behaviors. Done means the supported special methods and configuration interaction are defined, with return-type checking demonstrated for the examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100