question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question / issue with metaclasses and __init__

See original GitHub issue

Problem Description

In my project, certain classes have a strange result for the class initializer documentation. For example:

image

AdoptionData.__init__ does have its own docstring, and it does not say “call self as function”.

The classes that show this behavior all share a common trait: they have the same metaclass, and that metaclass overrides __call__. (The metaclass implements memo-izing/instance caching.)

I am sure that the metaclass is the reason for this behavior, but I haven’t quite figured out exactly how this is happening. I can see that the overridden __call__ function might be problematic – no telling what will happen when you invoke the classname as a function! But I hope there is some way to override this behavior and get it to use my docstring anyway?

Steps to reproduce the behavior:

I’ll create a minimal reproduction if needed, but I’m guessing the description is enough?

System Information

Paste the output of “pdoc --version” here: v9.0.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mhilscommented, Feb 16, 2022

Thanks, this was useful feedback! We take Metaclass.__call__ into account for special snowflakes like dataclasses and NamedTuples. If the class itself has an __init__ with docstring, we should of course prefer that. Fixed with https://github.com/mitmproxy/pdoc/commit/64aab58976320e5941f4dc25f105ce8ffca97fe6. 😃

0reactions
denisedcommented, Mar 1, 2022

Yay it works! Thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there any reason to choose __new__ over __init__ when ...
This is important to know, if __init__ calls a super metaclass which does some additional magic. In that case, one has to change...
Read more >
During metaclass.__init__, super() of the constructed class ...
Attached patch is some new test cases for an approach that I figured out *won't work*. The problem I hit is that "__classcell__"...
Read more >
Understanding Object Instantiation and Metaclasses in Python
I'll start with the fundamentals of object creation, and then we'll dive deep into understanding specific methods, such as __new__ , __init__ , ......
Read more >
Python metaclasses by example - Eli Bendersky's website
Unfortunately, metaclasses have a reputation for "a solution seeking a problem". The aim of this article is to demonstrate a few actual uses...
Read more >
Python Metaclasses - Real Python
That might be going a bit far, but it is probably true that custom metaclasses mostly aren't necessary. If it isn't pretty obvious...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found