Skip to content

typing: GObject: Type Properties using inference#237

Open
kra-mo wants to merge 1 commit into
pygobject:masterfrom
kra-mo:typed-properties
Open

typing: GObject: Type Properties using inference#237
kra-mo wants to merge 1 commit into
pygobject:masterfrom
kra-mo:typed-properties

Conversation

@kra-mo

@kra-mo kra-mo commented Nov 10, 2025

Copy link
Copy Markdown

This seems to work consistently, automatically unless you rely on the implicit nullability of a property. In that case, you will need to manually type it as:

prop = GObject.Property[T | None](type=T)

However, given that this behavior is ignored in other places and that the alternative would be wildly more annoying to deal with, it should probably be fine.

See https://gitlab.gnome.org/GNOME/pygobject/-/issues/701

This seems to work consistently, automatically
unless you rely on the implicit nullability of a property.
In that case, you will need to manually type it as:

```
prop = GObject.Property[T | None](type=T)
```

However, given that this behavior is ignored in other places
and that the alternative would be wildly more annoying
to deal with, it should probably be fine.
@lovetox

lovetox commented Jan 2, 2026

Copy link
Copy Markdown
Collaborator

But subscription does not work

type 'Property' is not subscriptable

This would have to be added in pygobject

And marking the object in the stubs as subscriptable, when it then leads to a runtime error is ugly

@kra-mo

kra-mo commented Jan 2, 2026

Copy link
Copy Markdown
Author

Hmm yeah, I guess you indeed wouldn't be able to specify it manually without changes in PyGObject.

Given that we've done the same with ListModel without runtime guarantees either, I guess we could add this upstream as a "useless" generic first?

@lovetox

lovetox commented Jan 2, 2026

Copy link
Copy Markdown
Collaborator

Yes i would say this should be added first in pygobject

@lovetox

lovetox commented Mar 16, 2026

Copy link
Copy Markdown
Collaborator

Sorry wrong PR closed

@lovetox lovetox closed this Mar 16, 2026
@lovetox lovetox reopened this Mar 16, 2026
@bryanforbes

Copy link
Copy Markdown
Collaborator

Additionally Property[T] syntax can't be added to stubs until Python 3.11 is EOL (mypy uses the ast module and this syntax wasn't added until Python 3.12)

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.

3 participants