python / python/mypy

mypy doesnt use unitest.TestCase.assertIsInstance checks for type narrowing

Open
#13,038 7 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-type-narrowing
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Apologies if i'm using the wrong words to describe what i'm trying to recommend/request.

Feature

At the moment, mypy recommends type narrowing using asserts. For example, if something is Optional[None], add assert something is not None to help mypy type check the statements that follow the assert.

In tests, instead of adding an additional assert, can mypy use a unittest.TestCase.assertIsInstance check?

Pitch

Most unittest-based tests already make use of the assertIsInstance check so we can prevent the need to add an additional assert or the need to convert the assertIsInstance into a assert isinstance(something) is sometype.

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 in the issue. Start by tracing mypy's existing handling of unittest.TestCase.assertIsInstance and its type-narrowing logic, then identify the appropriate regression-test area. Done means supported assertIsInstance checks narrow types without an additional assert and are covered by tests.

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
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.