[ty] Format conflicting types as an enumeration (#18956)

## Summary

Format conflicting declared types as
```
`str`, `int` and `bytes`
```

Thanks to @AlexWaygood for the initial draft.

@dcreager, looking forward to your one-character follow-up PR.
This commit is contained in:
David Peter 2025-06-26 14:29:33 +02:00 committed by GitHub
parent c0beb3412f
commit 86fd9b634e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 90 additions and 83 deletions

View file

@ -16,7 +16,7 @@ def _(flag: bool):
else:
x: int
x = 1 # error: [conflicting-declarations] "Conflicting declared types for `x`: str, int"
x = 1 # error: [conflicting-declarations] "Conflicting declared types for `x`: `str` and `int`"
```
## Incompatible declarations for 2 (out of 3) types
@ -29,7 +29,7 @@ def _(flag1: bool, flag2: bool):
x: int
# Here, the declared type for `x` is `int | str | Unknown`.
x = 1 # error: [conflicting-declarations] "Conflicting declared types for `x`: str, int"
x = 1 # error: [conflicting-declarations] "Conflicting declared types for `x`: `str` and `int`"
```
## Incompatible declarations with repeated types
@ -47,7 +47,7 @@ def _(flag1: bool, flag2: bool, flag3: bool, flag4: bool):
else:
x: bytes
x = "a" # error: [conflicting-declarations] "Conflicting declared types for `x`: str, int, bytes"
x = "a" # error: [conflicting-declarations] "Conflicting declared types for `x`: `str`, `int` and `bytes`"
```
## Incompatible declarations with bad assignment