Skip to content

Spurious Type failure on register_structure_hook_factory? #578

Closed

Description

@stephenprater
  • cattrs version - 24.0.1
  • Python version - 3.12.4
  • Operating System - OSX 14.6.1

Description

When attempting to use register_structure_hook_factory for my custom structure hook, I get a type error that the decorator expects 1 more positional argument:

@my_converter.register_structure_hook_factory(lambda r: r is MyClass)
def register_review_category_struct(cls: Type[MyClass], conv: cattrs.Converter) -> StructureHook:
    return cattrs.gen.make_dict_structure_fn(
        cls,
        conv,
    )
Screenshot 2024-09-04 at 9 19 21 AM

Using pyright 1.1.379.

The structure factory works in that it creates the correct methods and that my_converter.structure(a_dict, MyClass) - does what it's supposed to but I'm wondering if I'm missing something or if the type error is spurious.

Activity

  1. ericbn commented on Sep 4, 2024

    @ericbn
    Contributor

    Also getting an error with mypy. For the following code:

    from collections.abc import Callable
    from typing import Any
    
    from attr import fields, has
    from cattrs import Converter
    from cattrs.gen import make_dict_unstructure_fn, override
    
    def _my_converter_unstructure_override_field(f):
        if f.name.startswith("_"):
            return override(omit=True)
        if f.default is None:
            return override(omit_if_default=True)
        return None
    
    @my_converter.register_unstructure_hook_factory(has)
    def _my_converter_unstructure_override_class(cl: Any, converter: Converter) -> Callable:
        return make_dict_unstructure_fn(
            cl,
            converter,
            **{
                f.name: v
                for f in fields(cl)
                if (v := _my_converter_unstructure_override_field(f)) is not None
            },
        )

    mypy will fail with:

    error: Value of type variable "UnstructureHookFactory" of function cannot be "Callable[[Any, Converter], Callable[..., Any]]"  [type-var]
    
  2. Tinche commented on Sep 5, 2024

    @Tinche
    Member

    You folks are right, the type hints need improvement here. This might be more complex than I thought.

  3. added this to the 24.2 milestone on Sep 5, 2024
  4. ericbn commented on Sep 5, 2024

    @ericbn
    Contributor

    @Tinche, thank you for the prompt response. Using the tin/fix-type-hints branch (resolved to commit dc50023) worked for me. No more mypy errors using the same code as before.

    EDIT: For a quick reference, it can be installed locally using pip install git+https://github.com/python-attrs/cattrs.git@tin/fix-type-hints

  5. linked a pull request that will close this issueFix converter type hints #579on Sep 7, 2024
  6. Tinche commented on Sep 9, 2024

    @Tinche
    Member

    Yeah, according to my tests this is a big improvement. I'm planning on releasing this as a minor release in a few days.

  7. Tinche commented on Sep 11, 2024

    @Tinche
    Member

    Fix released!

  8. removed this from the 24.2 milestone on Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions