python / python/mypy

allow variables named `_` (underscore) to be untyped (throwaway variables)

Open
#16,786 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

There is a very common practice in python, to use the underscore character - _ - as a variable name for a "throwaway character" - mostly when receiving function output values that will not be used, or when implementing functions with defined signatures, without using some of the variables.

The feature request is to allow these variables to be without type hints, and do not warn.

Pitch

In our company, we work a lot with web development, APIs and such, and it is very common to have to implement predefined function signatures. For example, when using AWS lambdas, one needs to implement:
def lambda_handler(event, context):

However, it is very common to only use the event parameter in the code, and not the context - meaning many times we implement
def lambda_handler(event, _):

when typing the function, I currently do something like this

from typing import Any
def lambda_handler(event: dict, _: Any) -> None:

since I do not necessarily know what the _/context variable type is, and I do not care to invest work in finding it when the variable is not in use. It would be easier and nicer if I could simply do this
def lambda_handler(event: dict, _) -> None:

There are plenty of other examples, similar to this, and so I am sure this feature can be useful to many other people. I think even if the feature were to be configurable with a flag (allow_untyped_underscore or something) that would be very helpful as well.

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

No file, test, or entry point is named. Start by locating mypy's handling of untyped function parameters and existing underscore-related checks; done should mean that an underscore parameter can omit a type hint without a warning, with the intended behavior and any configuration choice clarified.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.