python / python/mypy

Support attrs 20.1's on_setattr=attr.setters.convert

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

Nobody has claimed this yet.

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

Description

Feature

Version 20.1 of attrs introduced support for running a field's converter whenever the field is set. Here's an example of it in action:

import re
import attr

@attr.s(auto_attribs=True)
class GHRepo:
    owner: str
    name: str


def parse_ghrepo(s: str) -> GHRepo:
    if (m := re.fullmatch(r'([^/]+)/([^/]+)', s)) is not None:
        return GHRepo(owner=m[1], name=m[2])
    else:
        raise ValueError(s)


@attr.s
class Foo:
    repo: GHRepo = attr.ib(converter=parse_ghrepo, on_setattr=attr.setters.convert)

f = Foo("owner/name")
f.repo = "python/mypy"  # mypy errors on this line
print(f)

This prints out Foo(repo=GHRepo(owner='python', name='mypy')), as the "python/mypy" assigned to f.repo is converted to a GHRepo with parse_ghrepo().

Unfortunately, mypy as of both version 0.812 and commit 28034cc does not recognize the typing implications of on_setattr=attr.setters.convert, as running it on the above code with the default settings gives the error Incompatible types in assignment (expression has type "str", variable has type "GHRepo") on the marked line. I would like to request that mypy recognize the setting's typing implications and allow thus-configured fields to be set to the input types of their converters.

Pitch

This feature would provide greater support for attrs and allow for defining fields that are initialized & set by converting from other types.

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 reproducing the provided Python example with mypy and reviewing how attrs converter types are currently inferred. The issue mentions no specific files or tests, so the work requires locating the relevant attrs plugin and assignment-checking paths; it is done when the shown assignment is accepted without weakening other type checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.