python / python/mypy

Add support for attr.converters.default_if_none

Open
#6,136 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-1-normal topic-attrs
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I have the following:

@attr.s(auto_attribs=True)
class Foo:
    bars: List[Bar] = attr.ib(
        default=attr.Factory(list),
        converter=default_if_none(default=attr.Factory(list)),
    )

I'm running mypy via pre-commit and getting:

error: Unsupported converter, only named functions and types are currently supported

I have the following in my .pre-commit-config.yaml:

- repo: https://github.com/pre-commit/mirrors-mypy
  rev: v0.650
  hooks:
  - id: mypy
    additional_dependencies:
      - attrs>=18.2.0

(Without the additional_dependencies part I also get error: Module 'attr.converters' has no attribute 'default_if_none')

Could the issue be that there's special handling for optional here but not for default_if_none?

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 in mypy/plugins/attrs.py at the existing special handling for optional converters linked in the issue, then reproduce the reported snippet with mypy through the pre-commit configuration. Trace how attr.converters.default_if_none is validated and determine the expected typing behavior; done means the converter is accepted without the reported errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.