Skip to content

Commit 70b0499

Browse files
committed
feat: Add ty as a linter
1 parent f26ea9c commit 70b0499

File tree

19 files changed

+478
-4
lines changed

19 files changed

+478
-4
lines changed

.github/workflows/mirror.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ jobs:
1818
npx @bazel/buildifier lint/ruff_versions.bzl
1919
cd docs
2020
bazel run update
21+
- name: Update ty
22+
run: |
23+
./lint/mirror_ty.sh
24+
npx @bazel/buildifier lint/ty_versions.bzl
25+
cd docs
26+
bazel run update
2127
- name: Download and Extract Latest Multitool
2228
run: wget -O- https://github.com/theoremlp/multitool/releases/download/v0.11.0/multitool-x86_64-unknown-linux-gnu.tar.xz | tar --strip-components=1 -xJf -
2329
- name: Find Updates and Render Lockfile

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Linters which are not language-specific:
4747
| Kotlin | [ktfmt] | [ktlint] |
4848
| Markdown | [Prettier] | [Vale] |
4949
| Protocol Buffer | [buf] | [buf lint] |
50-
| Python | [ruff] | [flake8], [pylint], [ruff] |
50+
| Python | [ruff] | [flake8], [pylint], [ruff], [ty] |
5151
| Ruby | | [RuboCop] |
5252
| Rust | [rustfmt] | |
5353
| SQL | [prettier-plugin-sql] | |
@@ -86,6 +86,7 @@ Linters which are not language-specific:
8686
[scalafmt]: https://scalameta.org/scalafmt
8787
[rubocop]: https://docs.rubocop.org/
8888
[ruff]: https://docs.astral.sh/ruff/
89+
[ty]: https://docs.astral.sh/ty/
8990
[pylint]: https://pylint.readthedocs.io/en/stable/
9091
[shellcheck]: https://www.shellcheck.net/
9192
[shfmt]: https://github.com/mvdan/sh

example/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ exports_files(
3030
"ktlint-baseline.xml",
3131
".clang-tidy",
3232
"spotbugs-exclude.xml",
33+
"ty.toml",
3334
],
3435
visibility = ["//visibility:public"],
3536
)

example/lint.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ esac
3535
args=()
3636
if [ $machine == "Windows" ]; then
3737
# avoid missing linters on windows platform
38-
args=("--aspects=$(echo //tools/lint:linters.bzl%{flake8,pylint,pmd,ruff,vale,yamllint,clang_tidy} | tr ' ' ',')")
38+
args=("--aspects=$(echo //tools/lint:linters.bzl%{flake8,pylint,pmd,ruff,ty,vale,yamllint,clang_tidy} | tr ' ' ',')")
3939
else
40-
args=("--aspects=$(echo //tools/lint:linters.bzl%{buf,eslint,flake8,keep_sorted,ktlint,pmd,pylint,ruff,shellcheck,stylelint,vale,yamllint,clang_tidy,spotbugs} | tr ' ' ',')")
40+
args=("--aspects=$(echo //tools/lint:linters.bzl%{buf,eslint,flake8,keep_sorted,ktlint,pmd,pylint,ruff,ty,shellcheck,stylelint,vale,yamllint,clang_tidy,spotbugs} | tr ' ' ',')")
4141
fi
4242

4343
# NB: perhaps --remote_download_toplevel is needed as well with remote execution?

example/src/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ py_library(
9090
srcs = ["unused_import.py"],
9191
)
9292

93+
py_library(
94+
name = "unsupported_operator",
95+
srcs = ["unsupported_operator.py"],
96+
)
97+
98+
py_library(
99+
name = "call_non_callable",
100+
srcs = ["call_non_callable.py"],
101+
)
102+
93103
filegroup(
94104
name = "unused_import_pyi",
95105
srcs = ["unused_import.pyi"],

example/src/call_non_callable.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Demo with just running ty:
2+
# $ ./lint.sh src:call_non_callable
3+
B = 1
4+
# This error should be ignored, as it is specified as ignored in src/ty.toml
5+
B()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Demo with just running ty from the examples dir:
2+
# $ ./lint.sh src:unsupported_operator
3+
a = 10 + "test"

example/test/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ load(
1515
"rubocop_test",
1616
"ruff_test",
1717
"shellcheck_test",
18+
"ty_test",
1819
)
1920

2021
write_file(
@@ -78,6 +79,11 @@ py_library(
7879
srcs = ["excluded.py"],
7980
)
8081

82+
py_library(
83+
name = "unsupported_operator",
84+
srcs = ["unsupported_operator.py"],
85+
)
86+
8187
java_library(
8288
name = "generated_java",
8389
srcs = ["generated.java"],
@@ -139,6 +145,13 @@ pylint_test(
139145
tags = ["manual"],
140146
)
141147

148+
ty_test(
149+
name = "ty_should_fail_unsupported_operator",
150+
srcs = ["//src:unsupported_operator"],
151+
# Expected to fail based on current content of the file.
152+
tags = ["manual"],
153+
)
154+
142155
pmd_test(
143156
name = "pmd",
144157
srcs = ["//src:foo"],

example/test/lint_test.bats

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ EOF
2121
src/unused_import.py:15:0: C0301: Line too long (180/120) (line-too-long)
2222
src/unused_import.py:22:6: W1302: Invalid format string (bad-format-string)
2323
src/unused_import.py:18:0: W0611: Unused import os (unused-import)
24+
EOF
25+
26+
# Ty
27+
echo <<"EOF" | assert_output --partial
28+
error[unsupported-operator]: Operator `+` is unsupported between objects of type `Literal[10]` and `Literal["test"]`
29+
--> src/unsupported_operator.py:3:5
30+
|
31+
1 | # Demo with just running ty from the examples dir:
32+
2 | # $ ./lint.sh src:unsupported_operator
33+
3 | a = 10 + "test"
34+
| ^^^^^^^^^^^
35+
|
36+
info: rule `unsupported-operator` is enabled by default
37+
38+
Found 1 diagnostic
39+
EOF
40+
41+
# pylint
42+
echo <<"EOF" | assert_output --partial
43+
src/unused_import.py:15:0: C0301: Line too long (180/120) (line-too-long)
44+
src/unused_import.py:22:6: W1302: Invalid format string (bad-format-string)
45+
src/unused_import.py:18:0: W0611: Unused import os (unused-import)
2446
EOF
2547

2648
# Flake8

example/test/machine_outputs/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load(
99
"machine_ruff_report",
1010
"machine_shellcheck_report",
1111
"machine_stylelint_report",
12+
"machine_ty_report",
1213
"machine_vale_report",
1314
"machine_yamllint_report",
1415
"report_test",
@@ -38,6 +39,18 @@ report_test(
3839
report = "machine_ruff_pyi_report",
3940
)
4041

42+
machine_ty_report(
43+
name = "machine_ty_report",
44+
src = "//src:unsupported_operator",
45+
)
46+
47+
report_test(
48+
name = "ty_machine_output_test",
49+
expected_tool = "Ty",
50+
expected_uri = "src/unsupported_operator.py",
51+
report = "machine_ty_report",
52+
)
53+
4154
machine_shellcheck_report(
4255
name = "machine_shellcheck_report",
4356
src = "//src:hello_shell",

0 commit comments

Comments
 (0)