python / python/mypy

Request: Optional config to warn when a parameter annotation could use `Mapping` instead of `Dict` or `Sequence` instead of `List`

Open
#8,484 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-2-low topic-usability
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.