[ty] Improve error messages for unresolved attribute diagnostics (#20963)

## Summary

- Type checkers (and type-checker authors) think in terms of types, but
I think most Python users think in terms of values. Rather than saying
that a _type_ `X` "has no attribute `foo`" (which I think sounds strange
to many users), say that "an object of type `X` has no attribute `foo`"
- Special-case certain types so that the diagnostic messages read more
like normal English: rather than saying "Type `<class 'Foo'>` has no
attribute `bar`" or "Object of type `<class 'Foo'>` has no attribute
`bar`", just say "Class `Foo` has no attribute `bar`"

## Test Plan

Mdtests and snapshots updated
This commit is contained in:
Alex Waygood 2025-10-19 10:58:25 +01:00 committed by GitHub
parent b6b96d75eb
commit 1f8297cfe6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 102 additions and 63 deletions

View file

@ -30,7 +30,7 @@ fn config_override_python_version() -> anyhow::Result<()> {
success: false
exit_code: 1
----- stdout -----
error[unresolved-attribute]: Type `<module 'sys'>` has no attribute `last_exc`
error[unresolved-attribute]: Module `sys` has no member `last_exc`
--> test.py:5:7
|
4 | # Access `sys.last_exc` that was only added in Python 3.12
@ -962,7 +962,7 @@ fn defaults_to_a_new_python_version() -> anyhow::Result<()> {
success: false
exit_code: 1
----- stdout -----
error[unresolved-attribute]: Type `<module 'os'>` has no attribute `grantpt`
error[unresolved-attribute]: Module `os` has no member `grantpt`
--> main.py:4:1
|
2 | import os

View file

@ -1112,11 +1112,11 @@ print(sys.last_exc, os.getegid())
assert_eq!(diagnostics.len(), 2);
assert_eq!(
diagnostics[0].primary_message(),
"Type `<module 'sys'>` has no attribute `last_exc`"
"Module `sys` has no member `last_exc`"
);
assert_eq!(
diagnostics[1].primary_message(),
"Type `<module 'os'>` has no attribute `getegid`"
"Module `os` has no member `getegid`"
);
// Change the python version