-
-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Spurious Type failure on register_structure_hook_factory
?
#578
Comments
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:
|
You folks are right, the type hints need improvement here. This might be more complex than I thought. |
Can you folks try https://github.com/python-attrs/cattrs/tree/tin/fix-type-hints and see if it helps? |
@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 |
Yeah, according to my tests this is a big improvement. I'm planning on releasing this as a minor release in a few days. |
Fix released! |
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: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.The text was updated successfully, but these errors were encountered: