pylint-dev / pylint-dev/astroid

brain_gi uses wrong Gtk version for analysis

Open
#2,190 1 comment 0 reactions 0 assignees View on GitHub
Brain 🧠
Dominant language
Python
Stars
582
Forks
357
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### Steps to reproduce
This only happens on systems where both Gtk 3 and Gtk 4 are available.

```python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

cell = Gtk.CellRendererText()
#cell.props.xalign = 1.0

Gtk.Builder().connect_signals({})
```
When you run pylint on this code, it works as expected; however, when you uncomment the `cell.props.xalign` line, it breaks with
```test.py:8:0: E1101: Instance of 'Builder' has no 'connect_signals' member (no-member)```
(`connect_signals` is only available in Gtk3, not in Gtk4)

### Current behavior
```test.py:8:0: E1101: Instance of 'Builder' has no 'connect_signals' member (no-member)```

### Expected behavior
**E1101** is not there (as `Builder` does have `connect_signals` member in Gtk 3.0).

### More info

I did a little bit more digging and found out that in the second (failing) case, following exception is raised in `_register_require_version`:
```Namespace Gtk is already loaded with version 4.0```

Debug prints in each function in brain_gi.py further showed that in the first case, `require_version` is handled first as expected. In the second case, `_register_require_version` is called after everything else has been processed, which is wrong (newer Gtk was already loaded at this point).

I found this in astroid 2.12.5 and pylint 2.15.2 with Python 3.7 and 3.11, but I also tried the latest and greatest (astroid 2.15.5 & pylint 2.17.4), and the same issue is still reproducible.

This issue was initially reported here:
https://github.com/pylint-dev/pylint/issues/6352

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with brain_gi.py and trace _register_require_version while reproducing the provided Gtk 3 snippet with both Gtk versions installed. Check why the require_version handling occurs after other processing in the failing case. Done means the Gtk 3 requirement is honored before Gtk 4 is loaded and the E1101 warning no longer appears.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.