diff --git a/ChangeLog b/ChangeLog index d730e8e829..bacf9baa64 100644 --- a/ChangeLog +++ b/ChangeLog @@ -41,6 +41,10 @@ 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? ============================ diff --git a/astroid/modutils.py b/astroid/modutils.py index 29d09f860a..6029e33c1d 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -489,8 +489,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 7bf909b149..1289a9a9be 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 e1b4be3841..cdd677edd9 100644 --- a/tests/test_modutils.py +++ b/tests/test_modutils.py @@ -335,6 +335,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