Skip to content

Commit d87efc6

Browse files
Pick correct file if two files with the same name but with different extensions exist (pylint-dev#2722) (pylint-dev#2728)
Co-authored-by: Charlie Ringström <34444482+Chasarr@users.noreply.github.com>
1 parent e29d726 commit d87efc6

File tree

6 files changed

+28
-2
lines changed

6 files changed

+28
-2
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ Release date: TBA
1717

1818
Closes #2684
1919

20+
* Fix bug where ``pylint code.custom_extension`` would analyze ``code.py`` or ``code.pyi`` instead if they existed.
21+
22+
Closes pylint-dev/pylint#3631
23+
24+
2025
What's New in astroid 3.3.9?
2126
============================
2227
Release date: 2025-03-09

astroid/modutils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,9 @@ def get_source_file(
487487
"""
488488
filename = os.path.abspath(_path_from_filename(filename))
489489
base, orig_ext = os.path.splitext(filename)
490-
if orig_ext == ".pyi" and os.path.exists(f"{base}{orig_ext}"):
491-
return f"{base}{orig_ext}"
490+
orig_ext = orig_ext.lstrip(".")
491+
if orig_ext not in PY_SOURCE_EXTS and os.path.exists(f"{base}.{orig_ext}"):
492+
return f"{base}.{orig_ext}"
492493
for ext in PY_SOURCE_EXTS_STUBS_FIRST if prefer_stubs else PY_SOURCE_EXTS:
493494
source_path = f"{base}.{ext}"
494495
if os.path.exists(source_path):

script/.contributors_aliases.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"c.ringstrom@gmail.com": {
3+
"mails": ["c.ringstrom@gmail.com"],
4+
"name": "Charlie Ringström"
5+
},
26
"134317971+correctmost@users.noreply.github.com": {
37
"mails": ["134317971+correctmost@users.noreply.github.com"],
48
"name": "correctmost"

tests/test_modutils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,22 @@ def test_pyi_preferred(self) -> None:
304304
os.path.normpath(module) + "i",
305305
)
306306

307+
def test_nonstandard_extension(self) -> None:
308+
package = resources.find("pyi_data/find_test")
309+
modules = [
310+
os.path.join(package, "__init__.weird_ext"),
311+
os.path.join(package, "standalone_file.weird_ext"),
312+
]
313+
for module in modules:
314+
self.assertEqual(
315+
modutils.get_source_file(module, prefer_stubs=True),
316+
module,
317+
)
318+
self.assertEqual(
319+
modutils.get_source_file(module),
320+
module,
321+
)
322+
307323

308324
class IsStandardModuleTest(resources.SysPathSetup, unittest.TestCase):
309325
"""

tests/testdata/python3/pyi_data/find_test/__init__.weird_ext

Whitespace-only changes.

tests/testdata/python3/pyi_data/find_test/standalone_file.weird_ext

Whitespace-only changes.

0 commit comments

Comments
 (0)