Repository navigation
Commit fb2f921
authored
[ty] Fix narrowing of strings and integers compared with enum members (#29743)
A plain string can compare equal to a `StrEnum` member. Previously, `if
value == Color.RED` narrowed a `str` to the enum and allowed
`value.name`, even when `value` was the string `"red"`. The same problem
affected integers compared with `IntEnum` members.
Keep the matching `str` or `int` type in equality checks, tuple
membership tests, and `match` value patterns. A value already known to
belong to the enum can still narrow to a specific member.
Closes astral-sh/ty#4827
## Test plan
Ran the reproducer with Python 3.13. The plain values `"a"` and `1`
entered all three kinds of branches, and `.name` on the string raised
`AttributeError`. With both default and strict equality settings, ty now
reveals `str` and `int` in those branches and reports the invalid
`.name` access.
## Ecosystem report
Apprise loses a false warning about converting a plain string to an enum
member. pip gains an assignment error where typeshed requires
`VerifyMode` and CPython also accepts the plain integer `2`. Both
changes are expected. [Analyzed head
`e0388646c9f0d4ac09dfea732b05ca2d84839950`](e038864)
· [Full ecosystem
report](#29743 (comment))1 parent d181287 commit fb2f921
4 files changed
Lines changed: 60 additions & 10 deletions
File tree
- crates/ty_python_semantic
- resources/mdtest/narrow
- conditionals
- src/types
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
864 | 864 | | |
865 | 865 | | |
866 | 866 | | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
867 | 890 | | |
868 | 891 | | |
869 | 892 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1442 | 1442 | | |
1443 | 1443 | | |
1444 | 1444 | | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
| 1452 | + | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
1445 | 1458 | | |
1446 | 1459 | | |
1447 | 1460 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4032 | 4032 | | |
4033 | 4033 | | |
4034 | 4034 | | |
| 4035 | + | |
| 4036 | + | |
| 4037 | + | |
| 4038 | + | |
| 4039 | + | |
| 4040 | + | |
4035 | 4041 | | |
4036 | 4042 | | |
4037 | 4043 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1530 | 1530 | | |
1531 | 1531 | | |
1532 | 1532 | | |
1533 | | - | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
| 1536 | + | |
| 1537 | + | |
| 1538 | + | |
| 1539 | + | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
1534 | 1547 | | |
1535 | 1548 | | |
1536 | 1549 | | |
| |||
1539 | 1552 | | |
1540 | 1553 | | |
1541 | 1554 | | |
1542 | | - | |
| 1555 | + | |
1543 | 1556 | | |
1544 | 1557 | | |
1545 | 1558 | | |
| |||
1564 | 1577 | | |
1565 | 1578 | | |
1566 | 1579 | | |
1567 | | - | |
1568 | | - | |
1569 | | - | |
1570 | | - | |
1571 | | - | |
| 1580 | + | |
1572 | 1581 | | |
1573 | | - | |
1574 | 1582 | | |
1575 | | - | |
| 1583 | + | |
1576 | 1584 | | |
1577 | | - | |
| 1585 | + | |
1578 | 1586 | | |
1579 | 1587 | | |
1580 | 1588 | | |
| |||
0 commit comments