Skip to content

Useless lines affect type checking #11176

@titorau

Description

@titorau

Versions
Pylance: 2025.10.4
Pyright: 1.1.407

Use case
I want to to export my dataclass instances to CSV. Hence, I write with the following code snippet in VSCode.

# Animals 1

import csv
from dataclasses import asdict, dataclass, fields
import sys


@dataclass
class Animal:
    name: str
    age: int

animals = [
    Animal(name="Foo", age=3),
    Animal(name="Bar", age=5),
]

field_names = [field.name for field in fields(Animal)]
writer = csv.DictWriter(sys.stdout, fieldnames=field_names)
writer.writeheader()
writer.writerows(
    map(asdict, animals),
)

Python runs it as expected and all function signatures are correctly detected by Pylance.

Expected behavior
There should be no error.

Current behavior
I get 3 errors at line 22, on map(asdict, animals).

Argument of type "Overload[(obj: DataclassInstance) -> dict[str, Any], (obj: DataclassInstance, *, dict_factory: (list[tuple[str, Any]]) -> _T@asdict) -> _T@asdict]" cannot be assigned to parameter "func" of type "(_T1@__new__) -> _S@map" in function "__new__"
  No overloaded function matches type "(Animal) -> _S@map"  (reportArgumentType)
Argument of type "Overload[(obj: DataclassInstance) -> dict[str, Any], (obj: DataclassInstance, *, dict_factory: (list[tuple[str, Any]]) -> _T@asdict) -> _T@asdict]" cannot be assigned to parameter "func" of type "(_T1@__new__) -> _S@map" in function "__new__"
  No overloaded function matches type "(_T1@__new__) -> _S@map"  (reportArgumentType)
Argument of type "map[Unknown]" cannot be assigned to parameter "rowdicts" of type "Iterable[Mapping[str, Any]]" in function "writerows"
  "map[Unknown]" is incompatible with protocol "Iterable[Mapping[str, Any]]"
    "__iter__" is an incompatible type
      Type "() -> map[Unknown]" is not assignable to type "() -> Iterator[_T_co@Iterable]"
        Function return type "map[Unknown]" is incompatible with type "Iterator[_T_co@Iterable]"
          "map[Unknown]" is incompatible with protocol "Iterator[_T_co@Iterable]"  (reportArgumentType)

Bug analysis
I have tested the 6 following variants which are basically doing the same things. Pylance and Pyright behave exactly the same, I only get errors in versions 1 and 4. The example I give uses csv and dataclasses but the issue seems much deeper since useless lines affect type checking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions