Skip to content

Pick correct file if two files with the same name but with different extensions exist #2728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions astroid/modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions script/.contributors_aliases.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
16 changes: 16 additions & 0 deletions tests/test_modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
Empty file.
Empty file.