Skip to content

mypy confuses dataclass field name with imported function name  #10640

Open
@larspetrus

Description

@larspetrus

Bug Report

This standalone file produces the mistaken error models/mypy_bug.py:8: error: "str" not callable:

from dataclasses import dataclass, field
from typing import List

@dataclass
class FilterExpression:
    match_type: str
    field: str
    value: List[str] = field(default_factory=list)

This is because mypy confuses the field "field" and the imported function "field()". Renaming the field removes the error message.

Expected Behavior

This should not be an error.

It's not obvious to me what's correct here, but this code executes as intended in Python 3.8.

So either mypy or Python 3.8 is wrong about this. I choose to file the bug with mypy.

Actual Behavior

mypy gives this error:

models/mypy_bug.py:8: error: "str" not callable

Your Environment

  • Mypy version used: 0.800
  • Mypy command-line flags: Only --ignore-missing-imports [paths]
  • Mypy configuration options from mypy.ini: strict_optional = False
  • Python version used: 3.8
  • Operating system and version: MacOS 11.2

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions