python / python/mypy

Doesn't understand the attrs `default` and `converter` arguments correctly

Open
#10,998 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-attrs
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Mypy is complaining about incompatible assignment when it should not, because the default value assigned in the attr.ib() call will be passed to the converter (and be converted). But it seems that mypy do no currently understand that.

attrs docs about order of execution: https://www.attrs.org/en/stable/init.html#order-of-execution

To Reproduce

Code example

from pathlib import Path
import attr
@attr.s
class Options:
    path: Path = attr.ib(default='.', converter=Path)

Expected Behavior

It should not trigger errors.

Actual Behavior

Triggers error: 6: error: Incompatible types in assignment (expression has type "str", variable has type "Path")

Your Environment

  • Mypy version used: mypy 0.910
  • Mypy command-line flags: Nothing special
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8
  • Operating system and version: MacOS catalina

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 with the provided Python reproducer and the attrs order-of-execution documentation, then trace mypy's handling of attr.ib() defaults and converters. Done means the example no longer reports an incompatible assignment while existing attrs typing behavior remains correct.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.