[2/N] Apply py39 ruff fixes (#141938)

Fixes #ISSUE_NUMBER

Pull Request resolved: https://github.com/pytorch/pytorch/pull/141938
Approved by: https://github.com/ezyang
This commit is contained in:
cyy
2024-12-05 06:26:03 +00:00
committed by PyTorch MergeBot
parent 653efe14e4
commit aa95618268
16 changed files with 53 additions and 62 deletions

View File

@ -956,14 +956,13 @@ def argument_type_str_pyi(t: Type) -> str:
ret = "Union[_int, _size]" if t.size is not None else "_size"
elif t.is_tensor_like():
# TODO: this doesn't seem right...
# Tensor?[] currently translates to Optional[Union[Tuple[Tensor, ...], List[Tensor]]]
# It should probably translate to Union[Tuple[Optional[Tensor], ...], List[Optional[Tensor]]]
if isinstance(t.elem, OptionalType):
add_optional = True
# Tensor?[] currently translates to Optional[Union[tuple[Tensor, ...], list[Tensor]]]
# It should probably translate to Union[tuple[Optional[Tensor], ...], list[Optional[Tensor]]]
add_optional = True
ret = (
"Union[Tensor, Tuple[Tensor, ...], List[Tensor]]"
"Union[Tensor, tuple[Tensor, ...], list[Tensor]]"
if t.size is not None
else "Union[Tuple[Tensor, ...], List[Tensor]]"
else "Union[tuple[Tensor, ...], list[Tensor]]"
)
elif str(t.elem) == "float":
ret = "Sequence[_float]"
@ -1001,7 +1000,7 @@ def return_type_str_pyi(t: Type) -> str:
if isinstance(t, ListType):
inner = return_type_str_pyi(t.elem)
return f"Tuple[{inner}, ...]"
return f"tuple[{inner}, ...]"
return argument_type_str_pyi(t)
@ -1014,7 +1013,7 @@ def returns_structseq_pyi(signature: PythonSignature) -> tuple[str, str] | None:
# These types are structseq objects which act like named NamedTuples, but
# the constructor acts like the constructor of tuple. Using typing.NamedTuple
# does not allow us to override __init__.
seq_type = f"Tuple[{', '.join(python_returns)}]"
seq_type = f"tuple[{', '.join(python_returns)}]"
structseq_def_lines = [
f"class {structseq_name}({seq_type}):",
]
@ -1038,12 +1037,12 @@ def returns_structseq_pyi(signature: PythonSignature) -> tuple[str, str] | None:
structseq_def = "\n".join(structseq_def_lines)
# Example:
# structseq_def = (
# "class max(Tuple[Tensor, Tensor]):\n"
# "class max(tuple[Tensor, Tensor]):\n"
# " @property\n"
# " def values(self) -> Tensor: ...\n"
# " @property\n"
# " def indices(self) -> Tensor: ...\n"
# " def __new__(cls, sequence: Tuple[Tensor, Tensor]): ...\n"
# " def __new__(cls, sequence: tuple[Tensor, Tensor]): ...\n"
# " n_fields: _int = 2",
# " n_sequeunce_fields: _int = 2",
# " n_unnamed_fields: _int = 0",
@ -1060,7 +1059,7 @@ def returns_str_pyi(signature: PythonSignature) -> str:
python_returns = [return_type_str_pyi(r.type) for r in signature.returns.returns]
if len(python_returns) > 1:
return "Tuple[" + ", ".join(python_returns) + "]"
return "tuple[" + ", ".join(python_returns) + "]"
if len(python_returns) == 1:
return python_returns[0]
return "None"