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
descriptor __set_name__ feature broken for dataclass descriptor fields #77322
Comments
Summary: The descriptor Details: If I set a (Bravo on that by the way! Working descriptors isn't mentioned at all in the PEP as a feature but I was very pleased to see them working!!) System details: btw this is my first ever Python bug report; I hope I did a good job. |
That's a tough one. Because C.d is not set to a descriptor at type creation time (it's set to a Field object), the __set_name__ behavior is never invoked. It's when the @DataClass decorator is called that C.d is set to D(). https://docs.python.org/3/reference/datamodel.html#creating-the-class-object I'm not sure it's possible to work around this without duplicating some of the type creation code, and even then I'm not convinced it's doable. |
I suppose I could, when overwriting the class member, check for inspect.ismethoddescriptor and call __set_name__ myself. |
hmmm... if I check the C.d class attribute it seems to return the i didn't realize there's nothing "special" going on with descriptors here- maybe because of this descriptors should short-circuit the field creation one idea: what if it there were a keyword argument to mark a field as a --- "I've never met a Kentucky man who wasn't either thinking about going home On Mon, Mar 26, 2018 at 6:47 AM, Eric V. Smith <report@bugs.python.org>
|
Nick Coghlan suggested (on python-dev) to have Field implement __set_name__ and call through to the default value, if it exists. That approach seems to work fine. I'll generate a PR with tests sometime before today's release. |
Sounds like a relatively easy solution then. Hopefully it makes the beta 3 so I can use it immediately- thanks! |
I suppose one downside of that solution is __set_name__ will be called for every Field whether or not it is need. Probably can't be helped without major complications. |
I don't expect there to be too much usage of Field objects, so I'm not so worried about it. If you can, try out the code in the PR. Thanks. |
Eric, looking at the PR; note that if you do this for the __set_name__ check: if inspect.ismethoddescriptor(self.default): ...an object like the one below will not get its __set_name__ called, even though PEP-487 says it should: class D:
def __set_name__(self, o, n):
self.name = n
class C:
d: int = D()
if __name__ == "__main__":
print(f"C.d.name = {C.d.name}") # __set_name__ was called
assert inspect.ismethoddescriptor(C.d) # Error |
Yes, I noticed the same thing. I'll remove the test. |
Yeah and I think lines 2709-2712 of TestDescriptors also needs removed. |
I've updated the PR. I left those lines in, and added a different test. After all, it does need to work with real descriptors. |
Looks great to me. Thanks! |
Thanks for the bug report. |
descriptor_object.name
attributeNote: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: