Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1461.change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed annotations for `attrs.field(converter=...)`. Previously, a `tuple` of converters was only accepted if it had exactly one element.
6 changes: 3 additions & 3 deletions src/attrs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def field(
metadata: Mapping[Any, Any] | None = ...,
converter: _ConverterType
| list[_ConverterType]
| tuple[_ConverterType]
| tuple[_ConverterType, ...]
| None = ...,
factory: Callable[[], _T] | None = ...,
kw_only: bool | None = ...,
Expand All @@ -121,7 +121,7 @@ def field(
metadata: Mapping[Any, Any] | None = ...,
converter: _ConverterType
| list[_ConverterType]
| tuple[_ConverterType]
| tuple[_ConverterType, ...]
| None = ...,
factory: Callable[[], _T] | None = ...,
kw_only: bool | None = ...,
Expand All @@ -144,7 +144,7 @@ def field(
metadata: Mapping[Any, Any] | None = ...,
converter: _ConverterType
| list[_ConverterType]
| tuple[_ConverterType]
| tuple[_ConverterType, ...]
| None = ...,
factory: Callable[[], _T] | None = ...,
kw_only: bool | None = ...,
Expand Down
32 changes: 32 additions & 0 deletions tests/test_pyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,35 @@ def test_pyright_attrsinstance_compat(tmp_path):
)
}
assert diagnostics == expected_diagnostics


def test_pyright_field_converters_tuple(tmp_path):
"""
Test that `field(converter=(_, _, ...))` raises no FPs in Pyright.
"""
test_pyright_field_converters_tuple_path = (
tmp_path / "test_pyright_field_converters_tuple.py"
)
test_pyright_field_converters_tuple_path.write_text(
"""\
import attrs


def square(value: int) -> int:
return value * value

def negate(value: int) -> int:
return -value


@attrs.define
class SomeClass:

value: int = attrs.field(converter=(square, negate))
"""
)

diagnostics = parse_pyright_output(
test_pyright_field_converters_tuple_path
)
assert not diagnostics