File: test_any.py

package info (click to toggle)
python-cattrs 25.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,812 kB
  • sloc: python: 12,236; makefile: 155
file content (37 lines) | stat: -rw-r--r-- 911 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""Tests for handling `typing.Any`."""

from typing import Any, Dict, Optional

from attrs import define
from typing_extensions import Any as ExtendedAny


@define
class A:
    pass


def test_unstructuring_dict_of_any(converter):
    """Dicts with Any values should use runtime dispatch for their values."""
    assert converter.unstructure({"a": A()}, Dict[str, Any]) == {"a": {}}


def test_unstructuring_any(converter):
    """`Any` should use runtime dispatch."""

    assert converter.unstructure(A(), Any) == {}


def test_unstructure_optional_any(converter):
    """Unstructuring `Optional[Any]` should use the runtime value."""

    assert converter.unstructure(A(), Optional[Any]) == {}


def test_extended_any(converter):
    """`typing_extensions.Any` works."""

    assert converter.unstructure(A(), unstructure_as=ExtendedAny) == {}

    d = {}
    assert converter.structure(d, ExtendedAny) is d