diff --git a/ChangeLog b/ChangeLog index 3ba00b434f..f4bde4ddb5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,6 +17,11 @@ Release date: TBA Closes #2684 +* Fix bug where ``pylint code.custom_extension`` would analyze ``code.py`` or ``code.pyi`` instead if they existed. + + Closes pylint-dev/pylint#3631 + + What's New in astroid 3.3.9? ============================ Release date: 2025-03-09 diff --git a/astroid/modutils.py b/astroid/modutils.py index 8f7d0d3fe9..0a933054df 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -487,8 +487,9 @@ def get_source_file( """ filename = os.path.abspath(_path_from_filename(filename)) base, orig_ext = os.path.splitext(filename) - if orig_ext == ".pyi" and os.path.exists(f"{base}{orig_ext}"): - return f"{base}{orig_ext}" + orig_ext = orig_ext.lstrip(".") + if orig_ext not in PY_SOURCE_EXTS and os.path.exists(f"{base}.{orig_ext}"): + return f"{base}.{orig_ext}" for ext in PY_SOURCE_EXTS_STUBS_FIRST if prefer_stubs else PY_SOURCE_EXTS: source_path = f"{base}.{ext}" if os.path.exists(source_path): diff --git a/script/.contributors_aliases.json b/script/.contributors_aliases.json index 20f67fc00f..6615d60f2a 100644 --- a/script/.contributors_aliases.json +++ b/script/.contributors_aliases.json @@ -1,4 +1,8 @@ { + "c.ringstrom@gmail.com": { + "mails": ["c.ringstrom@gmail.com"], + "name": "Charlie Ringström" + }, "134317971+correctmost@users.noreply.github.com": { "mails": ["134317971+correctmost@users.noreply.github.com"], "name": "correctmost" diff --git a/tests/test_modutils.py b/tests/test_modutils.py index be9dce8fb3..8ea7713abe 100644 --- a/tests/test_modutils.py +++ b/tests/test_modutils.py @@ -304,6 +304,22 @@ def test_pyi_preferred(self) -> None: os.path.normpath(module) + "i", ) + def test_nonstandard_extension(self) -> None: + package = resources.find("pyi_data/find_test") + modules = [ + os.path.join(package, "__init__.weird_ext"), + os.path.join(package, "standalone_file.weird_ext"), + ] + for module in modules: + self.assertEqual( + modutils.get_source_file(module, prefer_stubs=True), + module, + ) + self.assertEqual( + modutils.get_source_file(module), + module, + ) + class IsStandardModuleTest(resources.SysPathSetup, unittest.TestCase): """ diff --git a/tests/testdata/python3/pyi_data/find_test/__init__.weird_ext b/tests/testdata/python3/pyi_data/find_test/__init__.weird_ext new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/testdata/python3/pyi_data/find_test/standalone_file.weird_ext b/tests/testdata/python3/pyi_data/find_test/standalone_file.weird_ext new file mode 100644 index 0000000000..e69de29bb2