Skip to content

mutable-override does not understand unions #20028

@kasium

Description

@kasium

Bug Report
If I enable mutable-override it fails as it does not understand simple unions.

To Reproduce
Turn on mutable-override

class Base:
    val: bool|None = True
    
class Derived(Base):
    val = False

Gist: https://gist.github.com/mypy-play/aeec67e97957ea9e60431ce8466d8af3
Playground: https://mypy-play.net/?mypy=latest&python=3.12&enable-error-code=mutable-override&gist=aeec67e97957ea9e60431ce8466d8af3

Expected Behavior
no error

Actual Behavior

main.py:5: error: Covariant override of a mutable attribute (base class "Base" defined the type as "bool | None", expression has type "bool")  [mutable-override]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.18.2
  • Mypy command-line flags: enable-error-code="mutable-override"
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongpendingIssues that may be closedtopic-inheritanceInheritance and incompatible overrides

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions