Skip to content

Conversation

@ooooo-create
Copy link
Contributor

@ooooo-create ooooo-create commented Aug 14, 2025

PR Category

User Experience

PR Types

Bug fixes

Description

由于 Tensor 下新增了一些类型转化方法,比如 xx.int(), xxx.float(),会在 tensor.pyi 文件中生成函数注解,在 def int 之后注解的 int,在 mypy 下会被识别成 int?

看了一下 Torch 这部分也是类似的做法,现在 import 的类别就是在 Torch 上复制过来,因为看最近都在做 API Compatibility 的工作

Other

cc @SigureMo @megemini

@paddle-bot
Copy link

paddle-bot bot commented Aug 14, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Aug 14, 2025
Comment on lines 429 to 434
sig = re.sub(r'\bint\b', '_int', f"{sig}")
sig = re.sub(r'\bfloat\b', '_float', f"{sig}")
sig = re.sub(r'\bbool\b', '_bool', f"{sig}")
sig = re.sub(r'\bcomplex\b', '_complex', f"{sig}")
sig = re.sub(r'\bbytes\b', '_bytes', f"{sig}")
sig = re.sub(r'\bstr\b', '_str', f"{sig}")
Copy link
Member

@SigureMo SigureMo Aug 14, 2025

Choose a reason for hiding this comment

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

包装成函数吧,而且逻辑既然一样就不要写多次了,考虑使用循环之类的组织下吧(参考下下面的代码),这样后续只需要维护一个列表就好了

Copy link
Member

Choose a reason for hiding this comment

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

另外我担心正则不够可靠,既然输入是 signature,而且我们前提是相关 API 所在文件都用了 PEP 563,那么就可以这样写:

from __future__ import annotations
import inspect


def fn(a: int, b: bool, c: str) -> float: ...


def rename_builtin_annotation(annotation):
    # NOTE(ooooo-create): Rename built-in types to avoid naming conflicts
    if annotation in ["int", "bool", "str", "float"]:
        return f"_{annotation}"
    return annotation


sig = inspect.signature(fn)
sig = sig.replace(
    parameters=[
        p.replace(annotation=rename_builtin_annotation(p.annotation))
        for p in sig.parameters.values()
    ],
    return_annotation=rename_builtin_annotation(sig.return_annotation),
)
print(sig)
# (a: '_int', b: '_bool', c: '_str') -> '_float'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OKOK,我再去看看,当时是偷懒不想处理嵌套的 Sequence[int], str | None ,就用了正则 (doge

Copy link
Member

Choose a reason for hiding this comment

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

嵌套形式的话,这样呢?

import re

@cache
def create_builtin_annotation_renamer():
    # NOTE(ooooo-create): Rename built-in types to avoid naming conflicts
    builtin_types = ["int", "bool", "str", "float"]
    regex_string = "|".join([rf"\b{t}\b" for t in builtin_types])
    regex = re.compile(regex_string)

    def renamer(annotations):
        return regex.sub(lambda m: f"_{m.group(0)}", annotations)

    return renamer


def rename_builtin_annotation(annotation):
    renamer = create_builtin_annotation_renamer()
    return renamer(annotation)

结合一下正则,直接正则会导致函数名也被修改的

Copy link
Contributor Author

Choose a reason for hiding this comment

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

牛哇牛哇😺

函数名也被修改的

是参数名叭

Copy link
Member

Choose a reason for hiding this comment

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

是参数名叭

函数名啊,不是有新增的方法就叫 intbool 什么的么

Copy link
Contributor Author

@ooooo-create ooooo-create Aug 14, 2025

Choose a reason for hiding this comment

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

图片

inspect.signature()文档上好像是不处理函数名的,name是后面的代码再加的

Copy link
Member

Choose a reason for hiding this comment

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

那尴尬了

Copy link
Contributor Author

Choose a reason for hiding this comment

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

还是这个结合好🎉

@luotao1 luotao1 added the HappyOpenSource 快乐开源活动issue与PR label Aug 14, 2025
@ooooo-create
Copy link
Contributor Author

/re-run all failed

@SigureMo SigureMo changed the title [Typing] Fix tensor.pyi [Typing] Fix builtin name conflict tensor.pyi and remove decorator on overloads Aug 15, 2025


@overload
@param_two_alias_one_default(["x", "input"], ["axis", "dim"], ["mode", 'min'])
Copy link
Member

Choose a reason for hiding this comment

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

@zhengshengning 不要在 overload 上添加装饰器,不了解 overload 是什么参考文档 https://typing.python.org/en/latest/spec/overload.html

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

LGTMeow 🐾

@SigureMo SigureMo merged commit 6bd5e66 into PaddlePaddle:develop Aug 15, 2025
88 of 91 checks passed
@ooooo-create ooooo-create deleted the fix_typing branch September 29, 2025 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers HappyOpenSource 快乐开源活动issue与PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants