def _get_module()

in metaflow/cmd/develop/stub_generator.py [0:0]


    def _get_module(self, alias, name):
        debug.stubgen_exec("Analyzing module %s (aliased at %s)..." % (name, alias))
        self._current_module = importlib.import_module(name)
        self._current_module_name = alias
        for objname, obj in self._current_module.__dict__.items():
            if objname == "_addl_stubgen_modules":
                debug.stubgen_exec(
                    "Adding modules %s from _addl_stubgen_modules" % str(obj)
                )
                self._pending_modules.extend(
                    (self._get_module_name_alias(m), m) for m in obj
                )
                continue
            if objname.startswith("_"):
                debug.stubgen_exec(
                    "Skipping object because it starts with _ %s" % objname
                )
                continue
            if inspect.ismodule(obj):
                # Only consider modules that are safe modules
                if (
                    any(obj.__name__.startswith(m) for m in self._safe_modules)
                    and not obj.__name__ in self._exclude_modules
                ):
                    debug.stubgen_exec(
                        "Adding child module %s to process" % obj.__name__
                    )

                    new_module_alias = self._get_module_name_alias(obj.__name__)
                    self._pending_modules.append((new_module_alias, obj.__name__))

                    new_parent, new_name = new_module_alias.rsplit(".", 1)
                    self._current_references.append(
                        "from %s import %s as %s"
                        % (
                            self._get_relative_import(
                                new_parent,
                                alias,
                                hasattr(self._current_module, "__path__"),
                            ),
                            new_name,
                            objname,
                        )
                    )
                else:
                    debug.stubgen_exec("Skipping child module %s" % obj.__name__)
            else:
                parent_module = inspect.getmodule(obj)
                # For objects we include:
                #  - stuff that is a functools.partial (these are all the decorators;
                #    we could be more specific but good enough for now) for root module.
                #    We also include the step decorator (it's from metaflow.decorators
                #    which is typically excluded)
                #  - Stuff that is defined in this module itself
                #  - a reference to anything in the modules we will process later
                #    (so we don't duplicate a ton of times)

                if (
                    parent_module is None
                    or (
                        name + "." == self._root_module
                        and (
                            (parent_module.__name__.startswith("functools"))
                            or obj == step
                        )
                    )
                    or parent_module.__name__ == name
                ):
                    debug.stubgen_exec("Adding object %s to process" % objname)
                    self._current_objects[objname] = obj

                elif not any(
                    [
                        parent_module.__name__.startswith(p)
                        for p in self._exclude_modules
                    ]
                ) and any(
                    [parent_module.__name__.startswith(p) for p in self._safe_modules]
                ):
                    parent_alias = self._get_module_name_alias(parent_module.__name__)

                    relative_import = self._get_relative_import(
                        parent_alias, alias, hasattr(self._current_module, "__path__")
                    )

                    debug.stubgen_exec(
                        "Adding reference %s and adding module %s as %s"
                        % (objname, parent_module.__name__, parent_alias)
                    )
                    obj_import_name = getattr(obj, "__name__", objname)
                    if obj_import_name == "<lambda>":
                        # We have one case of this
                        obj_import_name = objname
                    self._current_references.append(
                        "from %s import %s as %s"
                        % (relative_import, obj_import_name, objname)
                    )
                    self._pending_modules.append((parent_alias, parent_module.__name__))
                else:
                    debug.stubgen_exec("Skipping object %s" % objname)