python / python/mypy

`ClassVar` cannot be overridden with `Final`

Open
#20,183 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

A typing.ClassVar cannot be overridden with typing.Final.

main.py:7: error: Cannot override class variable (previously declared on base class "Base") with instance variable  [misc]
Found 1 error in 1 file (checked 1 source file)

To Reproduce

import typing

class Base:
    value : typing.ClassVar[str]
    
class Derived(Base):
    value : typing.Final[str] = 'derived'

https://mypy-play.net/?mypy=latest&python=3.12&flags=verbose&gist=03819c6e8c7b6fb53baebe094faed123

Expected Behavior

I though using a typing.Final annotation would imply typing.ClassVar.

Actual Behavior

It seems not allowed to override a ClassVar with a Final.

Your Environment

See reproducer.

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 provided Python reproducer and trace how mypy handles overriding a ClassVar with a Final annotation. Confirm the intended typing behavior and add coverage for this case; done means the reproducer no longer reports the incorrect override error and existing checks remain passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.