可以通过编程方式从已实例化的类生成一个.pyi文件以进行自动完成吗?

huangapple go评论73阅读模式
英文:

Is it possible to programmatically generate a pyi file from an instantiated class (for autocomplete)?

问题

class MyClass:
def init(self, dictionary):
for k, v in dictionary.items():
setattr(self, k, v)

我正在尝试弄清楚如何为这个动态生成的类获得智能感知。大多数集成开发环境可以读取pyi文件来实现这种功能。

我不想手动编写pyi文件。

是否可以实例化这个类,并以编程方式将pyi文件写入磁盘?

mypy有stubgen工具,但我无法弄清楚是否可以以这种方式使用它。

我是否可以从mypy中导入stubgen,并以某种方式将MyClass(<some dict>)传递给它?

英文:

I'm creating a class from a dictionary like this:

class MyClass:
    def __init__(self, dictionary):
        for k, v in dictionary.items():
            setattr(self, k, v)

I'm trying to figure out how I can get Intellisense for this dynamically generated class. Most IDEs can read pyi files for this sort of thing.

I don't want to write out a pyi file manually though.

Is it possible to instantiate this class and programmatically write a pyi file to disk from it?

mypy has the stubgen tool, but I can't figure out if it's possible to use it this way.

Can I import stubgen from mypy and feed it MyClass(&lt;some dict&gt;) somehow?

答案1

得分: 2

静态分析程序如stubgen不适用于分析动态填充的类,因为它们无法看到您完全形成的类的源代码以生成类的存根。您必须在运行源代码以填充实例属性之前在运行时生成存根。

假设您有一个动态填充的类,就像您的示例一样:

class MyClass:
    def __init__(self, dictionary: dict[str, object]) -> None:
        k: str
        v: object
        for k, v in dictionary.items():
            setattr(self, k, v)

并且您将此字典传递给构造函数:

import statistics

instance: MyClass = MyClass({"a": 1, "b": "my_string", "distribution": statistics.NormalDist(0.0, 1.0)})

并且您希望这作为输出:

import statistics

class MyClass:
    a: int
    b: str
    distribution: statistics.NormalDist

    def __init__(self, dictionary: dict[str, object]) -> None:
        ...

生成上述输出的最简单方法是通过钩入实例创建和初始化,以便不影响已存在于类上的__new____init__链式super调用。这可以通过元类的__call__方法完成:

class _PostInitialisationMeta(type):

    """
    Metaclass for classes subject to dynamic stub generation
    """

    def __call__(
        cls, dictionary: dict[str, object], *args: object, **kwargs: object
    ) -> object:

        """
        Override instance creation and initialisation. Generate a string representing
        the class's stub definition suitable for a `.pyi` file.

        Parameters
        ----------
        dictionary
            Mapping from instance attribute names to attribute values
        *args
        **kwargs
            Other positional and keyword arguments to the class's `__new__` and
            `__init` methods

        Returns
        -------
        object
            Created instance
        """

        instance: object = super().__call__(dictionary, *args, **kwargs)
        <generate string here>
        return instance

然后,您可以解析类为抽象语法树,通过添加、删除或转换节点来修改树,然后取消解析转换后的树。下面是使用Python标准库的ast.NodeVisitor的一个可能的实现:

仅适用于Python 3.9+

from __future__ import annotations

import ast
import inspect
import typing as t

# ... 其他代码 ...


class _DynamicClassStubsGenerator(ast.NodeVisitor):

    """
    Generate and cache stubs for class instances whose instance variables are populated
    dynamically
    """

    @classmethod
    def cache_stub_for_dynamic_class(
        StubsGenerator, Class: type, dictionary: dict[str, object], /
    ) -> None:
        # ... 其他代码 ...

然后,您可以像往常一样运行您的类,然后检查存储在缓存_CLASS_TO_STUB_SOURCE_DICT中的内容:

class MyClass(metaclass=_PostInitialisationMeta):
    def __init__(self, dictionary: dict[str, object]) -> None:
        k: str
        v: object
        for k, v in dictionary.items():
            setattr(self, k, v)

>>> MyClass({"a": 1, "b": "my_string", "distribution": statistics.NormalDist(0.0, 1.0)})
>>> src: str
>>> for src in _CLASS_TO_STUB_SOURCE_DICT.values():
...     print(src)
...
import statistics

class MyClass:
    a: int
    b: str
    distribution: statistics.NormalDist

    def __init__(self, dictionary: dict[str, object]) -> None:
        ...

在实际应用中,.pyi文件是基于每个模块的类型接口,因此上面的实现不会立即可用,因为它只适用于一个类。在将源代码写入.pyi文件之前,还必须对.pyi模块中的其他类型的节点进行更多处理,决定如何处理未注释的节点、重复的导入等等。这是stubgen可能会派上用场的地方 - 它可以分析模块的静态部分,您可以使用输出,编写一个ast.NodeTransformer来将输出转换为您动态生成的类。

英文:

Static analysis programs like stubgen are the wrong tool for analysing a class populated dynamically, because they can't see the source code of your fully-formed class to give you the stub of the class. You have to do the stub generation at runtime by running the source code to populate your instance attributes first.


Let's say that you have a dynamically-populated class, as in your example,

class MyClass:
    def __init__(self, dictionary: dict[str, object]) -&gt; None:
        k: str
        v: object
        for k, v in dictionary.items():
            setattr(self, k, v)

and you pass in this dictionary to the constructor,

import statistics

instance: MyClass = MyClass({&quot;a&quot;: 1, &quot;b&quot;: &quot;my_string&quot;, &quot;distribution&quot;: statistics.NormalDist(0.0, 1.0)})

and you want this as your output:

import statistics

class MyClass:
    a: int
    b: str
    distribution: statistics.NormalDist

    def __init__(self, dictionary: dict[str, object]) -&gt; None:
        ...

The easiest way to generate the output above is to hook into instance creation and initialisation, so you don't affect whatever __new__ or __init__ chained super calls which already exist on your class. This can be done via a metaclass's __call__ method:

class _PostInitialisationMeta(type):

    &quot;&quot;&quot;
    Metaclass for classes subject to dynamic stub generation
    &quot;&quot;&quot;

    def __call__(
        cls, dictionary: dict[str, object], *args: object, **kwargs: object
    ) -&gt; object:

        &quot;&quot;&quot;
        Override instance creation and initialisation. Generate a string representing
        the class&#39;s stub definition suitable for a `.pyi` file.

        Parameters
        ----------
        dictionary
            Mapping from instance attribute names to attribute values
        *args
        **kwargs
            Other positional and keyword arguments to the class&#39;s `__new__` and
            `__init__` methods

        Returns
        -------
        object
            Created instance
        &quot;&quot;&quot;

        instance: object = super().__call__(dictionary, *args, **kwargs)
        &lt;generate string here&gt;
        return instance

You can then parse the class into an abstract syntax tree, modify the tree by adding, removing, or transforming nodes, then unparse the transformed tree. Here's one possible implementation using the Python standard library's ast.NodeVisitor:

Python 3.9+ only

from __future__ import annotations

import ast
import inspect
import typing as t


if t.TYPE_CHECKING:

    class _SupportsBodyStatements(t.Protocol):
        body: list[ast.stmt]


_CLASS_TO_STUB_SOURCE_DICT: t.Final[dict[type, str]] = {}


class _PostInitialisationMeta(type):

    &quot;&quot;&quot;
    Metaclass for classes subject to dynamic stub generation
    &quot;&quot;&quot;

    def __call__(
        cls, dictionary: dict[str, object], *args: object, **kwargs: object
    ) -&gt; object:

        &quot;&quot;&quot;
        Override instance creation and initialisation. The first time an instance of a
        class is created and initialised, cache a string representing the class&#39;s stub
        definition suitable for a `.pyi` file.

        Parameters
        ----------
        dictionary
            Mapping from instance attribute names to attribute values
        *args
        **kwargs
            Other positional and keyword arguments to the class&#39;s `__new__` and
            `__init__` methods

        Returns
        -------
        object
            Created instance
        &quot;&quot;&quot;

        instance: object = super().__call__(dictionary, *args, **kwargs)
        _DynamicClassStubsGenerator.cache_stub_for_dynamic_class(cls, dictionary)
        return instance


def _remove_docstring(node: _SupportsBodyStatements, /) -&gt; None:

    &quot;&quot;&quot;
    Removes a docstring node if it exists in the given node&#39;s body
    &quot;&quot;&quot;

    first_node: ast.stmt = node.body[0]
    if (
        isinstance(first_node, ast.Expr)
        and isinstance(first_node.value, ast.Constant)
        and (type(first_node.value.value) is str)
    ):
        node.body.pop(0)


def _replace_body_with_ellipsis(node: _SupportsBodyStatements, /) -&gt; None:

    &quot;&quot;&quot;
    Replaces the body of a given node with a single `...`
    &quot;&quot;&quot;

    node.body[:] = [ast.Expr(ast.Constant(value=...))]


class _DynamicClassStubsGenerator(ast.NodeVisitor):

    &quot;&quot;&quot;
    Generate and cache stubs for class instances whose instance variables are populated
    dynamically
    &quot;&quot;&quot;

    @classmethod
    def cache_stub_for_dynamic_class(
        StubsGenerator, Class: type, dictionary: dict[str, object], /
    ) -&gt; None:

        # Disallow stubs generation if the stub source is already generated
        try:
            _CLASS_TO_STUB_SOURCE_DICT[Class]
        except KeyError:
            pass
        else:
            return

        # Get class&#39;s source code
        src: str = inspect.getsource(Class)
        module_tree: ast.Module = ast.parse(src)

        class_statement: ast.stmt = module_tree.body[0]
        assert isinstance(class_statement, ast.ClassDef)

        # Strip unnecessary details from class body
        stubs_generator: _DynamicClassStubsGenerator = StubsGenerator()
        stubs_generator.visit(module_tree)

        # Adds the following:
        #  - annotated instance attributes on the class body
        #  - import statements for non-builtins
        # --------------------------------------------------
        added_import_nodes: list[ast.stmt] = []
        added_class_nodes: list[ast.stmt] = []
        k: str
        v: object
        for k, v in dictionary.items():
            value_type: type = type(v)
            value_type_name: str = value_type.__qualname__
            value_type_module_name: str = value_type.__module__

            annotated_assignment_statement: ast.stmt = ast.parse(
                f&quot;{k}: {value_type_name}&quot;
            ).body[0]
            assert isinstance(annotated_assignment_statement, ast.AnnAssign)
            added_class_nodes.append(annotated_assignment_statement)
            if value_type_module_name != &quot;builtins&quot;:
                annotation_expression: ast.expr = (
                    annotated_assignment_statement.annotation
                )
                assert isinstance(annotation_expression, ast.Name)
                annotation_expression.id = (
                    f&quot;{value_type_module_name}.{annotation_expression.id}&quot;
                )
                added_import_nodes.append(
                    ast.Import(names=[ast.alias(name=value_type_module_name)])
                )

        module_tree.body[:] = [*added_import_nodes, *module_tree.body]
        class_statement.body[:] = [*added_class_nodes, *class_statement.body]
        _CLASS_TO_STUB_SOURCE_DICT[Class] = ast.unparse(module_tree)

    def visit_ClassDef(self, node: ast.ClassDef) -&gt; None:
        _remove_docstring(node)
        node.keywords = []  # Clear metaclass and other keywords in class definition
        self.generic_visit(node)

    def visit_FunctionDef(self, node: ast.FunctionDef) -&gt; None:
        _replace_body_with_ellipsis(node)

    def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -&gt; None:
        _replace_body_with_ellipsis(node)

You can then run your class as usual, and then inspect what's stored in the cache _CLASS_TO_STUB_SOURCE_DICT:

class MyClass(metaclass=_PostInitialisationMeta):
def __init__(self, dictionary: dict[str, object]) -&gt; None:
k: str
v: object
for k, v in dictionary.items():
setattr(self, k, v)
&gt;&gt;&gt; MyClass({&quot;a&quot;: 1, &quot;b&quot;: &quot;my_string&quot;, &quot;distribution&quot;: statistics.NormalDist(0.0, 1.0)})
&gt;&gt;&gt; src: str
&gt;&gt;&gt; for src in _CLASS_TO_STUB_SOURCE_DICT.values():
...     print(src)
...
import statistics
class MyClass:
a: int
b: str
distribution: statistics.NormalDist
def __init__(self, dictionary: dict[str, object]) -&gt; None:
...

In practice, .pyi files form the type interfaces on a per-module basis, so the implementation above isn't immediately usable as it is only for a class. You also have to do much more processing with other kinds of nodes in your .pyi module, decide what to do with unannotated nodes, repeated imports, etc., before writing the source to a .pyi file. This is where stubgen may come in handy - it can analyse the static parts of your module, and you can take that output and write an ast.NodeTransformer to transform that output into the classes you've generated dynamically.

huangapple
  • 本文由 发表于 2023年2月10日 12:45:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定