如何使用pydantic创建链接列表,因此自动完成不突破

发布于 2025-01-24 16:02:28 字数 904 浏览 3 评论 0原文

我正在尝试使用Pydantic创建一个链接列表。

以下代码有效,但我无法使自动完成工作。从附件的屏幕截图中可以看到,属性“ Next”不会显示自动完成。

我如何保留自动comp

更新:我正在研究Pycharm Professional 2020.3 + Python 3.10.2 + Pydantic 1.9.0


代码

from pydantic import BaseModel
from pydantic.typing import ForwardRef

Node = ForwardRef('Node')


class Node(BaseModel):
    data: int
    next: Node = None


# Node.update_forward_refs()

def get_l1() -> Node:
    l1: Node = Node(data=1)
    l1.next = Node(data=2)
    l1.next.next = Node(data=3)

    return l1


l2: Node = get_l1()

print(l2)
print(l2.next)
print(l2.next.next)

输出

data=1 next=Node(data=2, next=Node(data=3, next=None))
data=2 next=Node(data=3, next=None)
data=3 next=None

屏幕截图

I am trying to create a linked list using pydantic.

The following code works, but I can't get auto-completion to work. As you can see from the attached screenshot, the property 'next' doesn't show auto-completion.

How do I preserve the auto-comp

Update: I am working on Pycharm Professional 2020.3 + Python 3.10.2 + pydantic 1.9.0


Code

from pydantic import BaseModel
from pydantic.typing import ForwardRef

Node = ForwardRef('Node')


class Node(BaseModel):
    data: int
    next: Node = None


# Node.update_forward_refs()

def get_l1() -> Node:
    l1: Node = Node(data=1)
    l1.next = Node(data=2)
    l1.next.next = Node(data=3)

    return l1


l2: Node = get_l1()

print(l2)
print(l2.next)
print(l2.next.next)

Output

data=1 next=Node(data=2, next=Node(data=3, next=None))
data=2 next=Node(data=3, next=None)
data=3 next=None

Screenshot

l2.next does not show auto-completion

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宣告ˉ结束 2025-01-31 16:02:28

请勿使用forwardRef,使用typing.optional,因为您将none分配给 node> node field and collend node带引号:

# Node = ForwardRef('Node')  <- Do not use this


class Node(BaseModel):
    data: int
    next: Optional["Node"] = None # <- Use typing.Optional and quotes here

关于键入。 rel =“ nofollow noreferrer”> pydantic文档:

typing.optional

可选[x]只是union [x,none];

的简短手

关于引号的用法在类型的提示中,根据

当类型提示包含尚未定义的名称时,该定义可以表示为字符串字面,以后要解决。

通常发生这种情况的情况是容器类别的定义,其中定义的类在某些方法的签名中发生。例如,以下代码(简单的二进制树实现的开始)不起作用:

 类树:
    def __init __(自我,左:树,右:树):
        self.left =左
        self.right =对
 

要解决这个问题,我们写了:

 类树:
    def __init __(self,左:'tree',右:'tree'):
        self.left =左
        self.right =对
 

Do not use ForwardRef, use typing.Optional because you are assigning None to the node field and surround Node with quotes:

# Node = ForwardRef('Node')  <- Do not use this


class Node(BaseModel):
    data: int
    next: Optional["Node"] = None # <- Use typing.Optional and quotes here

About typing.Optional from the pydantic documentation:

typing.Optional

Optional[x] is simply short hand for Union[x, None];

About the usage of quotes in a type hint of a forward reference according to PEP 484:

When a type hint contains names that have not been defined yet, that definition may be expressed as a string literal, to be resolved later.

A situation where this occurs commonly is the definition of a container class, where the class being defined occurs in the signature of some of the methods. For example, the following code (the start of a simple binary tree implementation) does not work:

class Tree:
    def __init__(self, left: Tree, right: Tree):
        self.left = left
        self.right = right

To address this, we write:

class Tree:
    def __init__(self, left: 'Tree', right: 'Tree'):
        self.left = left
        self.right = right
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文