-
Notifications
You must be signed in to change notification settings - Fork 1
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
Incomplete attrs support #14
Comments
hey @AdrianSosic and thanks for opening an issue. I am admittedly less familiar with attrs than I am with pydantic and dataclasses, so it's very possible I overlooked or did it slightly wrong. perhaps you can help me out if I point you to the right spots. First off, most of the logic is actually in which simply passes the
yeah, i agree, it's a bit klugy there. I have a feeling this is more on the mkdocstrings side of things, and we'll need to dig a bit deeper into how it decides what to show in order to determine the best fix. I'm curious as well, but will need to look |
oh jeez... looking a bit more at the docs of attrs
I can see that might be useful at runtime, but that's kind of a tough case for documentation. It's basically saying there is no default that one could put in the documentation, since the default essentially depends on the values of the fields that were defined before it, right? In other words, in your example, if you added another field: @define
class MyClass:
x: int
my_attribute: int = field()
"""Docstring of my_attribute."""
@my_attribute.default
def _default_my_attribute(self) -> int:
"""Default factory for my_attribute."""
return self.x*2 what would you want the documentation to say? I think inspecting the body of the function to try to say "the default value for my_attribute is whatever the value of x is times 2" is probably beyond the scope of this package and of fieldz... would you be happy with a solution that just puts "default = " in the generated docs? |
Hi @tlambert03. Thank you very much for the quick reply π₯ Your support is much appreciated. The good news: I have a rather large attrs-based code base that I'm trying to migrate to mkdocs, so that could serve as a kind of testbed for attrs integration of griffe-fieldz. Therefore, if you are eager to get this running, I'm happy to conduct the corresponding tests on my end ππΌ Now back to the topic π Regarding the default factories that process other attributes: Note that this is not attrs-specific, i.e. the same mechanism is also available in pydantic. Here a code snippet taken from its docs: class User(BaseModel):
email: EmailStr
username: str = Field(default_factory=lambda data: data['email']) However, I don't see this as a big problem, tbh. And I agree that trying to infer the logic from the function body is completely impossible, since it can execute arbitrary code. Let's look at the (mutually exclusive) cases and see what the reasonable consequences could be. I'll take the attrs syntax as an example and explain what I consider the "most reasonable" action to take, not knowing the implied implementation details for griffe, though. Plain default
Factory provided as decorated method
Factory provided as lambda
What do you think? |
thanks for the feedback! this is helpful and I think I have a clear idea of how to move forward. Will try to find time in the next week or two. |
This is great news. Let me know when there is something for me to test π |
Description
Hi, thanks for creating and sharing this package ππΌ Having a proper
attrs
support would be awesome β in fact, this currently blocks me from using mkdocs/mkdocstrings for my package documentation. I have just tested the extension but, unfortunately, it is not "really" compatible withattrs
. While the basic functionality works as promised, adding attribute docstrings breaks the output and using certain built-inattrs
features raises errors. See summary below:What I Did
Here the example code I tried:
mkdocs serve
on the snippet, I getattrs
code.First, it is strange that
my_attribute
is listed again after the parameters list. This seems to be caused by having the docstring below the attribute definition, which however is a common thing to do. Second, I'm unsure what to expect from the "description" field. Perhaps this is where the docstring is supposed to go?Any feedback would be highly appreciated π
The text was updated successfully, but these errors were encountered: