Skip to content

Commit d098118

Browse files
authored
[ty] disable division-by-zero by default (astral-sh#18220)
## Summary I think `division-by-zero` is a low-value diagnostic in general; most real division-by-zero errors (especially those that are less obvious to the human eye) will occur on values typed as `int`, in which case we don't issue the diagnostic anyway. Mypy and pyright do not emit this diagnostic. Currently the diagnostic is prone to false positives because a) we do not silence it in unreachable code, and b) we do not implement narrowing of literals from inequality checks. We will probably fix (a) regardless, but (b) is low priority apart from division-by-zero. I think we have many more important things to do and should not allow false positives on a low-value diagnostic to be a distraction. Not opposed to re-enabling this diagnostic in future when we can prioritize reducing its false positives. References astral-sh/ty#443 ## Test Plan Existing tests.
1 parent 7917269 commit d098118

File tree

5 files changed

+43
-85
lines changed

5 files changed

+43
-85
lines changed

.github/mypy-primer-ty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
[rules]
66
possibly-unresolved-reference = "warn"
77
unused-ignore-comment = "warn"
8+
division-by-zero = "warn"

crates/ty/docs/rules.md

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ty/tests/cli.rs

Lines changed: 17 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -387,22 +387,11 @@ fn configuration_rule_severity() -> anyhow::Result<()> {
387387
"#,
388388
)?;
389389

390-
// Assert that there's an `unresolved-reference` diagnostic (error)
391-
// and a `division-by-zero` diagnostic (error).
392-
assert_cmd_snapshot!(case.command(), @r"
390+
// Assert that there's an `unresolved-reference` diagnostic (error).
391+
assert_cmd_snapshot!(case.command(), @r###"
393392
success: false
394393
exit_code: 1
395394
----- stdout -----
396-
error[division-by-zero]: Cannot divide object of type `Literal[4]` by zero
397-
--> test.py:2:5
398-
|
399-
2 | y = 4 / 0
400-
| ^^^^^
401-
3 |
402-
4 | for a in range(0, int(y)):
403-
|
404-
info: rule `division-by-zero` is enabled by default
405-
406395
error[unresolved-reference]: Name `prin` used when not defined
407396
--> test.py:7:1
408397
|
@@ -413,17 +402,17 @@ fn configuration_rule_severity() -> anyhow::Result<()> {
413402
|
414403
info: rule `unresolved-reference` is enabled by default
415404
416-
Found 2 diagnostics
405+
Found 1 diagnostic
417406
418407
----- stderr -----
419408
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
420-
");
409+
"###);
421410

422411
case.write_file(
423412
"pyproject.toml",
424413
r#"
425414
[tool.ty.rules]
426-
division-by-zero = "warn" # demote to warn
415+
division-by-zero = "warn" # promote to warn
427416
unresolved-reference = "ignore"
428417
"#,
429418
)?;
@@ -468,9 +457,9 @@ fn cli_rule_severity() -> anyhow::Result<()> {
468457
"#,
469458
)?;
470459

471-
// Assert that there's an `unresolved-reference` diagnostic (error),
472-
// a `division-by-zero` (error) and a unresolved-import (error) diagnostic by default.
473-
assert_cmd_snapshot!(case.command(), @r"
460+
// Assert that there's an `unresolved-reference` diagnostic (error)
461+
// and an unresolved-import (error) diagnostic by default.
462+
assert_cmd_snapshot!(case.command(), @r###"
474463
success: false
475464
exit_code: 1
476465
----- stdout -----
@@ -485,18 +474,6 @@ fn cli_rule_severity() -> anyhow::Result<()> {
485474
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
486475
info: rule `unresolved-import` is enabled by default
487476
488-
error[division-by-zero]: Cannot divide object of type `Literal[4]` by zero
489-
--> test.py:4:5
490-
|
491-
2 | import does_not_exit
492-
3 |
493-
4 | y = 4 / 0
494-
| ^^^^^
495-
5 |
496-
6 | for a in range(0, int(y)):
497-
|
498-
info: rule `division-by-zero` is enabled by default
499-
500477
error[unresolved-reference]: Name `prin` used when not defined
501478
--> test.py:9:1
502479
|
@@ -507,11 +484,11 @@ fn cli_rule_severity() -> anyhow::Result<()> {
507484
|
508485
info: rule `unresolved-reference` is enabled by default
509486
510-
Found 3 diagnostics
487+
Found 2 diagnostics
511488
512489
----- stderr -----
513490
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
514-
");
491+
"###);
515492

516493
assert_cmd_snapshot!(
517494
case
@@ -575,22 +552,11 @@ fn cli_rule_severity_precedence() -> anyhow::Result<()> {
575552
"#,
576553
)?;
577554

578-
// Assert that there's a `unresolved-reference` diagnostic (error)
579-
// and a `division-by-zero` (error) by default.
580-
assert_cmd_snapshot!(case.command(), @r"
555+
// Assert that there's a `unresolved-reference` diagnostic (error) by default.
556+
assert_cmd_snapshot!(case.command(), @r###"
581557
success: false
582558
exit_code: 1
583559
----- stdout -----
584-
error[division-by-zero]: Cannot divide object of type `Literal[4]` by zero
585-
--> test.py:2:5
586-
|
587-
2 | y = 4 / 0
588-
| ^^^^^
589-
3 |
590-
4 | for a in range(0, int(y)):
591-
|
592-
info: rule `division-by-zero` is enabled by default
593-
594560
error[unresolved-reference]: Name `prin` used when not defined
595561
--> test.py:7:1
596562
|
@@ -601,11 +567,11 @@ fn cli_rule_severity_precedence() -> anyhow::Result<()> {
601567
|
602568
info: rule `unresolved-reference` is enabled by default
603569
604-
Found 2 diagnostics
570+
Found 1 diagnostic
605571
606572
----- stderr -----
607573
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
608-
");
574+
"###);
609575

610576
assert_cmd_snapshot!(
611577
case
@@ -614,7 +580,6 @@ fn cli_rule_severity_precedence() -> anyhow::Result<()> {
614580
.arg("unresolved-reference")
615581
.arg("--warn")
616582
.arg("division-by-zero")
617-
// Override the error severity with warning
618583
.arg("--ignore")
619584
.arg("unresolved-reference"),
620585
@r"
@@ -1103,18 +1068,10 @@ fn check_specific_paths() -> anyhow::Result<()> {
11031068

11041069
assert_cmd_snapshot!(
11051070
case.command(),
1106-
@r"
1071+
@r###"
11071072
success: false
11081073
exit_code: 1
11091074
----- stdout -----
1110-
error[division-by-zero]: Cannot divide object of type `Literal[4]` by zero
1111-
--> project/main.py:2:5
1112-
|
1113-
2 | y = 4 / 0 # error: division-by-zero
1114-
| ^^^^^
1115-
|
1116-
info: rule `division-by-zero` is enabled by default
1117-
11181075
error[unresolved-import]: Cannot resolve imported module `main2`
11191076
--> project/other.py:2:6
11201077
|
@@ -1135,11 +1092,11 @@ fn check_specific_paths() -> anyhow::Result<()> {
11351092
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
11361093
info: rule `unresolved-import` is enabled by default
11371094
1138-
Found 3 diagnostics
1095+
Found 2 diagnostics
11391096
11401097
----- stderr -----
11411098
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
1142-
"
1099+
"###
11431100
);
11441101

11451102
// Now check only the `tests` and `other.py` files.

crates/ty_python_semantic/src/types/diagnostic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ declare_lint! {
232232
pub(crate) static DIVISION_BY_ZERO = {
233233
summary: "detects division by zero",
234234
status: LintStatus::preview("1.0.0"),
235-
default_level: Level::Error,
235+
default_level: Level::Ignore,
236236
}
237237
}
238238

ty.schema.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)