Support more converter forms in the attrs plugin#21408
Open
filbranden wants to merge 1 commit intopython:masterfrom
Open
Support more converter forms in the attrs plugin#21408filbranden wants to merge 1 commit intopython:masterfrom
filbranden wants to merge 1 commit intopython:masterfrom
Conversation
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. - Variables annotated with a callable type. - The `attrs.Converter` wrapper class, including the `takes_self` and `takes_field` keyword forms. - `attrs.converters.pipe(...)` — the existing `Overloaded` handling was too strict to recognize its return 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. Test stubs grew a non-generic `Converter` class plus `pipe` / `to_bool` so the new behavior can be exercised. The field's `converter=` parameter now accepts `Callable | Converter` via a new `_FieldConverterType` alias, which slightly changes the diagnostic message in `testAttrsUsingBadConverter[Reprocess]`. Fixes python#15736. This was authored with the help of Claude Code.
Contributor
|
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]
|
Author
Nice! This is exactly the issue to fix. If this PR goes in and a new release of mypy includes it, I'm glad to follow up with a PR to that project to drop the no longer needed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The attrs plugin only recognized converters that were named functions, types, or lambdas. Extend it to also accept:
converter=make_converter(arg)— the long-standing request in Unsupported converter, only named functions, types and lambdas are currently supported with higher-order function #15736.attrs.Converterwrapper class, including thetakes_selfandtakes_fieldkeyword forms.attrs.converters.pipe(...)— the existingOverloadedhandling was too strict to recognize its return 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.
Test stubs grew a non-generic
Converterclass pluspipe/to_boolso the new behavior can be exercised. The field'sconverter=parameter now acceptsCallable | Convertervia a new_FieldConverterTypealias, which slightly changes the diagnostic message intestAttrsUsingBadConverter[Reprocess].Fixes #15736.
This was authored with the help of Claude Code.
NOTE: Per
test-data/unit/README.md,lib-stubexpansion should be avoided and is the kind of change reviewers may want to discuss. Here I have only added theConverterclass,_FieldConverterTypealias andpipeandto_boolconverter functions. These are necessary to test the new converter forms and, as such, I imagine they might be acceptable. Let me know if otherwise.Checklist: