Error when creating Gtk.Box instance

Hi community,

Why the third call to create Box instance is wrong,
since Box.__init__() does not have keyword parameters?

Thanks

# box1 = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 5)
# box2 = Gtk.Box(    orientation=Gtk.Orientation.HORIZONTAL, spacing=5)
box3   = Gtk.Box(    Gtk.Orientation.HORIZONTAL, 5) # error

$ python3 hello.py
Traceback (most recent call last):
    box3 = Gtk.Box(Gtk.Orientation.HORIZONTAL, 5)
TypeError: GObject.__init__() takes exactly 0 arguments (2 given)
$ 
# /usr/local/lib/python3.10/site-packages/gi-stubs/repository/Gtk.pyi
class Box(Orientable, Container):
    def __init__(self, orientation: Optional[Orientation] = None, spacing: Optional[int] = None) -> None: ...
    def new(*args, **kwargs): ...

I already told you: when using pygobject, the Python’s normal constructor form does not take positional arguments.

Every type that derives from GObject.Object has an automatic constructor for the type, which takes named arguments; those named arguments are any writable object property.

For more information, please read the pygobject documentation. Also: stop using the gi-stubs module. It’s clearly wrong.

Hi ebassi,

If named parameters are intended, can the Box.__init__ be written like
this?

class Box:

# def __init__(self, orientation: Optional[Orientation] = None, spacing: Optional[int] = None) -> None: ...

  def __init__(self, **kwarg):
    ...

# x = Box("H", 5) # error

  x = Box(orientation="H", spacing=5)

The non-named args will be caught earlier in Box class, not until
PyGObject class. But names and values pair can be checked in PyGObject.

The function definitions from PyGObject-stubs match the API document
from https://lazka.github.io/pgi-docs. If the stubs function
definitions are wrong, what about the pgi-docs? What can I get for
reference?

If I uninstall PyGObject-stubs, I can not get function and parameter
hints and suggestions for PyGObject functions in VSCode.

Thanks

There is no Box.__init__() written in PyGObject. PyGObject uses introspection data to create objects. It would be extremely tedious work to explicitly write __init__() methods for each GObject Type. Explicitly written __init__() methods would also cause a lot of problems since the underlying GObject Type is bound to change. Properties, methods, constructors, etc. could be added, deprecated, or removed and PyGObject implementation of __init__() would have to be updated manually.

As far as I know, pgi-docs is not enough (or even required at all). I, personally, use the official documentation for C and PyGObject User Guide directly.

Sorry, but that is, currently, how it is; No auto-completion/suggestions for GObject based libraries in Python.

At least that’s case on VS Code. I don’t know about the situation on GNOME Builder though. On it’s homepage it does say

Explore APIs used by your project with auto-completion for C/C++, Python, Rust, and Vala. For languages without native support, ctags integration is provided.

So, maybe GNOME Builder has what you need.

1 Like

Thanks.

I’m trying tkinter. It contains enough information even in the document comment in its source code.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.