python / python/mypy

Many classes should be decorated as `@final`

Open
#11,179 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

While working on several mypy plugins I had often step on the fact you cannot subclass any of the mypy classes, because they are compiled with mypyc. It caused runtime TypeError in the plugin's code.

But, when I was trying to run mypy on the plugin's code - it was fine, no error was shown. Because type are not @final and mypy is not able to understand that this is a problem.

So, I propose to add explicit @final annotations to almost all types, exception:

  • Types with allow_interpreted_subclasses
  • Types that are subclassed inside mypy itself: Node, Expression, TypeQuery, etc

One more strong point to making this change is that many classes are not "designed" to be subclassed.
So, this should not be a problem.

Related https://github.com/python/mypy/pull/10125
Related https://github.com/python/mypy/pull/9602

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 files, tests, or entry points. Start by locating mypy's class definitions and identifying the stated exceptions, including types with allow_interpreted_subclasses and classes subclassed within mypy. Done means the applicable classes are annotated with @final and plugin code is checked accordingly.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.