mkdocstrings / pytkdocs

Load Python objects documentation.
https://mkdocstrings.github.io/pytkdocs
ISC License
50 stars 32 forks source link

Incorrect function's argument representation with 2 underscores #99

Closed deknowny closed 2 years ago

deknowny commented 3 years ago

When my function's argument starts with 2 underscores, it has another representation and typing hints missed

screenshot

pawamoy commented 3 years ago

Thank you for the report @deknowny!

The __handler parameter is documented using the right name because it's written explicitly in the Arguments section. The _Bot__handler in the signature is how inspect resolves its name. Maybe it's a bug in inspect? I don't think function arguments names are supposed to be changed the same way class/instance attributes are.

>>> class A:
...     def hello(self, __arg: int):
...         return str(__arg)
... 
>>> import inspect
>>> inspect.signature(A.hello)
<Signature (self, _A__arg: int)>

I'll see if I can find a bug report on inspect, or a rationale for this behavior.

pawamoy commented 3 years ago

Couldn't find anything on the subject yet. In the meantime, I suggest you don't prefix method/function parameters with double-underscores :confused: Why the double underscores by the way? Just being curious :slightly_smiling_face:

deknowny commented 3 years ago

Thanks for answering! I use two underscores in argument's name to define it as positional only. Unfortunately, using / for that is available only with python 3.8 and later but I need to support versions lower

pawamoy commented 3 years ago

Oh okay, so it seems to be the expected behavior, as Python itself changes the argument name to _Bot__handler. inspect has nothing to do with this.

>>> class A:
...     def run(self, __arg):
...         return __arg
... 
>>> a = A()
>>> a.run(__arg=0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: run() got an unexpected keyword argument '__arg'
>>> a.run(_A__arg=0)
0

Well, there's nothing we can do in mkdocstrings or pytkdocs then, unless we start trying to replace parameters name based on their class name, which would be messy I fear :confused: I would be glad to hear any other idea for a solution to this though!

pawamoy commented 2 years ago

This is likely solved by the new handler. Feedback appreciated!