Skip to content

incorrectly reports type error for union types in property setters #497

Description

@gswifort

Problem Description

MyPy extension in VS Code incorrectly reports a type error, while mypy from the command line finds no issues.

Environment

  • VS Code Version: 1.102.0
  • MyPy Extension: mypy-type-checker
  • MyPy Version: 1.20.0 (compiled: yes)
  • Python Version: 3.12.10

MyPy Extension Configuration

{
    "mypy-type-checker.path": [
        "C:\\Users\\GSWI\\AppData\\Local\\Programs\\Python\\Python312\\Scripts\\mypy.exe"
    ]
}

Code Sample

class A:
    _x: int = 0

    @property
    def x(self) -> int:
        return self._x

    @x.setter
    def x(self, value: int | str) -> None:
        self._x = int(value)


a = A()
a.x = "42"  # ← VS Code MyPy extension reports error here

Expected Behavior

The code should pass without errors because:

  1. The setter x accepts both int and str (union type int | str)
  2. MyPy from command line confirms the code is correct

Actual Behavior

VS Code MyPy Extension:

Incompatible types in assignment (expression has type "str", variable has type "int")

Command Line MyPy:

> C:\Users\GSWI\AppData\Local\Programs\Python\Python312\Scripts\mypy.exe main.py
Success: no issues found in 1 source file

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue identified by VS Code Team member as probable bugtriage-needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions