Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ To run a subset of tests:
$ just test tests/test_unstructure.py
```

Static typing tests live in `tests/test_typing_structure.md` and run through `pytest-typing` with Mypy on CPython 3.14.
Static typing tests live in `tests/test_typing_structure.md` and run through `pytest-typing` with Mypy and Pyrefly on CPython 3.14.
To run them on their own:

```shell
$ just --set python python3.14 test tests/test_typing_structure.md
```

The test dependencies include a Mypy version with `TypeForm` support enabled by default.
The test dependencies pin both checkers in `uv.lock`, including a Mypy version with `TypeForm` support enabled by default.
`pytest-typing` is configured to use Pyrefly's `strict` preset for the temporary test snippets.

[llm]: https://github.com/python-attrs/cattrs/blob/main/.github/AI_POLICY.md
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ test = [
"pytest>=8.4.1",
"pytest-typing; python_version == '3.14' and implementation_name == 'cpython'",
"mypy; python_version == '3.14' and implementation_name == 'cpython'",
"pyrefly; python_version == '3.14' and implementation_name == 'cpython'",
"pytest-benchmark>=5.1.0",
"immutables>=0.21",
"coverage>=7.9.2",
Expand Down Expand Up @@ -39,7 +40,8 @@ addopts = [
"--benchmark-warmup-iterations=5",
"--benchmark-group-by=fullname",
]
typing_checkers = ["mypy"]
typing_checkers = ["mypy", "pyrefly"]
typing_pyrefly_preset = "strict"

[project]
name = "cattrs"
Expand Down
44 changes: 34 additions & 10 deletions tests/test_typing_structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ These tests exercise the static return types for the structuring APIs.

```python
from attrs import define
from typing_extensions import assert_type

from cattrs import structure

Expand All @@ -17,7 +18,7 @@ class User:


user = structure({"id": 1, "name": "Ada"}, User)
reveal_type(user) # revealed: test_snippet.User
assert_type(user, User)

name: str = user.name
```
Expand All @@ -27,6 +28,8 @@ name: str = user.name
```python
from dataclasses import dataclass

from typing_extensions import assert_type

from cattrs import Converter


Expand All @@ -38,7 +41,7 @@ class Point:

converter = Converter()
point = converter.structure({"x": 1, "y": 2}, Point)
reveal_type(point) # revealed: test_snippet.Point
assert_type(point, Point)

x: int = point.x
```
Expand All @@ -48,6 +51,8 @@ x: int = point.x
```python
from typing import TypedDict

from typing_extensions import assert_type

from cattrs import Converter


Expand All @@ -58,33 +63,37 @@ class Movie(TypedDict):

converter = Converter()
movie = converter.structure({"title": "Alien", "year": 1979}, Movie)
reveal_type(movie) # revealed: TypedDict(test_snippet.Movie, {"title": str, "year": int})
assert_type(movie, Movie)

title: str = movie["title"]
```

## Converter structure returns homogeneous tuples

```python
from typing_extensions import assert_type

from cattrs import Converter


converter = Converter()
numbers = converter.structure([1, 2, 3], tuple[int, ...])
reveal_type(numbers) # revealed: tuple[int, ...]
assert_type(numbers, tuple[int, ...])

first: int = numbers[0]
```

## Converter structure returns heterogeneous tuples

```python
from typing_extensions import assert_type

from cattrs import Converter


converter = Converter()
row = converter.structure(["Ada", 37, True], tuple[str, int, bool])
reveal_type(row) # revealed: tuple[str, int, bool]
assert_type(row, tuple[str, int, bool])

name: str = row[0]
age: int = row[1]
Expand All @@ -100,7 +109,9 @@ from cattrs import Converter
converter = Converter()

value: int = converter.structure("1", int)
bad: str = converter.structure("1", int) # mypy-error: [assignment]
# mypy-error: [assignment]
# pyrefly-error: [bad-assignment]
bad: str = converter.structure("1", int)
```

## Hook factory decorators preserve factory types
Expand All @@ -109,6 +120,8 @@ bad: str = converter.structure("1", int) # mypy-error: [assignment]
from collections.abc import Callable
from typing import Any

from typing_extensions import assert_type

from cattrs import Converter


Expand All @@ -129,8 +142,15 @@ def structure_factory(cl: type[int]) -> Callable[[str, type[int]], int]:
return lambda value, _: int(value)


reveal_type(unstructure_factory) # revealed: def (cl: type[int]) -> def (int) -> str
reveal_type(structure_factory) # revealed: def (cl: type[int]) -> def (str, type[int]) -> int
assert_type(unstructure_factory(cl=int), Callable[[int], str])
assert_type(structure_factory(cl=int), Callable[[str, type[int]], int])

# mypy-error: [arg-type]
# pyrefly-error: [bad-argument-type]
unstructure_factory(str)
# mypy-error: [arg-type]
# pyrefly-error: [bad-argument-type]
structure_factory(str)
```

## Structure accepts type forms and preserves their result types
Expand Down Expand Up @@ -181,8 +201,12 @@ def convert(value: object, target: TypeForm[T]) -> T:

assert_type(convert("1", int | None), int | None)

bad: str = converter.structure("1", int | None) # mypy-error: [assignment]
converter.structure("1", 42) # mypy-error: [arg-type]
# mypy-error: [assignment]
# pyrefly-error: [bad-assignment]
bad: str = converter.structure("1", int | None)
# mypy-error: [arg-type]
# pyrefly-error: [bad-argument-type]
converter.structure("1", 42)
```

## Structure hook lookup preserves type forms
Expand Down
29 changes: 25 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading