mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-21 07:41:53 +00:00
[ty] Add version hint for failed stdlib attribute accesses (#20909)
This is the ultra-minimal implementation of * https://github.com/astral-sh/ty/issues/296 that was previously discussed as a good starting point. In particular we don't actually bother trying to figure out the exact python versions, but we still mention "hey btw for No Reason At All... you're on python 3.10" when you try to access something that has a definition rooted in the stdlib that we believe exists sometimes.
This commit is contained in:
parent
a67e0690f2
commit
7155a62e5c
7 changed files with 215 additions and 73 deletions
|
@ -2558,6 +2558,28 @@ class C:
|
|||
reveal_type(C().x) # revealed: Unknown | tuple[Divergent, Literal[1]]
|
||||
```
|
||||
|
||||
## Attributes of standard library modules that aren't yet defined
|
||||
|
||||
For attributes of stdlib modules that exist in future versions, we can give better diagnostics.
|
||||
|
||||
<!-- snapshot-diagnostics -->
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python-version = "3.10"
|
||||
```
|
||||
|
||||
`main.py`:
|
||||
|
||||
```py
|
||||
import datetime
|
||||
|
||||
# error: [unresolved-attribute]
|
||||
reveal_type(datetime.UTC) # revealed: Unknown
|
||||
# error: [unresolved-attribute]
|
||||
reveal_type(datetime.fakenotreal) # revealed: Unknown
|
||||
```
|
||||
|
||||
## References
|
||||
|
||||
Some of the tests in the *Class and instance variables* section draw inspiration from
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
---
|
||||
mdtest name: attributes.md - Attributes - Attributes of standard library modules that aren't yet defined
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/attributes.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## main.py
|
||||
|
||||
```
|
||||
1 | import datetime
|
||||
2 |
|
||||
3 | # error: [unresolved-attribute]
|
||||
4 | reveal_type(datetime.UTC) # revealed: Unknown
|
||||
5 | # error: [unresolved-attribute]
|
||||
6 | reveal_type(datetime.fakenotreal) # revealed: Unknown
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[unresolved-attribute]: Type `<module 'datetime'>` has no attribute `UTC`
|
||||
--> src/main.py:4:13
|
||||
|
|
||||
3 | # error: [unresolved-attribute]
|
||||
4 | reveal_type(datetime.UTC) # revealed: Unknown
|
||||
| ^^^^^^^^^^^^
|
||||
5 | # error: [unresolved-attribute]
|
||||
6 | reveal_type(datetime.fakenotreal) # revealed: Unknown
|
||||
|
|
||||
info: Python 3.10 was assumed when accessing `UTC` because it was specified on the command line
|
||||
info: rule `unresolved-attribute` is enabled by default
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
error[unresolved-attribute]: Type `<module 'datetime'>` has no attribute `fakenotreal`
|
||||
--> src/main.py:6:13
|
||||
|
|
||||
4 | reveal_type(datetime.UTC) # revealed: Unknown
|
||||
5 | # error: [unresolved-attribute]
|
||||
6 | reveal_type(datetime.fakenotreal) # revealed: Unknown
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
info: rule `unresolved-attribute` is enabled by default
|
||||
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue