Skip to content

imprv: always use __init__ instead of __new__#295

Merged
bryanforbes merged 1 commit intopygobject:masterfrom
bryanforbes:imprv/new-signature
Apr 18, 2026
Merged

imprv: always use __init__ instead of __new__#295
bryanforbes merged 1 commit intopygobject:masterfrom
bryanforbes:imprv/new-signature

Conversation

@bryanforbes
Copy link
Copy Markdown
Collaborator

For structs and boxed objects, PyGObject uses __new__ for the constructor named "new" under certain cases along with a dummy __init__ that does nothing. In addition, some GObject.Object overrides use __new__ (ex. Pango.Layout) and some struct and boxed overrides use both __new__ and __init__ or one or the other. Finally, PyGObject incorrectly uses Self in their type annotations. The following changes have been made to handle all of these scenarios:

  • __new__ and __init__ handling has been moved to ClassInfo.__build_init()
  • For structs and boxed, __new__ is used to build __init__ when either __new__ is the same as the "new" method from __info__ or __new__ exists directly on the class; otherwise, __init__ is used
  • For GObject.Object subclasses, __new__ is used when it exists directly on the class; otherwise, __init__ is built from the writable, construct, and construct-only props
  • Writing all constructors as __init__ eliminates the improper use of Self that comes from PyGObject

For structs and boxed objects, PyGObject uses `__new__` for the
constructor named "new" under certain cases along with a dummy
`__init__` that does nothing. In addition, some `GObject.Object`
overrides use `__new__` (ex. `Pango.Layout`) and some struct and boxed
overrides use both `__new__` and `__init__` or one or the other.
Finally, PyGObject incorrectly uses `Self` in their type annotations.
The following changes have been made to handle all of these scenarios:

- `__new__` and `__init__` handling has been moved to
  ClassInfo.__build_init()
- For structs and boxed, `__new__` is used to build `__init__` when
  either `__new__` is the same as the "new" method from `__info__` or
  `__new__` exists directly on the class; otherwise, `__init__` is used
- For `GObject.Object` subclasses, `__new__` is used when it exists
  directly on the class; otherwise, `__init__` is built from the
  writable, construct, and construct-only props
- Writing all constructors as `__init__` eliminates the improper use
  of `Self` that comes from PyGObject
@bryanforbes bryanforbes force-pushed the imprv/new-signature branch from 4b1c94b to 2cff169 Compare April 18, 2026 02:05
@bryanforbes bryanforbes merged commit 5df0758 into pygobject:master Apr 18, 2026
2 checks passed
@bryanforbes bryanforbes deleted the imprv/new-signature branch April 18, 2026 02:06
@bryanforbes
Copy link
Copy Markdown
Collaborator Author

@xclaesse this fixes the errors about __new__ and Self being used improperly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant