python / python/mypy

Consider allowing `@final` methods in Protocols

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

Nobody has claimed this yet.

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

Description

Feature

Allow this:

from typing import Protocol, final

class Sized(Protocol):
    len: int
    @final
    def __len__(self) -> int:
        return self.len

Pitch

I get that this is a bit unusual, but I don’t really see any harm that could come from this (as an aside, pyright allows it). My use case is that I’m using Protocols for mixins, and sometimes I want to make sure my mixed-in methods don’t get overwritten.

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 reported Python example and compare mypy’s current handling with pyright, which the issue says allows it. Trace the existing Protocol and @final validation paths, then add coverage showing that a final method in a Protocol is accepted and remains protected from overrides.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.