Request: Optional config to warn when a parameter annotation could use `Mapping` instead of `Dict` or `Sequence` instead of `List`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
- Are you reporting a bug, or opening a feature request?
Feature request
Basically, I think it would be nice if mypy could warn when the user could be giving their function immutable versions of the argument types instead of mutable ones. For example:
def len_greater_than_three(input_list: List[int]) -> bool: # Warning: Could use read only type Sequence instead of List for parameter 1.
return len(input_list) > 3
Being prompted to use immutable types when you're not mutating the input could improve the usability of mypy quite a bit since it would result in functions who's parameters are covariant much more often. It's also convenient to know when a function isn't mutating its arguments based on its annotation, and the desire to default to using immutable types wherever possible probably makes sense for a lot of users.
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 issue #8484 and mypy's existing diagnostic and configuration conventions. Define the optional warning behavior for parameters annotated with mutable types, then verify that the example is reported only when the parameter is not mutated and that the setting is configurable.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100