Skip to content

Support more callable converters in the attrs plugin#21442

Open
filbranden wants to merge 1 commit intopython:masterfrom
filbranden:converters2
Open

Support more callable converters in the attrs plugin#21442
filbranden wants to merge 1 commit intopython:masterfrom
filbranden:converters2

Conversation

@filbranden
Copy link
Copy Markdown

@filbranden filbranden commented May 8, 2026

The attrs plugin only recognized converters that were named functions, types, or lambdas. Extend it to also accept:

The init parameter type is still derived from the first positional parameter of the wrapped callable, so existing diagnostics for converters with the wrong signature continue to apply.

Fixes #15736.

This was authored with the help of Claude Code.

Checklist:

  • Read the Contributing Guidelines
  • Add tests for all changed behaviour.
  • Make sure CI passes.
  • Please do not force push to the PR once it has been reviewed.

This is a follow up to #21408, with a narrower scope of only supporting more callable converters and not adding support for the Converter class, which can be done in a later follow up or further revision of #21408 once this PR is merged.

The attrs plugin only recognized converters that were named functions,
types, or lambdas. Extend it to also accept:

- Calls returning a callable, e.g. `converter=make_converter(arg)` — the
  long-standing request in python#15736. Includes chained calls and overloaded
  callees (such as `attrs.converters.pipe`/`default_if_none`).
- Variables annotated with a callable type.

The init parameter type is still derived from the first positional
parameter of the wrapped callable, so existing diagnostics for
converters with the wrong signature continue to apply.

Fixes python#15736.

This was authored with the help of Claude Code.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 8, 2026

Diff from mypy_primer, showing the effect of this PR on open source code:

core (https://github.com/home-assistant/core)
+ homeassistant/helpers/entity_registry.py:223: error: Unused "type: ignore" comment  [unused-ignore]

@filbranden
Copy link
Copy Markdown
Author

Diff from mypy_primer, showing the effect of this PR on open source code:

core (https://github.com/home-assistant/core)
+ homeassistant/helpers/entity_registry.py:223: error: Unused "type: ignore" comment  [unused-ignore]

After a new release of mypy that includes this change, I'm glad to follow up with a PR to home-assistant/core to drop the no longer needed "type: ignore", together with a bump of the mypy version in use there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unsupported converter, only named functions, types and lambdas are currently supported with higher-order function

1 participant