python / python/mypy

Should non-overlapping comparisons allowed in assertions?

Open
#8,294 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs discussion topic-overlap
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

The following assertion generates an error when using --strict-equality, but perhaps it should be allowed, since an assertion can test something that should be impossible?

from typing import List

def f(x: List[int]) -> None:
    assert 'foo' not in x  # Error, but should this be okay?
    ...

I saw a few examples similar to the above in production code and had to use # type: ignore[...] with them, which didn't feel optimal.

@ilevkivskyi What do you think?

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

The issue names no file or test; first reproduce the example with --strict-equality and trace the assertion check that reports the comparison error. Decide whether an impossible membership assertion should be accepted, then define the expected diagnostic behavior and coverage for the shown List[int] case.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.