Don't require annotations of kwargs initialized to bool, float, int or string constants
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
See PR: #12918 (draft).
Feature
There should be no need for annotations for kwargs in the following (partial) function definitions:
def bool_args(ok=True, die=False)
def int_arg(i=1)
def float_arg(ratio=1.0)
def string_arg(s="Hello World")
In this example, the natural annotations are as follows:
def bool_args(ok: bool=True, die: bool=False): pass
def int_arg(i:int=1): pass
def float_arg(ratio: float=1.0): pass
def string_arg(s:str="Hello World"): pass
The issue proposes a non-trivial change to mypy's type inference. The proposal suggests that mypy act as if a natural annotation exists for an initialized kwarg. Note: mypy will (must!) check the new (natural) annotation, and mypy must complain if the natural annotation is not correct.
Pitch
At present, with disallow_untyped_defs = True and disallow_incomplete_defs = False, mypy complains that the signatures above lack annotations for the kinds of kwargs shown above. Such annotations unnecessarily clutter code. It should be possible to infer the types of the kwargs based on the obvious types of the constants!
This feature would fix my biggest gripe about mypy :-)
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 reviewing draft PR #12918 and the issue's examples of natural annotations for keyword arguments initialized with bool, int, float, and string constants. Trace the relevant type-inference behavior and verify that inferred annotations are checked for correctness. Done means these cases satisfy disallow_untyped_defs without explicit annotations while invalid defaults still produce errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100