Skip to content

Convention for test data which uses APIs defined by Python stubs #862

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 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ are important to the project's success.
* [Contact us](#discussion) before starting significant work.
* IMPORTANT: For new libraries, [get permission from the library owner first](#adding-a-new-library).
* Create your stubs [conforming to the coding style](#stub-file-coding-style).
* Add tests for your stubs to `test_data`. These tests are not meant
to be executable. Type checkers analyze them statically.
* Make sure `runtests.sh` passes cleanly on Mypy, pytype, and flake8.
4. [Submit your changes](#submitting-changes):
* Open a pull request
Expand Down
15 changes: 12 additions & 3 deletions stdlib/2/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,17 @@ def cast(tp: Type[_T], obj: Any) -> _T: ...

# Type constructors

# NamedTuple is special-cased in the type checker; the initializer is ignored.
def NamedTuple(typename: str, fields: Iterable[Tuple[str, Any]], *,
verbose: bool = ..., rename: bool = ...) -> Type[tuple]: ...
# NamedTuple is special-cased in the type checker
class NamedTuple(tuple):
_fields = ... # type: Tuple[str, ...]

def __init__(self, typename: str, fields: Iterable[Tuple[str, Any]], *,
verbose: bool = ..., rename: bool = ...) -> None: ...

@classmethod
def _make(cls, iterable: Iterable[Any]) -> NamedTuple: ...

def _asdict(self) -> dict: ...
def _replace(self, **kwargs: Any) -> NamedTuple: ...

def NewType(name: str, tp: Type[_T]) -> Type[_T]: ...
15 changes: 12 additions & 3 deletions stdlib/3/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,17 @@ def cast(tp: Type[_T], obj: Any) -> _T: ...

# Type constructors

# NamedTuple is special-cased in the type checker; the initializer is ignored.
def NamedTuple(typename: str, fields: Iterable[Tuple[str, Any]], *,
verbose: bool = ..., rename: bool = ..., module: str = None) -> Type[tuple]: ...
# NamedTuple is special-cased in the type checker
class NamedTuple(tuple):
_fields = ... # type: Tuple[str, ...]

def __init__(self, typename: str, fields: Iterable[Tuple[str, Any]], *,
verbose: bool = ..., rename: bool = ..., module: Any = ...) -> None: ...

@classmethod
def _make(cls, iterable: Iterable[Any]) -> NamedTuple: ...

def _asdict(self) -> dict: ...
def _replace(self, **kwargs: Any) -> NamedTuple: ...

def NewType(name: str, tp: Type[_T]) -> Type[_T]: ...
14 changes: 14 additions & 0 deletions test_data/stdlib/2and3/collections_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from collections import namedtuple


def test_collections_namedtuple():
# type: () -> None
Point = namedtuple('Point', 'x y')
p = Point(1, 2)

p._replace(y=3.14)
p._asdict()
p.x, p.y
p[0] + p[1]
p.index(1)
Point._make([('x', 1), ('y', 3.14)])
14 changes: 14 additions & 0 deletions test_data/stdlib/2and3/typing_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import NamedTuple


def test_typing_namedtuple():
# type: () -> None
Point = NamedTuple('Point', [('x', float), ('y', float)])
p = Point(1, 2)

p._replace(y=3.14)
p._asdict()
p.x, p.y
p[0] + p[1]
p.index(1)
Point._make([('x', 1), ('y', 3.14)])
16 changes: 16 additions & 0 deletions test_data/stdlib/3.6/typing_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import NamedTuple


class Point(NamedTuple):
x: float
y: float


def test_typing_namedtuple():
# type: () -> None
p = Point(1, 2)
p._asdict()
p.x, p.y
p[0] + p[1]
p.index(1)
Point._make([('x', 1), ('y', 3.14)])
2 changes: 1 addition & 1 deletion tests/mypy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def libpath(major, minor):
versions.append('2and3')
paths = []
for v in versions:
for top in ['stdlib', 'third_party']:
for top in ['stdlib', 'third_party', 'test_data/stdlib', 'test_data/third_party']:
p = os.path.join(top, v)
if os.path.isdir(p):
paths.append(p)
Expand Down
11 changes: 10 additions & 1 deletion tests/pytype_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def pytype_test(args):
print("Cannot run pytd. Did you install pytype?")
return 0, 0

wanted = re.compile(r"stdlib/(2|2\.7|2and3)/.*\.pyi$")
wanted = re.compile(r"stdlib/(2|2\.7|2and3)/.*\.pyi?$")
skipped = re.compile("(%s)$" % "|".join(load_blacklist()))
files = []

Expand All @@ -80,6 +80,15 @@ def pytype_test(args):
if wanted.search(f) and not skipped.search(f):
files.append(f)

# XXX: Temporarily disabled checking test_data/ due to a bug
# in pytype related to handling type hints in function comments

# for root, _, filenames in os.walk("test_data/stdlib"):
# for f in sorted(filenames):
# f = os.path.join(root, f)
# if wanted.search(f) and not skipped.search(f):
# files.append(f)

running_tests = collections.deque()
max_code, runs, errors = 0, 0, 0
print("Running pytype tests...")
Expand Down