-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Static and run-time tests for stubs #917
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
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4e8d414
Convention for test data which uses APIs defined by Python stubs
vlasovskikh 4066d7f
Run-time + static tests for stubs
vlasovskikh 4f2e158
Mentioned both static and run-time checking in CONTRIBUTING
vlasovskikh 992df42
Made runtime_test.py executable
vlasovskikh 774d5ff
Ignore flake8 warnings about imports not at the top of the file for a…
vlasovskikh ba61c11
Removed debug output for checking Python version at Travis
vlasovskikh 41d5b7c
Install *_requirements.txt for *_test.py automatically during test run
vlasovskikh aa4ace1
Added a section about testing stubs with an example
vlasovskikh dfca122
Temporarily turned off running pytype for test_data
vlasovskikh 3a3c6ad
Reverted running pytype_test (pytd, as it turned out) for *.py files
vlasovskikh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import re | ||
import pip | ||
import pytest | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def requirements(request): | ||
requirements_path = re.sub(r'(.*)_test\.py', r'\1_requirements.txt', | ||
request.module.__file__) | ||
pip.main(['install', '-r', requirements_path]) | ||
yield | ||
# We could uninstall everything here after the module tests finish |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[pytest] | ||
usefixtures = requirements |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
def test_namedtuple(): | ||
from collections import namedtuple | ||
|
||
Point = namedtuple('Point', 'x y') | ||
p = Point(1, 2) | ||
|
||
assert p == Point(1, 2) | ||
assert p == (1, 2) | ||
assert p._replace(y=3.14).y == 3.14 | ||
assert p._asdict()['x'] == 1 | ||
assert (p.x, p.y) == (1, 2) | ||
assert p[0] + p[1] == 3 | ||
assert p.index(1) == 0 | ||
assert Point._make([1, 3.14]).y == 3.14 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
six==1.10.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def test_python_checks(): | ||
from six import PY2, PY3 | ||
|
||
assert PY2 ^ PY3 | ||
|
||
|
||
def test_xrange(): | ||
from six.moves import xrange | ||
|
||
xs = xrange(5) | ||
assert xs.__iter__ | ||
assert xs[0] == 0 | ||
assert sum(xs) == 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env python | ||
|
||
import pytest | ||
import sys | ||
import os | ||
|
||
|
||
def main(): | ||
if sys.version_info < (3, 0): | ||
version_dirs = [ | ||
'2and3', | ||
'2', | ||
] | ||
else: | ||
version_dirs = [ | ||
'2and3', | ||
'3', | ||
'%d.%d' % (sys.version_info[0], sys.version_info[1]), | ||
] | ||
top_dirs = ['stdlib', 'third_party'] | ||
possible_paths = [os.path.join('test_data', t, v) | ||
for t in top_dirs | ||
for v in version_dirs] | ||
paths = [path for path in possible_paths if os.path.exists(path)] | ||
pytest.main(paths) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This should actually be all subdirectories from
sys.version_info[1]
upwards: when you're running 3.4 for example type checkers will look at the subdirectories for 3.3 and 3.4 but not 3.5.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.
@JelleZijlstra I believe it doesn't apply to run-time checks since we run them on a particular version of the interpreter and it may not have some module from older Python versions.
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.
But in typeshed directories like 3.4 apply also to newer versions. For example, asyncio is in stdlib/3.4 because it was introduced in 3.4, but the stubs are also used for 3.5 and 3.6. I think tests should behave the same.
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.
If there was some backwards incompatible change in the API of a module in older Python 3, the tests for the current version may fail. If it is absolutely necessary to run tests for some older Python 3.N module, we will set up another Travis environment with Python 3.N.
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.
This may be true, but it's certainly confusing, since you are using the same directory naming scheme as for the stubs, and for stubs the rule that Jelle explains is essential (otherwise e.g. asyncio would not be found for Python 3.5 or higher).
It also points, again, to yet another reason why I don't believe these runtime tests are going to help much (it's not checking that the
sys.version_info
checks in the stubs are correct).