-
-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Generate __init__
methods for typed dicts with **kwargs: Unpack[MyDict]
to help docstring parser fetch other parameters types
#284
Comments
Hi @butterlyn, thanks for reporting :) Yes we should handle typed dicts like we handle data classes I suppose. I'll implement that asap. |
Actually, they're not the same thing. Typed dicts will not create an >>> from typing import TypedDict
>>> class D(TypedDict):
... hello: str
... world: int
...
>>> D.__init__
<slot wrapper '__init__' of 'dict' objects>
>>> help(D.__init__)
Help on wrapper_descriptor:
__init__(self, /, *args, **kwargs)
Initialize self. See help(type(self)) for accurate signature.
>>> D(byebye=0.1)
{'byebye': 0.1} If you want to force your users to use I do note that marking I will close as not planned. |
Hi @pawamoy, thanks for the reply, though I'm a bit confused where to go from here. I still need a way to add TypedDict items to a docstring in a way where griffe doesn't raise a warning. I've used parameters (not attributes) because:
[TypedDict] can be used as a callable object with keyword arguments corresponding to the TypedDict items. Non-keyword arguments are not allowed. Example:
When called, the TypedDict type object returns an ordinary dictionary object at runtime:
Though I agree in almost all cases a dataclass (or pydantic model) is a better option, there are still cases where I need to use TypedDict for type hinting purposes and still need to include a docstring. I think parameters is the best class docstring heading that describes how TypedDict items behave in PEP 589 (though in an ideal world a new heading like "keys" would be supported). If you disagree, could you please advise how to add TypedDict docstrings in a way that's compatible with griffe? |
Thanks for your patience. The paragraphs from PEP 589 are just here for completeness I suppose: you get the same behavior with a basic dict, or a basic class that inherits from dict. The thing is, at runtime, you can pass any keyword, not just the ones you documented. We're a bit between two worlds here, runtime and static analysis. I can see a way to reconcile them: if no 1. No lies:
|
Another alternative is to wait for #251 to be merged (but it's a big PR and still in draft, and it will probably end up in Insiders first). It will allow to disable some warnings (downgrade them to info or debug messages), but only globally, not per message. |
Thanks @pawamoy for the detailed response, I can see how TypedDict makes this difficult 😅 Options 2 or 6 makes the most intuitive sense to me personally, "Other Parameters" seems like a nice way of characterising TypedDict items. Though this is merely my opinion which is subjective, I wouldn't want to cause more confusion by imposing my personal preferences. Happy to defer to you on which makes sense to implement (or wait for #251). In the meantime, I'm annotating in the style of option 2 ("Other Parameters" and duplicating types in the docstring). This doesn't raise a griffe warning and resolves the cicd pipeline errors I was getting originally. Thanks for your help with this issue 😄 |
Glad to see we agree: I also think option 2 and 6 are the best ones. Well, option 2 wouldn't be very useful since we can already use "Other parameters" and duplicated types, like you're doing. So ideally option 6 should be implemented, so that types can be fetched from the signature. Reopening. |
__init__
methods for typed dicts with **kwargs: Unpack[MyDict]
to help docstring parser fetch other parameters types
Description of the bug
TypedDict
classes gets a griffe warning "Parameter does not appear in the function signature" when correctly including the parameters in the docstring. (Note:TypedDict
's do not include an explicitdef __init__()
constructor)To Reproduce
Full traceback
Expected behavior
These warnings should not be raised, since (unlike normal python classes)
first_dict_key
andsecond_dict_key
are parameters for instantiating the class, not class attributes.Additional context
I'm using
--strict
flag for cicd testing of my MkDocs documentation, my cicd pipeline is failing due to this warning.Boost priority
The text was updated successfully, but these errors were encountered: