python / python/mypy

mypy confuses dataclass field name with imported function name

Open
#10,640 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-dataclasses topic-runtime-semantics topic-variable-scope
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running mypy 0.800 on the standalone models/mypy_bug.py example with the reported flags and Python 3.8 environment, then inspect the name-resolution path for dataclass fields and imported functions. Done means the example no longer reports "str" not callable while preserving correct handling of the field() default factory.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.