Skip to content

feat & refactor: Support forward references in ProtoStruct #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

pk5ls20
Copy link
Contributor

@pk5ls20 pk5ls20 commented Aug 14, 2024

Now you can using forward references in ProtoStruct class, like below:

class OidbFriend(ProtoStruct):
    uid: str = proto_field(1)
    custom_group: Optional[int] = proto_field(2, default=None)
    uin: int = proto_field(3)
    additional: list["OidbFriendAdditional"] = proto_field(10001)


class OidbFriendAdditional(ProtoStruct):
    type: int = proto_field(1)
    layer1: "OidbFriendLayer1" = proto_field(2, default=None)


class OidbFriendProperty(ProtoStruct):
    code: Optional[int] = proto_field(1)
    value: Optional[str] = proto_field(2)


class OidbFriendLayer1(ProtoStruct):
    properties: Optional[list[OidbFriendProperty]] = proto_field(2, default=None)

@pk5ls20 pk5ls20 marked this pull request as draft August 14, 2024 10:54
@pk5ls20 pk5ls20 marked this pull request as ready for review August 14, 2024 11:15
@pk5ls20 pk5ls20 marked this pull request as draft August 14, 2024 11:17
@pk5ls20
Copy link
Contributor Author

pk5ls20 commented Aug 14, 2024

先别合(

@pk5ls20 pk5ls20 force-pushed the refactor/proto-model branch from 228f864 to e1e24ff Compare August 14, 2024 11:44
@pk5ls20 pk5ls20 marked this pull request as ready for review August 14, 2024 11:44
@pk5ls20
Copy link
Contributor Author

pk5ls20 commented Aug 14, 2024

应该没问题了.jpg

@RF-Tar-Railt
Copy link
Contributor

?forwardref直接用它自己的_evaluate就行了啊

@pk5ls20 pk5ls20 marked this pull request as draft August 15, 2024 03:58
@pk5ls20
Copy link
Contributor Author

pk5ls20 commented Aug 15, 2024

@RF-Tar-Railt 能再帮着看下嘛,感谢

@pk5ls20 pk5ls20 marked this pull request as ready for review August 15, 2024 07:30
@RF-Tar-Railt
Copy link
Contributor

我不确定靠导入模块拿globals的行为是否可靠,相比通过frame查找。。。

这块或许还得看看pydanticV1是怎么做的

@pk5ls20
Copy link
Contributor Author

pk5ls20 commented Aug 15, 2024

我不确定靠导入模块拿globals的行为是否可靠,相比通过frame查找。。。

这块或许还得看看pydanticV1是怎么做的

这样写的话应该单文件内的前向引用都没有问题吧🤔

def _resolve_annotations(arg: Union[Type["ProtoStruct"], "ProtoStruct"]) -> None:
if not arg._delay_anno_map:
return
local = importlib.import_module(arg.__module__).__dict__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一块可以加一段先从 sys.modules 找模块:

if globalns is None:
    base_globals = sys.modules[base.__module__].__dict__
else:
    base_globals = globalns

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外我感觉整个annotations的实现都得改一下,多利用typing里现有的东西(例如 get_type_hints 之类的)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

纯typing不太好弄的样子,暴露的接口太少

@pk5ls20 pk5ls20 marked this pull request as draft August 15, 2024 08:37
@pk5ls20 pk5ls20 changed the title Support forward references in ProtoStruct feat & refactor: Support forward references in ProtoStruct Aug 25, 2024
@wyapx
Copy link
Collaborator

wyapx commented Sep 24, 2024

fix on #36

@wyapx wyapx closed this Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants