python / python/mypy

fail typechecking if a parameterized type doesn't declare `__class_getitem__`

Open
#9,712 1 comment 0 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

a parameterized (generic) class/type should fail to typecheck if it doesn't have the __class_getitem__ method

pitch

the syntax for parameterized types is SomeClass[int]. however, if SomeClass doesn't define the __class_getitem__ method, this will typecheck but not actually run properly. we ran into this unexpectedly over at typeddjango/django-stubs/issues/507

it would be extremely helpful to at least have a command line option to warn/error on this, just so that nobody runs into it unexpectedly

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 locating the type-checking path for parameterized classes and the existing handling of class_getitem, then inspect nearby tests and command-line option patterns. Done means parameterized types without class_getitem produce the agreed warning or error, with coverage for the behavior and its option.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.