Skip to content

only import gi in brain_gi when it resolves to an installed library - #3449

Open
kali834x wants to merge 1 commit into
pylint-dev:mainfrom
kali834x:gi-brain-no-project-import
Open

kali834x wants to merge 1 commit into
pylint-dev:mainfrom
kali834x:gi-brain-no-project-import

Conversation

@kali834x

Copy link
Copy Markdown
Contributor

Type of Changes

Type
✓ 🐛 Bug fix

Description

brain_gi imports gi while building the AST. _import_gi_module runs __import__("gi.repository.X") for any gi.repository import astroid cannot resolve from files, and _register_require_version runs import gi for every gi.require_version(...) call. Both resolve gi through sys.path, and pylint puts the analysed project first on sys.path, so a checkout that ships a gi/ package (regular or namespace) gets that package executed as soon as a linted file contains import gi.repository.Gtk, from gi.repository import Gtk or gi.require_version("Gtk", "3.0"). No extension-pkg-allow-list entry is involved; this is the same hole #3308 closed for numpy.

Both sites now go through _gi_is_installed, which locates gi with importlib.util.find_spec (no import for a top-level name) and only proceeds when the origin is in the standard library or a site-packages directory. The new modutils.is_library_path also covers Debian's dist-packages and the user site, since sysconfig alone misses the apt-installed python3-gi. A gi covered by extension-pkg-allow-list or unsafe-load-any-extension is still imported from anywhere, so unusual installs keep a knob. A normally installed gi is unaffected and an absent gi behaves as before.

GiFromProjectTest plants a gi package on sys.path and checks that neither site imports it, that a fake installed gi is still used, and the namespace, missing-spec and allow-list cases. The two end-to-end tests fail on main. Full suite, black, ruff, pylint, mypy and pyright pass locally.

Refs #562

@codspeed

codspeed Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 3 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing kali834x:gi-brain-no-project-import (01f934d) with main (0140d49)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩

@codecov

codecov Bot commented Oct 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.30%. Comparing base (0140d49) to head (01f934d).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3449      +/-   ##
==========================================
+ Coverage   91.15%   91.30%   +0.14%     
==========================================
  Files          93       93              
  Lines       11800    11819      +19     
  Branches     1938     1941       +3     
==========================================
+ Hits        10756    10791      +35     
+ Misses        735      714      -21     
- Partials      309      314       +5     
Flag Coverage Δ
linux 90.98% <100.00%> (+0.15%) ⬆️
pypy 91.30% <100.00%> (+0.14%) ⬆️
windows 91.14% <100.00%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
astroid/brain/brain_gi.py 40.95% <100.00%> (+13.94%) ⬆️
astroid/modutils.py 85.12% <100.00%> (+0.26%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

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

Labels

Brain 🧠 Needs a brain tip Bug 🪳

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants