-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate .pyi's using stubgen from mypy (#17521)
Add mypy (and dependencies) as new externals. Add a rule to generate `.pyi`s for `pydrake` using the same and a tiny wrapping helper script. For now, this must be manually invoked, and some functions have degraded type information (see #17520). However, this will give us the ability to manually test the generated `.pyi` files. Adding logic to install them and/or otherwise bundle them can come later.
- Loading branch information
1 parent
c501554
commit ba73941
Showing
16 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,8 @@ | ||
"""Command-line tool to generate Drake's Python Interface files (.pyi).""" | ||
|
||
import sys | ||
|
||
from mypy import stubgen | ||
|
||
if __name__ == "__main__": | ||
sys.exit(stubgen.main()) |
This file contains 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,31 @@ | ||
"""Tests generation of Drake's Python Interface files (.pyi).""" | ||
|
||
import os | ||
import unittest | ||
|
||
from mypy import stubgen | ||
|
||
|
||
class TestStubgen(unittest.TestCase): | ||
# TODO(mwoehlke-kitware): test the already-generated files instead. | ||
def test_generation(self): | ||
"""Ensure that stubgen runs and generates output. | ||
For now, this is more or less just a smoke test, with a very cursory | ||
check that the output is 'reasonable'. | ||
""" | ||
output_dir = os.environ['TEST_TMPDIR'] | ||
args = ['--package', 'pydrake', '--output', output_dir] | ||
|
||
# Generate stubs. | ||
result = stubgen.main(args) | ||
self.assertTrue(result is None or result == 0) | ||
|
||
# Find some of the expected output and look for an expected function. | ||
expected = os.path.join(output_dir, 'pydrake', '__init__.pyi') | ||
found_expected_decl = False | ||
for line in open(expected, 'r'): | ||
if line.startswith('def getDrakePath():'): | ||
found_expected_decl = True | ||
break | ||
self.assertTrue(found_expected_decl) |
This file contains 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 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,8 @@ | ||
# -*- python -*- | ||
|
||
# This file exists to make our directory into a Bazel package, so that our | ||
# neighboring *.bzl file can be loaded elsewhere. | ||
|
||
load("//tools/lint:lint.bzl", "add_lint_tests") | ||
|
||
add_lint_tests() |
12 changes: 12 additions & 0 deletions
12
tools/workspace/mypy_extensions_internal/package.BUILD.bazel
This file contains 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 @@ | ||
# -*- python -*- | ||
|
||
load("@drake//tools/skylark:py.bzl", "py_library") | ||
|
||
licenses(["notice"]) # MIT | ||
|
||
py_library( | ||
name = "mypy_extensions", | ||
srcs = ["mypy_extensions.py"], | ||
imports = ["."], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains 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,15 @@ | ||
# -*- python -*- | ||
|
||
load("@drake//tools/workspace:github.bzl", "github_archive") | ||
|
||
def mypy_extensions_internal_repository( | ||
name, | ||
mirrors = None): | ||
github_archive( | ||
name = name, | ||
repository = "python/mypy_extensions", | ||
commit = "0.4.3", | ||
sha256 = "21e830f4baf996d0a9bd7048a5b23722dbd3b88354b8bf0e22376f9a8d508c16", # noqa | ||
build_file = ":package.BUILD.bazel", | ||
mirrors = mirrors, | ||
) |
This file contains 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,8 @@ | ||
# -*- python -*- | ||
|
||
# This file exists to make our directory into a Bazel package, so that our | ||
# neighboring *.bzl file can be loaded elsewhere. | ||
|
||
load("//tools/lint:lint.bzl", "add_lint_tests") | ||
|
||
add_lint_tests() |
This file contains 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,17 @@ | ||
# -*- python -*- | ||
|
||
load("@drake//tools/skylark:py.bzl", "py_library") | ||
|
||
licenses(["notice"]) # Python-2.0 | ||
|
||
py_library( | ||
name = "mypy", | ||
srcs = glob(["mypy/**/*.py"]), | ||
data = glob(["mypy/typeshed/**"]), | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@mypy_extensions_internal//:mypy_extensions", | ||
"@tomli_internal//:tomli", | ||
"@typing_extensions_internal//:typing_extensions", | ||
], | ||
) |
This file contains 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,16 @@ | ||
# -*- python -*- | ||
|
||
load("@drake//tools/workspace:github.bzl", "github_archive") | ||
|
||
def mypy_internal_repository( | ||
name, | ||
mirrors = None): | ||
github_archive( | ||
name = name, | ||
repository = "python/mypy", | ||
# TODO(mwoehlke-kitware): switch to a tag >= v0.980. | ||
commit = "3ae19a25f0a39358ede1383e93d44ef9abf165e0", | ||
sha256 = "866503aed58d7207c0fe4bca9a6a51c1eaa6668157b1c5ed61b40eda71af9175", # noqa | ||
build_file = ":package.BUILD.bazel", | ||
mirrors = mirrors, | ||
) |
This file contains 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,8 @@ | ||
# -*- python -*- | ||
|
||
# This file exists to make our directory into a Bazel package, so that our | ||
# neighboring *.bzl file can be loaded elsewhere. | ||
|
||
load("//tools/lint:lint.bzl", "add_lint_tests") | ||
|
||
add_lint_tests() |
This file contains 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 @@ | ||
# -*- python -*- | ||
|
||
load("@drake//tools/skylark:py.bzl", "py_library") | ||
|
||
licenses(["notice"]) # MIT | ||
|
||
py_library( | ||
name = "tomli", | ||
srcs = glob(["src/tomli/*.py"]), | ||
imports = ["src"], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains 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,15 @@ | ||
# -*- python -*- | ||
|
||
load("@drake//tools/workspace:github.bzl", "github_archive") | ||
|
||
def tomli_internal_repository( | ||
name, | ||
mirrors = None): | ||
github_archive( | ||
name = name, | ||
repository = "hukkin/tomli", | ||
commit = "2.0.1", | ||
sha256 = "ad22dbc128623e0c156ffaff019f29f456eba8a5d5a05164dd34f63e560449df", # noqa | ||
build_file = ":package.BUILD.bazel", | ||
mirrors = mirrors, | ||
) |
This file contains 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,8 @@ | ||
# -*- python -*- | ||
|
||
# This file exists to make our directory into a Bazel package, so that our | ||
# neighboring *.bzl file can be loaded elsewhere. | ||
|
||
load("//tools/lint:lint.bzl", "add_lint_tests") | ||
|
||
add_lint_tests() |
12 changes: 12 additions & 0 deletions
12
tools/workspace/typing_extensions_internal/package.BUILD.bazel
This file contains 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 @@ | ||
# -*- python -*- | ||
|
||
load("@drake//tools/skylark:py.bzl", "py_library") | ||
|
||
licenses(["notice"]) # Python-2.0 | ||
|
||
py_library( | ||
name = "typing_extensions", | ||
srcs = ["src/typing_extensions.py"], | ||
imports = ["src"], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains 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,15 @@ | ||
# -*- python -*- | ||
|
||
load("@drake//tools/workspace:github.bzl", "github_archive") | ||
|
||
def typing_extensions_internal_repository( | ||
name, | ||
mirrors = None): | ||
github_archive( | ||
name = name, | ||
repository = "python/typing_extensions", | ||
commit = "4.3.0", | ||
sha256 = "9dbc928aed2839a23d210726697700a1c4593ab3bbf82b981fcc44585a47ce30", # noqa | ||
build_file = ":package.BUILD.bazel", | ||
mirrors = mirrors, | ||
) |