-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[Typing] Fix builtin name conflict tensor.pyi and remove decorator on overloads
#74608
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
Conversation
|
你的PR提交成功,感谢你对开源项目的贡献! |
tools/gen_tensor_stub.py
Outdated
| 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}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
包装成函数吧,而且逻辑既然一样就不要写多次了,考虑使用循环之类的组织下吧(参考下下面的代码),这样后续只需要维护一个列表就好了
There was a problem hiding this comment.
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'There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)结合一下正则,直接正则会导致函数名也被修改的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
牛哇牛哇😺
函数名也被修改的
是参数名叭
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是参数名叭
函数名啊,不是有新增的方法就叫 int、bool 什么的么
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那尴尬了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
还是这个结合好🎉
|
/re-run all failed |
tensor.pyi and remove decorator on overloads
|
|
||
|
|
||
| @overload | ||
| @param_two_alias_one_default(["x", "input"], ["axis", "dim"], ["mode", 'min']) |
There was a problem hiding this comment.
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
SigureMo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.

PR Category
User Experience
PR Types
Bug fixes
Description
由于 Tensor 下新增了一些类型转化方法,比如 xx.int(), xxx.float(),会在 tensor.pyi 文件中生成函数注解,在
def int之后注解的 int,在 mypy 下会被识别成int?Other
cc @SigureMo @megemini