Mailing List Archive

Get a function definition/implementation hint similar to the one shown in pycharm.
I've written the following python code snippet in pycharm:
```python
import numpy as np
from numpy import pi, sin

a = np.array([1], dtype=bool)
if np.in|vert(a) == ~a:
print('ok')
```
When putting the point/cursor in the above code snippet at the position denoted by `|`, I would like to see information similar to that provided by `pycharm`, as shown in the following screenshots:

https://user-images.githubusercontent.com/11155854/137619512-674e0eda-7564-4e76-af86-04a194ebeb8e.png
https://user-images.githubusercontent.com/11155854/137619524-a0b584a3-1627-4612-ab1f-05ec1af67d55.png

But I wonder if there are any other python packages/tools that can help me achieve this goal?

Regards,
HZ
--
https://mail.python.org/mailman/listinfo/python-list
Re: Get a function definition/implementation hint similar to the one shown in pycharm. [ In reply to ]
On 18Oct2021 01:43, Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>I've written the following python code snippet in pycharm:
>```python
>import numpy as np
>from numpy import pi, sin
>
>a = np.array([1], dtype=bool)
>if np.in|vert(a) == ~a:
> print('ok')
>```
>When putting the point/cursor in the above code snippet at the position denoted by `|`, I would like to see information similar to that provided by `pycharm`, as shown in the following screenshots:
>
>https://user-images.githubusercontent.com/11155854/137619512-674e0eda-7564-4e76-af86-04a194ebeb8e.png
>https://user-images.githubusercontent.com/11155854/137619524-a0b584a3-1627-4612-ab1f-05ec1af67d55.png
>
>But I wonder if there are any other python packages/tools that can help
>me achieve this goal?

Broadly, you want the "inspect" module, which is part of the stdlib,
documented here: https://docs.python.org/3/library/inspect.html

It has many functions for extracting information about things, and you
want the signature() function to get the parameters and type annotations
of a function.

def f(a):
...

sig = signature(f)

You also want the function docstring for the help text, which is
"f.__doc__" in the example above. This is what gets printed by help():

>>> import numpy as np
>>> help(np.invert)

It may be that PyCharm has additional information about some libraries
allowing it to include a reference to the only documentation.

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Get a function definition/implementation hint similar to the one shown in pycharm. [ In reply to ]
On Tuesday, October 19, 2021 at 5:22:25 AM UTC+8, cameron...@gmail.com wrote:
> On 18Oct2021 01:43, Hongyi Zhao <hongy...@gmail.com> wrote:
> >I've written the following python code snippet in pycharm:
> >```python
> >import numpy as np
> >from numpy import pi, sin
> >
> >a = np.array([1], dtype=bool)
> >if np.in|vert(a) == ~a:
> > print('ok')
> >```
> >When putting the point/cursor in the above code snippet at the position denoted by `|`, I would like to see information similar to that provided by `pycharm`, as shown in the following screenshots:
> >
> >https://user-images.githubusercontent.com/11155854/137619512-674e0eda-7564-4e76-af86-04a194ebeb8e.png
> >https://user-images.githubusercontent.com/11155854/137619524-a0b584a3-1627-4612-ab1f-05ec1af67d55.png
> >
> >But I wonder if there are any other python packages/tools that can help
> >me achieve this goal?
> Broadly, you want the "inspect" module, which is part of the stdlib,
> documented here: https://docs.python.org/3/library/inspect.html
>
> It has many functions for extracting information about things, and you
> want the signature() function to get the parameters and type annotations
> of a function.
>
> def f(a):
> ...
>
> sig = signature(f)

The following ipython test failed:


In [1]: import numpy as np

In [2]: from inspect import signature

In [3]: signature(np.invert)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-f1808184276a> in <module>
----> 1 signature(np.invert)

~/.pyenv/versions/3.9.1/lib/python3.9/inspect.py in signature(obj, follow_wrapped)
3128 def signature(obj, *, follow_wrapped=True):
3129 """Get a signature object for the passed callable."""
-> 3130 return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
3131
3132

~/.pyenv/versions/3.9.1/lib/python3.9/inspect.py in from_callable(cls, obj, follow_wrapped)
2877 def from_callable(cls, obj, *, follow_wrapped=True):
2878 """Constructs Signature for the given callable object."""
-> 2879 return _signature_from_callable(obj, sigcls=cls,
2880 follow_wrapper_chains=follow_wrapped)
2881

~/.pyenv/versions/3.9.1/lib/python3.9/inspect.py in _signature_from_callable(obj, follow_wrapper_chains, skip_bound_arg, sigcls)
2441 raise ValueError(msg)
2442
-> 2443 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2444
2445

ValueError: callable <ufunc 'invert'> is not supported by signature





> You also want the function docstring for the help text, which is
> "f.__doc__" in the example above. This is what gets printed by help():
>
> >>> import numpy as np
> >>> help(np.invert)
>
> It may be that PyCharm has additional information about some libraries
> allowing it to include a reference to the only documentation.
>
> Cheers,
> Cameron Simpson <c...@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Get a function definition/implementation hint similar to the one shown in pycharm. [ In reply to ]
On Tuesday, October 19, 2021 at 5:22:25 AM UTC+8, cameron...@gmail.com wrote:
> On 18Oct2021 01:43, Hongyi Zhao <hongy...@gmail.com> wrote:
> >I've written the following python code snippet in pycharm:
> >```python
> >import numpy as np
> >from numpy import pi, sin
> >
> >a = np.array([1], dtype=bool)
> >if np.in|vert(a) == ~a:
> > print('ok')
> >```
> >When putting the point/cursor in the above code snippet at the position denoted by `|`, I would like to see information similar to that provided by `pycharm`, as shown in the following screenshots:
> >
> >https://user-images.githubusercontent.com/11155854/137619512-674e0eda-7564-4e76-af86-04a194ebeb8e.png
> >https://user-images.githubusercontent.com/11155854/137619524-a0b584a3-1627-4612-ab1f-05ec1af67d55.png
> >
> >But I wonder if there are any other python packages/tools that can help
> >me achieve this goal?
> Broadly, you want the "inspect" module, which is part of the stdlib,
> documented here: https://docs.python.org/3/library/inspect.html
>
> It has many functions for extracting information about things, and you
> want the signature() function to get the parameters and type annotations
> of a function.
>
> def f(a):
> ...
>
> sig = signature(f)
>
> You also want the function docstring for the help text, which is
> "f.__doc__" in the example above. This is what gets printed by help():
>
> >>> import numpy as np
> >>> help(np.invert)

But the following doesn't work:

In [4]: help(~)
File "<ipython-input-4-2a4ba1e3ecdb>", line 1
help(~)
^
SyntaxError: invalid syntax


> It may be that PyCharm has additional information about some libraries
> allowing it to include a reference to the only documentation.
>
> Cheers,
> Cameron Simpson <c...@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Get a function definition/implementation hint similar to the one shown in pycharm. [ In reply to ]
On Wed, Oct 20, 2021 at 4:45 AM hongy...@gmail.com
<hongyi.zhao@gmail.com> wrote:
>
> On Tuesday, October 19, 2021 at 5:22:25 AM UTC+8, cameron...@gmail.com wrote:
> > On 18Oct2021 01:43, Hongyi Zhao <hongy...@gmail.com> wrote:
> > >I've written the following python code snippet in pycharm:
> > >```python
> > >import numpy as np
> > >from numpy import pi, sin
> > >
> > >a = np.array([1], dtype=bool)
> > >if np.in|vert(a) == ~a:
> > > print('ok')
> > >```
> > >When putting the point/cursor in the above code snippet at the position denoted by `|`, I would like to see information similar to that provided by `pycharm`, as shown in the following screenshots:
> > >
> > >https://user-images.githubusercontent.com/11155854/137619512-674e0eda-7564-4e76-af86-04a194ebeb8e.png
> > >https://user-images.githubusercontent.com/11155854/137619524-a0b584a3-1627-4612-ab1f-05ec1af67d55.png
> > >
> > >But I wonder if there are any other python packages/tools that can help
> > >me achieve this goal?
> > Broadly, you want the "inspect" module, which is part of the stdlib,
> > documented here: https://docs.python.org/3/library/inspect.html
> >
> > It has many functions for extracting information about things, and you
> > want the signature() function to get the parameters and type annotations
> > of a function.
> >
> > def f(a):
> > ...
> >
> > sig = signature(f)
> >
> > You also want the function docstring for the help text, which is
> > "f.__doc__" in the example above. This is what gets printed by help():
> >
> > >>> import numpy as np
> > >>> help(np.invert)
>
> But the following doesn't work:
>
> In [4]: help(~)
> File "<ipython-input-4-2a4ba1e3ecdb>", line 1
> help(~)
> ^
> SyntaxError: invalid syntax
>

That's because help() is a perfectly ordinary callable, and you can't
pass an operator as a parameter. Try passing it a string instead:

help("~")

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list