4444
4545
4646if sys .platform .startswith ("win" ):
47- PY_SOURCE_EXTS = ("pyi" , "pyw" , "py" )
47+ PY_SOURCE_EXTS = ("py" , "pyw" , "pyi" )
48+ PY_SOURCE_EXTS_STUBS_FIRST = ("pyi" , "pyw" , "py" )
4849 PY_COMPILED_EXTS = ("dll" , "pyd" )
4950else :
50- PY_SOURCE_EXTS = ("pyi" , "py" )
51+ PY_SOURCE_EXTS = ("py" , "pyi" )
52+ PY_SOURCE_EXTS_STUBS_FIRST = ("pyi" , "py" )
5153 PY_COMPILED_EXTS = ("so" ,)
5254
5355
@@ -484,7 +486,9 @@ def get_module_files(
484486 return files
485487
486488
487- def get_source_file (filename : str , include_no_ext : bool = False ) -> str :
489+ def get_source_file (
490+ filename : str , include_no_ext : bool = False , prefer_stubs : bool = False
491+ ) -> str :
488492 """Given a python module's file name return the matching source file
489493 name (the filename will be returned identically if it's already an
490494 absolute path to a python source file).
@@ -499,7 +503,7 @@ def get_source_file(filename: str, include_no_ext: bool = False) -> str:
499503 base , orig_ext = os .path .splitext (filename )
500504 if orig_ext == ".pyi" and os .path .exists (f"{ base } { orig_ext } " ):
501505 return f"{ base } { orig_ext } "
502- for ext in PY_SOURCE_EXTS if "numpy" not in filename else reversed ( PY_SOURCE_EXTS ) :
506+ for ext in PY_SOURCE_EXTS_STUBS_FIRST if prefer_stubs else PY_SOURCE_EXTS :
503507 source_path = f"{ base } .{ ext } "
504508 if os .path .exists (source_path ):
505509 return source_path
@@ -671,8 +675,7 @@ def _has_init(directory: str) -> str | None:
671675 else return None.
672676 """
673677 mod_or_pack = os .path .join (directory , "__init__" )
674- exts = reversed (PY_SOURCE_EXTS ) if "numpy" in directory else PY_SOURCE_EXTS
675- for ext in (* exts , "pyc" , "pyo" ):
678+ for ext in (* PY_SOURCE_EXTS , "pyc" , "pyo" ):
676679 if os .path .exists (mod_or_pack + "." + ext ):
677680 return mod_or_pack + "." + ext
678681 return None
0 commit comments