11"""Collect type information."""
22
33import builtins
4- import collections . abc
4+ import importlib
55import json
66import logging
77import re
8- import typing
98from dataclasses import asdict , dataclass
109from functools import cache
1110from pathlib import Path
@@ -227,7 +226,7 @@ def _is_type(value):
227226 return is_type
228227
229228
230- def _builtin_imports ():
229+ def _builtin_types ():
231230 """Return known imports for all builtins (in the current runtime).
232231
233232 Returns
@@ -248,45 +247,24 @@ def _builtin_imports():
248247 return known_imports
249248
250249
251- def _typing_imports ():
252- """Return known imports for public types in the `typing` module.
253-
254- Returns
255- -------
256- known_imports : dict[str, KnownImport]
257- """
258- known_imports = {}
259- for name in typing .__all__ :
250+ def _runtime_types_in_module (module_name ):
251+ module = importlib .import_module (module_name )
252+ types = {}
253+ for name in module .__all__ :
260254 if name .startswith ("_" ):
261255 continue
262- value = getattr (typing , name )
256+ value = getattr (module , name )
263257 if not _is_type (value ):
264258 continue
265- known_imports [name ] = KnownImport .one_from_config (name , info = {"from" : "typing" })
266- return known_imports
267-
268259
269- def _collections_abc_imports ():
270- """Return known imports for public types in the `collections.abc` module.
260+ import_ = KnownImport (import_path = module_name , import_name = name )
261+ types [name ] = import_
262+ types [f"{ module_name } .{ name } " ] = import_
271263
272- Returns
273- -------
274- known_imports : dict[str, KnownImport]
275- """
276- known_imports = {}
277- for name in collections .abc .__all__ :
278- if name .startswith ("_" ):
279- continue
280- value = getattr (collections .abc , name )
281- if not _is_type (value ):
282- continue
283- known_imports [name ] = KnownImport .one_from_config (
284- name , info = {"from" : "collections.abc" }
285- )
286- return known_imports
264+ return types
287265
288266
289- def common_known_imports ():
267+ def common_known_types ():
290268 """Return known imports for commonly supported types.
291269
292270 This includes builtin types, and types from the `typing` or
@@ -295,10 +273,21 @@ def common_known_imports():
295273 Returns
296274 -------
297275 known_imports : dict[str, KnownImport]
276+
277+ Examples
278+ --------
279+ >>> types = common_known_types()
280+ >>> types["str"]
281+ <KnownImport str (builtin)>
282+ >>> types["Iterable"]
283+ <KnownImport 'from collections.abc import Iterable'>
284+ >>> types["collections.abc.Iterable"]
285+ <KnownImport 'from collections.abc import Iterable'>
298286 """
299- known_imports = _builtin_imports ()
300- known_imports |= _typing_imports ()
301- known_imports |= _collections_abc_imports () # Overrides containers from typing
287+ known_imports = _builtin_types ()
288+ known_imports |= _runtime_types_in_module ("typing" )
289+ # Overrides containers from typing
290+ known_imports |= _runtime_types_in_module ("collections.abc" )
302291 return known_imports
303292
304293
@@ -426,7 +415,7 @@ class TypeMatcher:
426415
427416 Examples
428417 --------
429- >>> from docstub._analysis import TypeMatcher, common_known_imports
418+ >>> from docstub._analysis import TypeMatcher, common_known_types
430419 >>> db = TypeMatcher()
431420 >>> db.match("Any")
432421 ('Any', <KnownImport 'from typing import Any'>)
@@ -446,7 +435,7 @@ def __init__(
446435 type_prefixes : dict[str, KnownImport]
447436 type_nicknames : dict[str, str]
448437 """
449- self .types = types or common_known_imports ()
438+ self .types = types or common_known_types ()
450439 self .type_prefixes = type_prefixes or {}
451440 self .type_nicknames = type_nicknames or {}
452441 self .successful_queries = 0
0 commit comments