python-attrs / python-attrs/cattrs

Types of nested generic fields are not specific enough

Open
#185 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.1k
Forks
159
Avg merge
12h 21m
Merged PRs (30d)
6

Description

  • cattrs version: 1.8.0
  • Python version: 3.9.7
  • Operating System: macOS
Description

Let's have the following test case

    @define
    class X(Generic[T]):
        field: List[T]

    def pred(cls):
        print(f">>> {cls}")
        return False

    result = X[int]([1, 2, 3])
    data = asdict(result)

    c = GenConverter()
    c.register_unstructure_hook_factory(pred, lambda cls: lambda obj: None)
    c.unstructure(result, X[int])

    assert c.structure(data, X[int]) == result

it passes, yet it prints

>>> tests.test_generics.test_karel.<locals>.X[int]
>>> typing.List[~T]
>>> ~T

Should it not be the case that the nested fields types (such as List[~T]) are provided with the type resolved, similarly to how the X[int] is provided in its most specific form?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied generic test case and trace GenConverter.unstructure(result, X[int]) and c.structure(data, X[int]). Compare the types reaching the registered hook factory, including X[int], typing.List[~T], and ~T. Done means the expected resolved nested types are established and the regression test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.