Skip to content

Final variable is mutable when using structural subtyping #10850

@Tomatosoup97

Description

@Tomatosoup97

Bug Report

Under structural subtyping with a variable narrowed to Final, it's possible to modify the var contents

To Reproduce

# poc.py
from dataclasses import dataclass
from typing import Protocol, Final

@dataclass
class FinalY:
    y: Final[int] = 0

class Y(Protocol):
    y: int

def f(x: Y) -> Y:
    x.y = 42
    return x

print(f(FinalY(17)))

$ mypy poc.py
Success: no issues found in 1 source file

$ python poc.py
FinalY(y=42)

Expected Behavior

Class with a variable Final[T] should not be a structural subtype of a protocol with variable T

Your Environment

  • Mypy version used: 0.910
  • Python version used: 3.9.6

This bug was inspired by @erictraut's example of how to deal with other kind of issue

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions