File tree Expand file tree Collapse file tree 1 file changed +11
-7
lines changed
Expand file tree Collapse file tree 1 file changed +11
-7
lines changed Original file line number Diff line number Diff line change 33import ast
44import inspect
55import types
6+ import sysconfig
67import sys
78import platformdirs
89import builtins
@@ -148,24 +149,27 @@ def get_builtin_type_names():
148149 return set (name for name , obj in vars (builtins ).items () if isinstance (obj , type ))
149150
150151
151- def in_stdlib (obj : types .FunctionType | type ) -> bool :
152- """Check if a type is in the standard library."""
152+ def in_stdlib (obj : types .FunctionType | type ) -> str | bool :
153+ """Check if a type is in the standard library and return the name of the module if
154+ so."""
153155 module = inspect .getmodule (obj )
154156 if module is None :
155157 return False
156158 if module .__name__ .startswith ("builtins" ):
157- return True
159+ return "builtins"
158160 if module .__name__ == "types" and obj .__name__ not in dir (types ):
159161 return False
160- return module .__name__ .split ("." )[- 1 ] in STDLIB_MODULES
162+ toplevel = module .__name__ .split ("." )[0 ]
163+ if toplevel in STDLIB_MODULES :
164+ return toplevel
165+ return False
161166
162167
163168def _stdlib_modules () -> frozenset [str ]:
164169 """List all standard library modules."""
165170 std_lib_modules = set (sys .builtin_module_names )
166- for _ , modname , ispkg in pkgutil .iter_modules ():
167- if not ispkg :
168- std_lib_modules .add (modname )
171+ std_lib_path = sysconfig .get_path ("stdlib" )
172+ std_lib_modules .update (m [1 ] for m in pkgutil .iter_modules ([std_lib_path ]))
169173 return frozenset (std_lib_modules )
170174
171175
You can’t perform that action at this time.
0 commit comments