python / python/mypy

Documentation: type[BaseClass] accepting subclasses

Open
#20,213 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-mypyc
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Documentation

class BaseClass: pass
class Child1(BaseClass): pass

list[type[BaseClass]] = [BaseClass, Child1]
  • MyPy strict has not issues
  • MyPyC compiled fine
  • But running with MyPyc raises a TypeError: BaseClass expected; got Child1

A. Is there a documentation for this case? or is it an issue?

B. What is the solution for this? Can MyPyC read typing.cast to disable this strict mode?
like [BaseClass, typing.cast(BaseClass, Child1)] ?

C. I saw some TypeVar(bound='') solution but didn't work here.

D. Or do I need to just set as typing.Any to avoid issues?


  • Python 3.14
  • MyPy: 1.18.2

Thanks in advance

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 with the Python reproducer in the issue and compare its behavior under MyPy and MyPyC. Determine whether the subclass rejection is expected, a bug, or a documentation gap, and make the supported solution clear, including whether typing.cast or Any is appropriate.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.