securedrop-client icon indicating copy to clipboard operation
securedrop-client copied to clipboard

Update __init__ args to show actual type signature

Open heartsucker opened this issue 6 years ago • 5 comments

For example, it would be nice if help(Reply) actually showed me the arguments I need to init an object.

Currently:

>>> help(Reply)

Help on class Reply in module sdclientapi.sdlocalobjects:

class Reply(builtins.object)
 |  This class represents a reply to the source.
 |  
 |  Methods defined here:
 |  
 |  __init__(self, **kwargs) -> None
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

Also help(type(Reply)) to does not provide additional help.

One possible resolution for defining tedious __init__, __eq__, and other dunder methods would be to offload that to attrs.

heartsucker avatar Oct 26 '18 12:10 heartsucker

Note, this is heavily related to freedomofpress/securedrop-sdk#31 and freedomofpress/securedrop-sdk#32.

heartsucker avatar Oct 26 '18 13:10 heartsucker

@nabla-c0d3 Any thoughts on this approach / are you potentially interested in helping with it?

eloquence avatar Nov 13 '20 18:11 eloquence

@eloquence Sure - I could look into it.

What versions of Python is securedrop-sdk targeting? I can think of two really good approaches for this kind of problem:

  1. pydantic, which is Python 3.6+
  2. dataclasses which are Python 3.7+

nabla-c0d3 avatar Nov 14 '20 02:11 nabla-c0d3

@eloquence I made a poc PR for 1) at https://github.com/freedomofpress/securedrop-sdk/pull/149

nabla-c0d3 avatar Nov 14 '20 19:11 nabla-c0d3

For this particular usecase, we just have to add correct docstrings explaining the arguments and their type information. That would be a simple but totally useful solution. Right now, __init__ does not have any doctstring, and if we add some, it shows up properly via the help() call.

kushaldas avatar Dec 09 '20 08:12 kushaldas