mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Port NumPy typing testing style to PyTorch (#52408)
Summary: ref: https://github.com/pytorch/pytorch/issues/16574 Pull Request resolved: https://github.com/pytorch/pytorch/pull/52408 Reviewed By: anjali411 Differential Revision: D26654687 Pulled By: malfet fbshipit-source-id: 6feb603d8fb03c2ba2a01468bfde1a9901e889fd
This commit is contained in:
committed by
Facebook GitHub Bot
parent
17bc70e6f7
commit
cb68039363
@ -172,6 +172,7 @@ USE_PYTEST_LIST = [
|
||||
'distributions/test_constraints',
|
||||
'distributions/test_transforms',
|
||||
'distributions/test_utils',
|
||||
'test_typing',
|
||||
]
|
||||
|
||||
WINDOWS_BLOCKLIST = [
|
||||
|
149
test/test_typing.py
Normal file
149
test/test_typing.py
Normal file
@ -0,0 +1,149 @@
|
||||
# based on NumPy numpy/typing/tests/test_typing.py
|
||||
|
||||
import itertools
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
from typing import IO, Dict, List
|
||||
|
||||
import pytest
|
||||
|
||||
try:
|
||||
from mypy import api
|
||||
except ImportError:
|
||||
NO_MYPY = True
|
||||
else:
|
||||
NO_MYPY = False
|
||||
|
||||
|
||||
DATA_DIR = os.path.join(os.path.dirname(__file__), "typing")
|
||||
REVEAL_DIR = os.path.join(DATA_DIR, "reveal")
|
||||
MYPY_INI = os.path.join(DATA_DIR, os.pardir, os.pardir, "mypy.ini")
|
||||
CACHE_DIR = os.path.join(DATA_DIR, ".mypy_cache")
|
||||
|
||||
#: A dictionary with file names as keys and lists of the mypy stdout as values.
|
||||
#: To-be populated by `run_mypy`.
|
||||
OUTPUT_MYPY: Dict[str, List[str]] = {}
|
||||
|
||||
|
||||
def _key_func(key: str) -> str:
|
||||
"""Split at the first occurance of the ``:`` character.
|
||||
|
||||
Windows drive-letters (*e.g.* ``C:``) are ignored herein.
|
||||
"""
|
||||
drive, tail = os.path.splitdrive(key)
|
||||
return os.path.join(drive, tail.split(":", 1)[0])
|
||||
|
||||
|
||||
@pytest.mark.skipif(NO_MYPY, reason="Mypy is not installed")
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def run_mypy() -> None:
|
||||
"""Clears the cache and run mypy before running any of the typing tests.
|
||||
|
||||
The mypy results are cached in `OUTPUT_MYPY` for further use.
|
||||
|
||||
"""
|
||||
if os.path.isdir(CACHE_DIR):
|
||||
shutil.rmtree(CACHE_DIR)
|
||||
|
||||
for directory in (REVEAL_DIR,):
|
||||
# Run mypy
|
||||
stdout, stderr, _ = api.run(
|
||||
[
|
||||
"--show-absolute-path",
|
||||
"--config-file",
|
||||
MYPY_INI,
|
||||
"--cache-dir",
|
||||
CACHE_DIR,
|
||||
directory,
|
||||
]
|
||||
)
|
||||
assert not stderr, directory
|
||||
stdout = stdout.replace("*", "")
|
||||
|
||||
# Parse the output
|
||||
iterator = itertools.groupby(stdout.split("\n"), key=_key_func)
|
||||
OUTPUT_MYPY.update((k, list(v)) for k, v in iterator if k)
|
||||
|
||||
|
||||
def get_test_cases(directory):
|
||||
for root, _, files in os.walk(directory):
|
||||
for fname in files:
|
||||
if os.path.splitext(fname)[-1] == ".py":
|
||||
fullpath = os.path.join(root, fname)
|
||||
# Use relative path for nice py.test name
|
||||
relpath = os.path.relpath(fullpath, start=directory)
|
||||
|
||||
yield pytest.param(
|
||||
fullpath,
|
||||
# Manually specify a name for the test
|
||||
id=relpath,
|
||||
)
|
||||
|
||||
|
||||
#: A dictionary with all supported format keys (as keys)
|
||||
#: and matching values
|
||||
FORMAT_DICT: Dict[str, str] = {}
|
||||
|
||||
|
||||
def _parse_reveals(file: IO[str]) -> List[str]:
|
||||
"""Extract and parse all ``" # E: "`` comments from the passed file-like object.
|
||||
|
||||
All format keys will be substituted for their respective value from `FORMAT_DICT`,
|
||||
*e.g.* ``"{float64}"`` becomes ``"numpy.floating[numpy.typing._64Bit]"``.
|
||||
"""
|
||||
string = file.read().replace("*", "")
|
||||
|
||||
# Grab all `# E:`-based comments
|
||||
comments_array = list(map(lambda str: str.partition(" # E: ")[2], string.split("\n")))
|
||||
comments = "/n".join(comments_array)
|
||||
|
||||
# Only search for the `{*}` pattern within comments,
|
||||
# otherwise there is the risk of accidently grabbing dictionaries and sets
|
||||
key_set = set(re.findall(r"\{(.*?)\}", comments))
|
||||
kwargs = {
|
||||
k: FORMAT_DICT.get(k, f"<UNRECOGNIZED FORMAT KEY {k!r}>") for k in key_set
|
||||
}
|
||||
fmt_str = comments.format(**kwargs)
|
||||
|
||||
return fmt_str.split("/n")
|
||||
|
||||
|
||||
@pytest.mark.skipif(NO_MYPY, reason="Mypy is not installed")
|
||||
@pytest.mark.parametrize("path", get_test_cases(REVEAL_DIR))
|
||||
def test_reveal(path):
|
||||
__tracebackhide__ = True
|
||||
|
||||
with open(path) as fin:
|
||||
lines = _parse_reveals(fin)
|
||||
|
||||
output_mypy = OUTPUT_MYPY
|
||||
assert path in output_mypy
|
||||
for error_line in output_mypy[path]:
|
||||
match = re.match(
|
||||
r"^.+\.py:(?P<lineno>\d+): note: .+$",
|
||||
error_line,
|
||||
)
|
||||
if match is None:
|
||||
raise ValueError(f"Unexpected reveal line format: {error_line}")
|
||||
lineno = int(match.group("lineno")) - 1
|
||||
assert "Revealed type is" in error_line
|
||||
|
||||
marker = lines[lineno]
|
||||
_test_reveal(path, marker, error_line, 1 + lineno)
|
||||
|
||||
|
||||
_REVEAL_MSG = """Reveal mismatch at line {}
|
||||
|
||||
Expected reveal: {!r}
|
||||
Observed reveal: {!r}
|
||||
"""
|
||||
|
||||
|
||||
def _test_reveal(path: str, reveal: str, expected_reveal: str, lineno: int) -> None:
|
||||
if reveal not in expected_reveal:
|
||||
raise AssertionError(_REVEAL_MSG.format(lineno, expected_reveal, reveal))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__])
|
114
test/typing/reveal/tensor_constructors.py
Normal file
114
test/typing/reveal/tensor_constructors.py
Normal file
@ -0,0 +1,114 @@
|
||||
import torch
|
||||
from torch.testing._internal.common_utils import TEST_NUMPY
|
||||
if TEST_NUMPY:
|
||||
import numpy as np
|
||||
|
||||
# From the docs, there are quite a few ways to create a tensor:
|
||||
# https://pytorch.org/docs/stable/tensors.html
|
||||
|
||||
# torch.tensor()
|
||||
reveal_type(torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.tensor([0, 1])) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.tensor([[0.11111, 0.222222, 0.3333333]],
|
||||
dtype=torch.float64,
|
||||
device=torch.device('cuda:0'))) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.tensor(3.14159)) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.sparse_coo_tensor
|
||||
i = torch.tensor([[0, 1, 1],
|
||||
[2, 0, 2]]) # E: torch.tensor.Tensor
|
||||
v = torch.tensor([3, 4, 5], dtype=torch.float32) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.sparse_coo_tensor(i, v, [2, 4])) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.sparse_coo_tensor(i, v)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.sparse_coo_tensor(i, v, [2, 4],
|
||||
dtype=torch.float64,
|
||||
device=torch.device('cuda:0'))) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.sparse_coo_tensor(torch.empty([1, 0]), [], [1])) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.sparse_coo_tensor(torch.empty([1, 0]),
|
||||
torch.empty([0, 2]), [1, 2])) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.as_tensor
|
||||
if TEST_NUMPY:
|
||||
a = np.array([1, 2, 3])
|
||||
reveal_type(torch.as_tensor(a)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.as_tensor(a, device=torch.device('cuda'))) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.as_strided
|
||||
x = torch.randn(3, 3)
|
||||
reveal_type(torch.as_strided(x, (2, 2), (1, 2))) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.as_strided(x, (2, 2), (1, 2), 1)) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.from_numpy
|
||||
if TEST_NUMPY:
|
||||
a = np.array([1, 2, 3])
|
||||
reveal_type(torch.from_numpy(a)) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.zeros/zeros_like
|
||||
reveal_type(torch.zeros(2, 3)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.zeros(5)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.zeros_like(torch.empty(2, 3))) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.ones/ones_like
|
||||
reveal_type(torch.ones(2, 3)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.ones(5)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.ones_like(torch.empty(2, 3))) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.arange
|
||||
reveal_type(torch.arange(5)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.arange(1, 4)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.arange(1, 2.5, 0.5)) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.range
|
||||
reveal_type(torch.range(1, 4)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.range(1, 4, 0.5)) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.linspace
|
||||
reveal_type(torch.linspace(3, 10, steps=5)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.linspace(-10, 10, steps=5)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.linspace(start=-10, end=10, steps=5)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.linspace(start=-10, end=10, steps=1)) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.logspace
|
||||
reveal_type(torch.logspace(start=-10, end=10, steps=5)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.logspace(start=0.1, end=1.0, steps=5)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.logspace(start=0.1, end=1.0, steps=1)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.logspace(start=2, end=2, steps=1, base=2)) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.eye
|
||||
reveal_type(torch.eye(3)) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.empty/empty_like/empty_strided
|
||||
reveal_type(torch.empty(2, 3)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.empty_like(torch.empty(2, 3), dtype=torch.int64)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.empty_strided((2, 3), (1, 2))) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.full/full_like
|
||||
reveal_type(torch.full((2, 3), 3.141592)) # E: torch.tensor.Tensor
|
||||
reveal_type(torch.full_like(torch.full((2, 3), 3.141592), 2.71828)) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.quantize_per_tensor
|
||||
reveal_type(torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8)) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.quantize_per_channel
|
||||
x = torch.tensor([[-1.0, 0.0], [1.0, 2.0]])
|
||||
quant = torch.quantize_per_channel(x, torch.tensor([0.1, 0.01]), torch.tensor([10, 0]), 0, torch.quint8)
|
||||
reveal_type(x) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.dequantize
|
||||
reveal_type(torch.dequantize(x)) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.complex
|
||||
real = torch.tensor([1, 2], dtype=torch.float32)
|
||||
imag = torch.tensor([3, 4], dtype=torch.float32)
|
||||
reveal_type(torch.complex(real, imag)) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.polar
|
||||
abs = torch.tensor([1, 2], dtype=torch.float64)
|
||||
pi = torch.acos(torch.zeros(1)).item() * 2
|
||||
angle = torch.tensor([pi / 2, 5 * pi / 4], dtype=torch.float64)
|
||||
reveal_type(torch.polar(abs, angle)) # E: torch.tensor.Tensor
|
||||
|
||||
# torch.heaviside
|
||||
inp = torch.tensor([-1.5, 0, 2.0])
|
||||
values = torch.tensor([0.5])
|
||||
reveal_type(torch.heaviside(inp, values)) # E: torch.tensor.Tensor
|
Reference in New Issue
Block a user