Skip to content

Commit f52268d

Browse files
authored
Pick correct file if two files with the same name but with different extensions exist (#2722)
1 parent e939b66 commit f52268d

File tree

6 files changed

+27
-2
lines changed

6 files changed

+27
-2
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Release date: TBA
4141

4242
Closes #2684
4343

44+
* Fix bug where ``pylint code.custom_extension`` would analyze ``code.py`` or ``code.pyi`` instead if they existed.
45+
46+
Closes pylint-dev/pylint#3631
47+
4448

4549
What's New in astroid 3.3.9?
4650
============================

astroid/modutils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,9 @@ def get_source_file(
489489
"""
490490
filename = os.path.abspath(_path_from_filename(filename))
491491
base, orig_ext = os.path.splitext(filename)
492-
if orig_ext == ".pyi" and os.path.exists(f"{base}{orig_ext}"):
493-
return f"{base}{orig_ext}"
492+
orig_ext = orig_ext.lstrip(".")
493+
if orig_ext not in PY_SOURCE_EXTS and os.path.exists(f"{base}.{orig_ext}"):
494+
return f"{base}.{orig_ext}"
494495
for ext in PY_SOURCE_EXTS_STUBS_FIRST if prefer_stubs else PY_SOURCE_EXTS:
495496
source_path = f"{base}.{ext}"
496497
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+
3+
"mails": ["[email protected]"],
4+
"name": "Charlie Ringström"
5+
},
26
37
"mails": ["[email protected]"],
48
"name": "correctmost"

tests/test_modutils.py

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

338+
def test_nonstandard_extension(self) -> None:
339+
package = resources.find("pyi_data/find_test")
340+
modules = [
341+
os.path.join(package, "__init__.weird_ext"),
342+
os.path.join(package, "standalone_file.weird_ext"),
343+
]
344+
for module in modules:
345+
self.assertEqual(
346+
modutils.get_source_file(module, prefer_stubs=True),
347+
module,
348+
)
349+
self.assertEqual(
350+
modutils.get_source_file(module),
351+
module,
352+
)
353+
338354

339355
class IsStandardModuleTest(resources.SysPathSetup, unittest.TestCase):
340356
"""

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)