Compare commits
9 Commits
micha/mdte
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
87eec9bb51 | ||
|
|
eaed0d9b5c | ||
|
|
eb96456e1e | ||
|
|
3e0299488e | ||
|
|
7f0ce3e88d | ||
|
|
ba0736385d | ||
|
|
e41f045ec5 | ||
|
|
b24afb643c | ||
|
|
853bb00626 |
194
crates/ty/docs/rules.md
generated
194
crates/ty/docs/rules.md
generated
@@ -8,7 +8,7 @@
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.20">0.0.1-alpha.20</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20ambiguous-protocol-member" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L542" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L543" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ def test(): -> "int":
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20call-non-callable" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L141" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L142" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ Calling a non-callable object will raise a `TypeError` at runtime.
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.7">0.0.7</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20call-top-callable" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L159" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L160" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -135,7 +135,7 @@ def f(x: object):
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20conflicting-argument-forms" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L210" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L211" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -167,7 +167,7 @@ f(int) # error
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20conflicting-declarations" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L236" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L237" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -198,7 +198,7 @@ a = 1
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20conflicting-metaclass" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L261" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L262" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -230,7 +230,7 @@ class C(A, B): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20cyclic-class-definition" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L287" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L288" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -262,7 +262,7 @@ class B(A): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Preview (since <a href="https://github.com/astral-sh/ty/releases/tag/1.0.0">1.0.0</a>) ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20cyclic-type-alias-definition" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L313" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L314" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -290,7 +290,7 @@ type B = A
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.16">0.0.1-alpha.16</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20deprecated" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L357" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L358" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -317,7 +317,7 @@ old_func() # emits [deprecated] diagnostic
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'ignore'."><code>ignore</code></a> ·
|
||||
Preview (since <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a>) ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20division-by-zero" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L335" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L336" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -346,7 +346,7 @@ false positives it can produce.
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20duplicate-base" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L378" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L379" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -373,7 +373,7 @@ class B(A, A): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.12">0.0.1-alpha.12</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20duplicate-kw-only" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L399" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L400" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -529,7 +529,7 @@ def test(): -> "Literal[5]":
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20inconsistent-mro" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L625" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L626" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -559,7 +559,7 @@ class C(A, B): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20index-out-of-bounds" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L649" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L650" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -579,13 +579,47 @@ t = (0, 1, 2)
|
||||
t[3] # IndexError: tuple index out of range
|
||||
```
|
||||
|
||||
## `ineffective-final`
|
||||
|
||||
<small>
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Preview (since <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.33">0.0.1-alpha.33</a>) ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20ineffective-final" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1793" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
**What it does**
|
||||
|
||||
Checks for calls to `final()` that type checkers cannot interpret.
|
||||
|
||||
**Why is this bad?**
|
||||
|
||||
The `final()` function is designed to be used as a decorator. When called directly
|
||||
as a function (e.g., `final(type(...))`), type checkers will not understand the
|
||||
application of `final` and will not prevent subclassing.
|
||||
|
||||
**Example**
|
||||
|
||||
|
||||
```python
|
||||
from typing import final
|
||||
|
||||
# Incorrect: type checkers will not prevent subclassing
|
||||
MyClass = final(type("MyClass", (), {}))
|
||||
|
||||
# Correct: use `final` as a decorator
|
||||
@final
|
||||
class MyClass: ...
|
||||
```
|
||||
|
||||
## `instance-layout-conflict`
|
||||
|
||||
<small>
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.12">0.0.1-alpha.12</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20instance-layout-conflict" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L431" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L432" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -674,7 +708,7 @@ an atypical memory layout.
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-argument-type" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L703" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L704" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -701,7 +735,7 @@ func("foo") # error: [invalid-argument-type]
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-assignment" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L743" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L744" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -729,7 +763,7 @@ a: int = ''
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-attribute-access" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2122" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2151" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -763,7 +797,7 @@ C.instance_var = 3 # error: Cannot assign to instance variable
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.19">0.0.1-alpha.19</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-await" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L765" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L766" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -799,7 +833,7 @@ asyncio.run(main())
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-base" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L795" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L796" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -823,7 +857,7 @@ class A(42): ... # error: [invalid-base]
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-context-manager" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L880" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L881" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -850,7 +884,7 @@ with 1:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-declaration" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L901" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L902" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -879,7 +913,7 @@ a: str
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-exception-caught" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L924" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L925" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -923,7 +957,7 @@ except ZeroDivisionError:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.28">0.0.1-alpha.28</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-explicit-override" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1792" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1821" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -965,7 +999,7 @@ class D(A):
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.35">0.0.1-alpha.35</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-frozen-dataclass-subclass" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2373" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2402" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1009,7 +1043,7 @@ class NonFrozenChild(FrozenBase): # Error raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-generic-class" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1002" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1003" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1047,7 +1081,7 @@ class D(Generic[U, T]): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.12">0.0.12</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-generic-enum" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L960" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L961" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1126,7 +1160,7 @@ a = 20 / 0 # type: ignore
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.17">0.0.1-alpha.17</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-key" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L670" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L671" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1165,7 +1199,7 @@ carol = Person(name="Carol", age=25) # typo!
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-legacy-type-variable" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1033" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1034" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1200,7 +1234,7 @@ def f(t: TypeVar("U")): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-metaclass" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1130" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1131" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1234,7 +1268,7 @@ class B(metaclass=f): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.20">0.0.1-alpha.20</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-method-override" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2275" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2304" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1341,7 +1375,7 @@ Correct use of `@override` is enforced by ty's `invalid-explicit-override` rule.
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.19">0.0.1-alpha.19</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-named-tuple" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L577" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L578" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1395,7 +1429,7 @@ AttributeError: Cannot overwrite NamedTuple attribute _asdict
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Preview (since <a href="https://github.com/astral-sh/ty/releases/tag/1.0.0">1.0.0</a>) ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-newtype" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1106" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1107" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1425,7 +1459,7 @@ Baz = NewType("Baz", int | str) # error: invalid base for `typing.NewType`
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-overload" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1157" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1158" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1475,7 +1509,7 @@ def foo(x: int) -> int: ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-parameter-default" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1256" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1257" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1501,7 +1535,7 @@ def f(a: int = ''): ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-paramspec" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1061" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1062" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1532,7 +1566,7 @@ P2 = ParamSpec("S2") # error: ParamSpec name must match the variable it's assig
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-protocol" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L513" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L514" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1566,7 +1600,7 @@ TypeError: Protocols can only inherit from other protocols, got <class 'int'>
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-raise" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1276" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1277" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1615,7 +1649,7 @@ def g():
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-return-type" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L724" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L725" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1640,7 +1674,7 @@ def func() -> int:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-super-argument" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1319" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1320" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1736,7 +1770,7 @@ class C: ...
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.10">0.0.10</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-total-ordering" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2411" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2440" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1782,7 +1816,7 @@ class MyClass:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.6">0.0.1-alpha.6</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-alias-type" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1085" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1086" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1809,7 +1843,7 @@ NewAlias = TypeAliasType(get_name(), int) # error: TypeAliasType name mus
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.29">0.0.1-alpha.29</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-arguments" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1551" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1552" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1856,7 +1890,7 @@ Bar[int] # error: too few arguments
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-checking-constant" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1358" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1359" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1886,7 +1920,7 @@ TYPE_CHECKING = ''
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-form" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1382" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1383" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1916,7 +1950,7 @@ b: Annotated[int] # `Annotated` expects at least two arguments
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.11">0.0.1-alpha.11</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-guard-call" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1434" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1435" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1950,7 +1984,7 @@ f(10) # Error
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.11">0.0.1-alpha.11</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-guard-definition" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1406" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1407" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1984,7 +2018,7 @@ class C:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-type-variable-constraints" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1462" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1463" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2019,7 +2053,7 @@ T = TypeVar('T', bound=str) # valid bound TypeVar
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.9">0.0.9</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20invalid-typed-dict-statement" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2250" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2279" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2050,7 +2084,7 @@ class Foo(TypedDict):
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20missing-argument" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1491" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1492" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2075,7 +2109,7 @@ func() # TypeError: func() missing 1 required positional argument: 'x'
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.20">0.0.1-alpha.20</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20missing-typed-dict-key" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2223" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2252" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2108,7 +2142,7 @@ alice["age"] # KeyError
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20no-matching-overload" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1510" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1511" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2137,7 +2171,7 @@ func("string") # error: [no-matching-overload]
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20not-iterable" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1592" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1593" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2163,7 +2197,7 @@ for i in 34: # TypeError: 'int' object is not iterable
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20not-subscriptable" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1533" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1534" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2187,7 +2221,7 @@ Subscripting an object that does not support it will raise a `TypeError` at runt
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.29">0.0.1-alpha.29</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20override-of-final-method" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1765" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1766" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2220,7 +2254,7 @@ class B(A):
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20parameter-already-assigned" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1643" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1644" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2247,7 +2281,7 @@ f(1, x=2) # Error raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.22">0.0.1-alpha.22</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20positional-only-parameter-as-kwarg" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1976" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2005" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2274,7 +2308,7 @@ f(x=1) # Error raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.22">0.0.1-alpha.22</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20possibly-missing-attribute" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1664" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1665" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2302,7 +2336,7 @@ A.c # AttributeError: type object 'A' has no attribute 'c'
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.22">0.0.1-alpha.22</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20possibly-missing-implicit-call" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L184" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L185" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2334,7 +2368,7 @@ A()[0] # TypeError: 'A' object is not subscriptable
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'ignore'."><code>ignore</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.22">0.0.1-alpha.22</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20possibly-missing-import" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1686" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1687" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2371,7 +2405,7 @@ from module import a # ImportError: cannot import name 'a' from 'module'
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'ignore'."><code>ignore</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20possibly-unresolved-reference" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1716" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1717" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2435,7 +2469,7 @@ def test(): -> "int":
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20redundant-cast" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2150" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2179" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2462,7 +2496,7 @@ cast(int, f()) # Redundant
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20static-assert-error" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2098" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2127" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2492,7 +2526,7 @@ static_assert(int(2.0 * 3.0) == 6) # error: does not have a statically known tr
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20subclass-of-final-class" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1742" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1743" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2521,7 +2555,7 @@ class B(A): ... # Error raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Preview (since <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.30">0.0.1-alpha.30</a>) ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20super-call-in-named-tuple-method" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1910" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1939" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2555,7 +2589,7 @@ class F(NamedTuple):
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20too-many-positional-arguments" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1850" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1879" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2582,7 +2616,7 @@ f("foo") # Error raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20type-assertion-failure" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1828" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1857" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2610,7 +2644,7 @@ def _(x: int):
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unavailable-implicit-super-arguments" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1871" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1900" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2656,7 +2690,7 @@ class A:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20undefined-reveal" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1937" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1966" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2680,7 +2714,7 @@ reveal_type(1) # NameError: name 'reveal_type' is not defined
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unknown-argument" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1955" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1984" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2707,7 +2741,7 @@ f(x=1, y=2) # Error raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unresolved-attribute" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1997" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2026" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2735,7 +2769,7 @@ A().foo # AttributeError: 'A' object has no attribute 'foo'
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.15">0.0.1-alpha.15</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unresolved-global" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2171" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2200" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2793,7 +2827,7 @@ def g():
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unresolved-import" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2019" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2048" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2818,7 +2852,7 @@ import foo # ModuleNotFoundError: No module named 'foo'
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unresolved-reference" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2038" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2067" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2843,7 +2877,7 @@ print(x) # NameError: name 'x' is not defined
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.7">0.0.1-alpha.7</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unsupported-base" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L813" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L814" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2882,7 +2916,7 @@ class D(C): ... # error: [unsupported-base]
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unsupported-bool-conversion" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1612" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1613" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2919,7 +2953,7 @@ b1 < b2 < b1 # exception raised here
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'ignore'."><code>ignore</code></a> ·
|
||||
Preview (since <a href="https://github.com/astral-sh/ty/releases/tag/1.0.0">1.0.0</a>) ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unsupported-dynamic-base" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L846" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L847" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2960,7 +2994,7 @@ def factory(base: type[Base]) -> type:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20unsupported-operator" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2057" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2086" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -3024,7 +3058,7 @@ to `false` to prevent this rule from reporting unused `type: ignore` comments.
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'warn'."><code>warn</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.22">0.0.1-alpha.22</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20useless-overload-body" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1200" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1201" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -3087,7 +3121,7 @@ def foo(x: int | str) -> int | str:
|
||||
Default level: <a href="../../rules#rule-levels" title="This lint has a default level of 'error'."><code>error</code></a> ·
|
||||
Added in <a href="https://github.com/astral-sh/ty/releases/tag/0.0.1-alpha.1">0.0.1-alpha.1</a> ·
|
||||
<a href="https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20zero-stepsize-in-slice" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2079" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L2108" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
|
||||
@@ -2585,7 +2585,7 @@ type<CURSOR>
|
||||
|
||||
assert_snapshot!(
|
||||
test.type_signatures().skip_auto_import().build().snapshot(),
|
||||
@r"
|
||||
@"
|
||||
type :: <class 'type'>
|
||||
TypeError :: <class 'TypeError'>
|
||||
",
|
||||
@@ -4025,7 +4025,7 @@ quux.<CURSOR>
|
||||
__module__ :: str
|
||||
__mul__ :: bound method Quux.__mul__(value: SupportsIndex, /) -> tuple[int | str, ...]
|
||||
__ne__ :: bound method Quux.__ne__(value: object, /) -> bool
|
||||
__new__ :: (x: int, y: str) -> None
|
||||
__new__ :: (x: int, y: str) -> Quux
|
||||
__orig_bases__ :: tuple[Any, ...]
|
||||
__reduce__ :: bound method Quux.__reduce__() -> str | tuple[Any, ...]
|
||||
__reduce_ex__ :: bound method Quux.__reduce_ex__(protocol: SupportsIndex, /) -> str | tuple[Any, ...]
|
||||
@@ -8009,7 +8009,7 @@ my_list[0].remove<CURSOR>
|
||||
);
|
||||
assert_snapshot!(
|
||||
builder.build().snapshot(),
|
||||
@r"
|
||||
@"
|
||||
removeprefix
|
||||
removesuffix
|
||||
",
|
||||
@@ -8030,7 +8030,7 @@ def f(x: Any | str):
|
||||
);
|
||||
assert_snapshot!(
|
||||
builder.build().snapshot(),
|
||||
@r"
|
||||
@"
|
||||
removeprefix
|
||||
removesuffix
|
||||
",
|
||||
@@ -8052,7 +8052,7 @@ def f(x: Intersection[int, Any] | str):
|
||||
);
|
||||
assert_snapshot!(
|
||||
builder.build().snapshot(),
|
||||
@r"
|
||||
@"
|
||||
removeprefix
|
||||
removesuffix
|
||||
",
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
use regex::Regex;
|
||||
use ruff_python_trivia::{PythonWhitespace, leading_indentation};
|
||||
use ruff_source_file::UniversalNewlines;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
@@ -349,16 +350,37 @@ fn render_markdown(docstring: &str) -> String {
|
||||
| "versionchanged" | "version-changed" | "version-deprecated" | "deprecated"
|
||||
| "version-removed" | "versionremoved",
|
||||
) => {
|
||||
// Map version directives to human-readable phrases (matching Sphinx output)
|
||||
let pretty_directive = match directive.unwrap() {
|
||||
"versionadded" | "version-added" => Cow::Borrowed("Added in version"),
|
||||
"versionchanged" | "version-changed" => Cow::Borrowed("Changed in version"),
|
||||
"deprecated" | "version-deprecated" => {
|
||||
Cow::Borrowed("Deprecated since version")
|
||||
}
|
||||
"versionremoved" | "version-removed" => Cow::Borrowed("Removed in version"),
|
||||
other => Cow::Owned(
|
||||
other
|
||||
.char_indices()
|
||||
.map(|(index, c)| {
|
||||
if index == 0 {
|
||||
c.to_ascii_uppercase()
|
||||
} else {
|
||||
c
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
};
|
||||
|
||||
// Render the argument of things like `.. version-added:: 4.0`
|
||||
let suffix = if let Some(lang) = lang {
|
||||
format!(" *{lang}*")
|
||||
format!(" {lang}")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
// We prepend without_directive here out of caution for preserving input.
|
||||
// This is probably gibberish/invalid syntax? But it's a no-op in normal cases.
|
||||
temp_owned_line =
|
||||
format!("**{without_directive}{}:**{suffix}", directive.unwrap());
|
||||
temp_owned_line = format!("**{without_directive}{pretty_directive}{suffix}:**");
|
||||
|
||||
line = temp_owned_line.as_str();
|
||||
None
|
||||
@@ -1076,7 +1098,7 @@ mod tests {
|
||||
assert_snapshot!(docstring.render_markdown(), @r#"
|
||||
The thing you need to understand is that computers are hard.
|
||||
|
||||
**warning:**
|
||||
**Warning:**
|
||||
Now listen here buckaroo you might have seen me say computers are hard,
|
||||
and though "yeah I know computers are hard but NO you DON'T KNOW.
|
||||
|
||||
@@ -1115,12 +1137,12 @@ mod tests {
|
||||
assert_snapshot!(docstring.render_markdown(), @"
|
||||
Some much-updated docs
|
||||
|
||||
**version-added:** *3.0*
|
||||
**Added in version 3.0:**
|
||||
Function added
|
||||
|
||||
**version-changed:** *4.0*
|
||||
**Changed in version 4.0:**
|
||||
The `spam` argument was added
|
||||
**version-changed:** *4.1*
|
||||
**Changed in version 4.1:**
|
||||
The `spam` argument is considered evil now.
|
||||
|
||||
You really shouldnt use it
|
||||
@@ -1141,7 +1163,7 @@ mod tests {
|
||||
let docstring = Docstring::new(docstring.to_owned());
|
||||
|
||||
assert_snapshot!(docstring.render_markdown(), @"
|
||||
**wow this is some changes deprecated:** *1.2.3*
|
||||
**wow this is some changes Deprecated since version 1.2.3:**
|
||||
x = 2
|
||||
");
|
||||
}
|
||||
@@ -1151,7 +1173,7 @@ mod tests {
|
||||
fn explicit_markdown_block_with_ps1_contents() {
|
||||
let docstring = r#"
|
||||
My cool func:
|
||||
|
||||
|
||||
```python
|
||||
>>> thing.do_thing()
|
||||
wow it did the thing
|
||||
@@ -1162,7 +1184,7 @@ mod tests {
|
||||
|
||||
let docstring = Docstring::new(docstring.to_owned());
|
||||
|
||||
assert_snapshot!(docstring.render_markdown(), @r"
|
||||
assert_snapshot!(docstring.render_markdown(), @"
|
||||
My cool func:
|
||||
|
||||
```python
|
||||
@@ -1179,7 +1201,7 @@ mod tests {
|
||||
fn explicit_markdown_block_with_underscore_contents_tick() {
|
||||
let docstring = r#"
|
||||
My cool func:
|
||||
|
||||
|
||||
`````python
|
||||
x_y = thing_do();
|
||||
``` # this should't close the fence!
|
||||
@@ -1189,7 +1211,7 @@ mod tests {
|
||||
|
||||
let docstring = Docstring::new(docstring.to_owned());
|
||||
|
||||
assert_snapshot!(docstring.render_markdown(), @r"
|
||||
assert_snapshot!(docstring.render_markdown(), @"
|
||||
My cool func:
|
||||
|
||||
`````python
|
||||
@@ -1205,7 +1227,7 @@ mod tests {
|
||||
fn explicit_markdown_block_with_underscore_contents_tilde() {
|
||||
let docstring = r#"
|
||||
My cool func:
|
||||
|
||||
|
||||
~~~~~python
|
||||
x_y = thing_do();
|
||||
~~~ # this should't close the fence!
|
||||
@@ -1215,7 +1237,7 @@ mod tests {
|
||||
|
||||
let docstring = Docstring::new(docstring.to_owned());
|
||||
|
||||
assert_snapshot!(docstring.render_markdown(), @r"
|
||||
assert_snapshot!(docstring.render_markdown(), @"
|
||||
My cool func:
|
||||
|
||||
~~~~~python
|
||||
@@ -1233,7 +1255,7 @@ mod tests {
|
||||
fn explicit_markdown_block_with_indent_tick() {
|
||||
let docstring = r#"
|
||||
My cool func...
|
||||
|
||||
|
||||
Returns:
|
||||
Some details
|
||||
`````python
|
||||
@@ -1246,7 +1268,7 @@ mod tests {
|
||||
|
||||
let docstring = Docstring::new(docstring.to_owned());
|
||||
|
||||
assert_snapshot!(docstring.render_markdown(), @r"
|
||||
assert_snapshot!(docstring.render_markdown(), @"
|
||||
My cool func...
|
||||
|
||||
Returns:
|
||||
@@ -1267,7 +1289,7 @@ mod tests {
|
||||
fn explicit_markdown_block_with_indent_tilde() {
|
||||
let docstring = r#"
|
||||
My cool func...
|
||||
|
||||
|
||||
Returns:
|
||||
Some details
|
||||
~~~~~~python
|
||||
@@ -1280,7 +1302,7 @@ mod tests {
|
||||
|
||||
let docstring = Docstring::new(docstring.to_owned());
|
||||
|
||||
assert_snapshot!(docstring.render_markdown(), @r"
|
||||
assert_snapshot!(docstring.render_markdown(), @"
|
||||
My cool func...
|
||||
|
||||
Returns:
|
||||
@@ -1299,14 +1321,14 @@ mod tests {
|
||||
fn explicit_markdown_block_with_unclosed_fence_tick() {
|
||||
let docstring = r#"
|
||||
My cool func:
|
||||
|
||||
|
||||
````python
|
||||
x_y = thing_do();
|
||||
"#;
|
||||
|
||||
let docstring = Docstring::new(docstring.to_owned());
|
||||
|
||||
assert_snapshot!(docstring.render_markdown(), @r"
|
||||
assert_snapshot!(docstring.render_markdown(), @"
|
||||
My cool func:
|
||||
|
||||
````python
|
||||
@@ -1320,14 +1342,14 @@ mod tests {
|
||||
fn explicit_markdown_block_with_unclosed_fence_tilde() {
|
||||
let docstring = r#"
|
||||
My cool func:
|
||||
|
||||
|
||||
~~~~~python
|
||||
x_y = thing_do();
|
||||
"#;
|
||||
|
||||
let docstring = Docstring::new(docstring.to_owned());
|
||||
|
||||
assert_snapshot!(docstring.render_markdown(), @r"
|
||||
assert_snapshot!(docstring.render_markdown(), @"
|
||||
My cool func:
|
||||
|
||||
~~~~~python
|
||||
@@ -1342,7 +1364,7 @@ mod tests {
|
||||
fn explicit_markdown_block_messy_corners_tick() {
|
||||
let docstring = r#"
|
||||
My cool func:
|
||||
|
||||
|
||||
``````we still think this is a codefence```
|
||||
x_y = thing_do();
|
||||
```````````` and are sloppy as heck with indentation and closing shrugggg
|
||||
@@ -1350,7 +1372,7 @@ mod tests {
|
||||
|
||||
let docstring = Docstring::new(docstring.to_owned());
|
||||
|
||||
assert_snapshot!(docstring.render_markdown(), @r"
|
||||
assert_snapshot!(docstring.render_markdown(), @"
|
||||
My cool func:
|
||||
|
||||
``````we still think this is a codefence```
|
||||
@@ -1365,7 +1387,7 @@ mod tests {
|
||||
fn explicit_markdown_block_messy_corners_tilde() {
|
||||
let docstring = r#"
|
||||
My cool func:
|
||||
|
||||
|
||||
~~~~~~we still think this is a codefence~~~
|
||||
x_y = thing_do();
|
||||
~~~~~~~~~~~~~ and are sloppy as heck with indentation and closing shrugggg
|
||||
@@ -1373,7 +1395,7 @@ mod tests {
|
||||
|
||||
let docstring = Docstring::new(docstring.to_owned());
|
||||
|
||||
assert_snapshot!(docstring.render_markdown(), @r"
|
||||
assert_snapshot!(docstring.render_markdown(), @"
|
||||
My cool func:
|
||||
|
||||
~~~~~~we still think this is a codefence~~~
|
||||
|
||||
@@ -1649,6 +1649,103 @@ Traceb<CURSOR>ackType
|
||||
assert_snapshot!(test.goto_definition(), @"No goto target found");
|
||||
}
|
||||
|
||||
/// goto-definition on a dynamic class literal (created via `type()`)
|
||||
#[test]
|
||||
fn goto_definition_dynamic_class_literal() {
|
||||
let test = CursorTest::builder()
|
||||
.source(
|
||||
"main.py",
|
||||
r#"
|
||||
DynClass = type("DynClass", (), {})
|
||||
|
||||
x = DynCla<CURSOR>ss()
|
||||
"#,
|
||||
)
|
||||
.build();
|
||||
|
||||
assert_snapshot!(test.goto_definition(), @r#"
|
||||
info[goto-definition]: Go to definition
|
||||
--> main.py:4:5
|
||||
|
|
||||
2 | DynClass = type("DynClass", (), {})
|
||||
3 |
|
||||
4 | x = DynClass()
|
||||
| ^^^^^^^^ Clicking here
|
||||
|
|
||||
info: Found 2 definitions
|
||||
--> main.py:2:1
|
||||
|
|
||||
2 | DynClass = type("DynClass", (), {})
|
||||
| --------
|
||||
3 |
|
||||
4 | x = DynClass()
|
||||
|
|
||||
::: stdlib/builtins.pyi:137:9
|
||||
|
|
||||
135 | def __class__(self, type: type[Self], /) -> None: ...
|
||||
136 | def __init__(self) -> None: ...
|
||||
137 | def __new__(cls) -> Self: ...
|
||||
| -------
|
||||
138 | # N.B. `object.__setattr__` and `object.__delattr__` are heavily special-cased by type checkers.
|
||||
139 | # Overriding them in subclasses has different semantics, even if the override has an identical signature.
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
/// goto-definition on a dangling dynamic class literal (not assigned to a variable)
|
||||
#[test]
|
||||
fn goto_definition_dangling_dynamic_class_literal() {
|
||||
let test = CursorTest::builder()
|
||||
.source(
|
||||
"main.py",
|
||||
r#"
|
||||
class Foo(type("Ba<CURSOR>r", (), {})):
|
||||
pass
|
||||
"#,
|
||||
)
|
||||
.build();
|
||||
|
||||
assert_snapshot!(test.goto_definition(), @"No goto target found");
|
||||
}
|
||||
|
||||
/// goto-definition on a dynamic namedtuple class literal (created via `collections.namedtuple()`)
|
||||
#[test]
|
||||
fn goto_definition_dynamic_namedtuple_literal() {
|
||||
let test = CursorTest::builder()
|
||||
.source(
|
||||
"main.py",
|
||||
r#"
|
||||
from collections import namedtuple
|
||||
|
||||
Point = namedtuple("Point", ["x", "y"])
|
||||
|
||||
p = Poi<CURSOR>nt(1, 2)
|
||||
"#,
|
||||
)
|
||||
.build();
|
||||
|
||||
assert_snapshot!(test.goto_definition(), @r#"
|
||||
info[goto-definition]: Go to definition
|
||||
--> main.py:6:5
|
||||
|
|
||||
4 | Point = namedtuple("Point", ["x", "y"])
|
||||
5 |
|
||||
6 | p = Point(1, 2)
|
||||
| ^^^^^ Clicking here
|
||||
|
|
||||
info: Found 1 definition
|
||||
--> main.py:4:1
|
||||
|
|
||||
2 | from collections import namedtuple
|
||||
3 |
|
||||
4 | Point = namedtuple("Point", ["x", "y"])
|
||||
| -----
|
||||
5 |
|
||||
6 | p = Point(1, 2)
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
// TODO: Should only list `a: int`
|
||||
#[test]
|
||||
fn redeclarations() {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Unsupported special types
|
||||
|
||||
We do not understand the functional syntax for creating `NamedTuple`s, `TypedDict`s or `Enum`s yet.
|
||||
But we also do not emit false positives when these are used in type expressions.
|
||||
We do not understand the functional syntax for creating `TypedDict`s or `Enum`s yet. But we also do
|
||||
not emit false positives when these are used in type expressions.
|
||||
|
||||
```py
|
||||
import collections
|
||||
@@ -11,8 +11,6 @@ import typing
|
||||
MyEnum = enum.Enum("MyEnum", ["foo", "bar", "baz"])
|
||||
MyIntEnum = enum.IntEnum("MyIntEnum", ["foo", "bar", "baz"])
|
||||
MyTypedDict = typing.TypedDict("MyTypedDict", {"foo": int})
|
||||
MyNamedTuple1 = typing.NamedTuple("MyNamedTuple1", [("foo", int)])
|
||||
MyNamedTuple2 = collections.namedtuple("MyNamedTuple2", ["foo"])
|
||||
|
||||
def f(a: MyEnum, b: MyTypedDict, c: MyNamedTuple1, d: MyNamedTuple2): ...
|
||||
def f(a: MyEnum, b: MyTypedDict): ...
|
||||
```
|
||||
|
||||
@@ -20,16 +20,13 @@ class Base: ...
|
||||
class Mixin: ...
|
||||
|
||||
# We synthesize a class type using the name argument
|
||||
Foo = type("Foo", (), {})
|
||||
reveal_type(Foo) # revealed: <class 'Foo'>
|
||||
reveal_type(type("Foo", (), {})) # revealed: <class 'Foo'>
|
||||
|
||||
# With a single base class
|
||||
Foo2 = type("Foo", (Base,), {"attr": 1})
|
||||
reveal_type(Foo2) # revealed: <class 'Foo'>
|
||||
reveal_type(type("Foo", (Base,), {"attr": 1})) # revealed: <class 'Foo'>
|
||||
|
||||
# With multiple base classes
|
||||
Foo3 = type("Foo", (Base, Mixin), {})
|
||||
reveal_type(Foo3) # revealed: <class 'Foo'>
|
||||
reveal_type(type("Foo", (Base, Mixin), {})) # revealed: <class 'Foo'>
|
||||
|
||||
# The inferred type is assignable to type[Base] since Foo inherits from Base
|
||||
tests: list[type[Base]] = []
|
||||
@@ -499,39 +496,38 @@ Other numbers of arguments are invalid:
|
||||
|
||||
```py
|
||||
# error: [no-matching-overload] "No overload of class `type` matches arguments"
|
||||
reveal_type(type("Foo", ())) # revealed: Unknown
|
||||
reveal_type(type("Foo", ())) # revealed: type[Unknown]
|
||||
|
||||
# TODO: the keyword arguments for `Foo`/`Bar`/`Baz` here are invalid
|
||||
# (you cannot pass `metaclass=` to `type()`, and none of them have
|
||||
# base classes with `__init_subclass__` methods),
|
||||
# but `type[Unknown]` would be better than `Unknown` here
|
||||
# (none of them have base classes with `__init_subclass__` methods).
|
||||
#
|
||||
# The intent to create a new class is however clear,
|
||||
# so we still infer these as class-literal types.
|
||||
reveal_type(type("Foo", (), {}, weird_other_arg=42)) # revealed: <class 'Foo'>
|
||||
reveal_type(type("Bar", (int,), {}, weird_other_arg=42)) # revealed: <class 'Bar'>
|
||||
|
||||
# You can't pass `metaclass=` to the `type()` constructor, but the intent is clear,
|
||||
# so we infer `<class 'Baz'>` here rather than `type[Unknown]`
|
||||
# error: [no-matching-overload] "No overload of class `type` matches arguments"
|
||||
reveal_type(type("Foo", (), {}, weird_other_arg=42)) # revealed: Unknown
|
||||
# error: [no-matching-overload] "No overload of class `type` matches arguments"
|
||||
reveal_type(type("Bar", (int,), {}, weird_other_arg=42)) # revealed: Unknown
|
||||
# error: [no-matching-overload] "No overload of class `type` matches arguments"
|
||||
reveal_type(type("Baz", (), {}, metaclass=type)) # revealed: Unknown
|
||||
reveal_type(type("Baz", (), {}, metaclass=type)) # revealed: <class 'Baz'>
|
||||
```
|
||||
|
||||
The following calls are also invalid, due to incorrect argument types.
|
||||
|
||||
Inline calls (not assigned to a variable) fall back to regular `type` overload matching, which
|
||||
produces slightly different error messages than assigned dynamic class creation:
|
||||
The following calls are also invalid, due to incorrect argument types:
|
||||
|
||||
```py
|
||||
class Base: ...
|
||||
|
||||
# error: 6 [invalid-argument-type] "Argument to class `type` is incorrect: Expected `str`, found `Literal[b"Foo"]`"
|
||||
# error: [invalid-argument-type] "Invalid argument to parameter 1 (`name`) of `type()`: Expected `str`, found `Literal[b"Foo"]`"
|
||||
type(b"Foo", (), {})
|
||||
|
||||
# error: 13 [invalid-argument-type] "Argument to class `type` is incorrect: Expected `tuple[type, ...]`, found `<class 'Base'>`"
|
||||
# error: [invalid-argument-type] "Invalid argument to parameter 2 (`bases`) of `type()`: Expected `tuple[type, ...]`, found `<class 'Base'>`"
|
||||
type("Foo", Base, {})
|
||||
|
||||
# error: 13 [invalid-argument-type] "Argument to class `type` is incorrect: Expected `tuple[type, ...]`, found `tuple[Literal[1], Literal[2]]`"
|
||||
# error: 14 [invalid-base] "Invalid class base with type `Literal[1]`"
|
||||
# error: 17 [invalid-base] "Invalid class base with type `Literal[2]`"
|
||||
type("Foo", (1, 2), {})
|
||||
|
||||
# error: 22 [invalid-argument-type] "Argument to class `type` is incorrect: Expected `dict[str, Any]`, found `dict[str | bytes, Any]`"
|
||||
# error: [invalid-argument-type] "Invalid argument to parameter 3 (`namespace`) of `type()`: Expected `dict[str, Any]`, found `dict[Unknown | bytes, Unknown | int]`"
|
||||
type("Foo", (Base,), {b"attr": 1})
|
||||
```
|
||||
|
||||
@@ -598,6 +594,19 @@ class Y(C, B): ...
|
||||
Conflict = type("Conflict", (X, Y), {})
|
||||
```
|
||||
|
||||
## MRO error highlighting (snapshot)
|
||||
|
||||
<!-- snapshot-diagnostics -->
|
||||
|
||||
This snapshot test documents the diagnostic highlighting range for dynamic class literals.
|
||||
Currently, the entire `type()` call expression is highlighted:
|
||||
|
||||
```py
|
||||
class A: ...
|
||||
|
||||
Dup = type("Dup", (A, A), {}) # error: [duplicate-base]
|
||||
```
|
||||
|
||||
## Metaclass conflicts
|
||||
|
||||
Metaclass conflicts are detected and reported:
|
||||
@@ -853,55 +862,77 @@ def f(*args, **kwargs):
|
||||
|
||||
# Has a string first arg, but unknown additional args from *args
|
||||
B = type("B", *args, **kwargs)
|
||||
# TODO: `type[Unknown]` would cause fewer false positives
|
||||
reveal_type(B) # revealed: <class 'str'>
|
||||
reveal_type(B) # revealed: type[Unknown]
|
||||
|
||||
# Has string and tuple, but unknown additional args
|
||||
C = type("C", (), *args, **kwargs)
|
||||
# TODO: `type[Unknown]` would cause fewer false positives
|
||||
reveal_type(C) # revealed: type
|
||||
reveal_type(C) # revealed: type[Unknown]
|
||||
|
||||
# All three positional args provided, only **kwargs unknown
|
||||
D = type("D", (), {}, **kwargs)
|
||||
# TODO: `type[Unknown]` would cause fewer false positives
|
||||
reveal_type(D) # revealed: type
|
||||
reveal_type(D) # revealed: <class 'D'>
|
||||
|
||||
# Three starred expressions - we can't know how they expand
|
||||
a = ("E",)
|
||||
b = ((),)
|
||||
c = ({},)
|
||||
E = type(*a, *b, *c)
|
||||
# TODO: `type[Unknown]` would cause fewer false positives
|
||||
reveal_type(E) # revealed: type
|
||||
reveal_type(E) # revealed: type[Unknown]
|
||||
```
|
||||
|
||||
## Explicit type annotations
|
||||
|
||||
TODO: Annotated assignments with `type()` calls don't currently synthesize the specific class type.
|
||||
This will be fixed when we support all `type()` calls (including inline) via generic handling.
|
||||
When an explicit type annotation is provided, the inferred type is checked against it:
|
||||
|
||||
```py
|
||||
# The annotation `type` is compatible with the inferred class literal type
|
||||
T: type = type("T", (), {})
|
||||
reveal_type(T) # revealed: <class 'T'>
|
||||
|
||||
# The annotation `type[Base]` is compatible with the inferred type
|
||||
class Base: ...
|
||||
|
||||
# TODO: Should infer `<class 'T'>` instead of `type`
|
||||
T: type = type("T", (), {})
|
||||
reveal_type(T) # revealed: type
|
||||
|
||||
# TODO: Should infer `<class 'Derived'>` instead of `type[Base]}
|
||||
# error: [invalid-assignment] "Object of type `type` is not assignable to `type[Base]`"
|
||||
Derived: type[Base] = type("Derived", (Base,), {})
|
||||
reveal_type(Derived) # revealed: type[Base]
|
||||
reveal_type(Derived) # revealed: <class 'Derived'>
|
||||
|
||||
# Incompatible annotation produces an error
|
||||
class Unrelated: ...
|
||||
|
||||
# error: [invalid-assignment]
|
||||
Bad: type[Unrelated] = type("Bad", (Base,), {})
|
||||
```
|
||||
|
||||
## Special base classes
|
||||
|
||||
Some special base classes work with dynamic class creation, but special semantics may not be fully
|
||||
synthesized:
|
||||
synthesized.
|
||||
|
||||
### Invalid special bases
|
||||
|
||||
Dynamic classes cannot directly inherit from `Generic`, `Protocol`, or `TypedDict`. These special
|
||||
forms require class syntax for their semantics to be properly applied:
|
||||
|
||||
```py
|
||||
from typing import Generic, Protocol, TypeVar
|
||||
from typing_extensions import TypedDict
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
# error: [invalid-base] "Invalid base for class created via `type()`"
|
||||
GenericClass = type("GenericClass", (Generic[T],), {})
|
||||
|
||||
# error: [unsupported-dynamic-base] "Unsupported base for class created via `type()`"
|
||||
ProtocolClass = type("ProtocolClass", (Protocol,), {})
|
||||
|
||||
# error: [invalid-base] "Invalid base for class created via `type()`"
|
||||
TypedDictClass = type("TypedDictClass", (TypedDict,), {})
|
||||
```
|
||||
|
||||
### Protocol bases
|
||||
|
||||
Inheriting from a class that is itself a protocol is valid:
|
||||
|
||||
```py
|
||||
# Protocol bases work - the class is created as a subclass of the protocol
|
||||
from typing import Protocol
|
||||
from ty_extensions import reveal_mro
|
||||
|
||||
@@ -918,8 +949,9 @@ reveal_type(instance) # revealed: ProtoImpl
|
||||
|
||||
### TypedDict bases
|
||||
|
||||
Inheriting from a class that is itself a TypedDict is valid:
|
||||
|
||||
```py
|
||||
# TypedDict bases work but TypedDict semantics aren't applied to dynamic subclasses
|
||||
from typing_extensions import TypedDict
|
||||
from ty_extensions import reveal_mro
|
||||
|
||||
@@ -952,26 +984,26 @@ reveal_mro(Point3D) # revealed: (<class 'Point3D'>, <class 'Point'>, <class 'tu
|
||||
|
||||
### Enum bases
|
||||
|
||||
Creating a class via `type()` that inherits from any Enum class fails at runtime because `EnumMeta`
|
||||
expects special attributes in the class dict that `type()` doesn't provide:
|
||||
|
||||
```py
|
||||
# Enum subclassing via type() is not supported - EnumMeta requires special dict handling
|
||||
# that type() cannot provide. This applies even to empty enums.
|
||||
from enum import Enum
|
||||
|
||||
class Color(Enum):
|
||||
RED = 1
|
||||
GREEN = 2
|
||||
|
||||
# Enums with members are final and cannot be subclassed
|
||||
# error: [subclass-of-final-class]
|
||||
ExtendedColor = type("ExtendedColor", (Color,), {})
|
||||
|
||||
class EmptyEnum(Enum):
|
||||
pass
|
||||
|
||||
# TODO: We should emit a diagnostic here - type() cannot create Enum subclasses
|
||||
ExtendedColor = type("ExtendedColor", (Color,), {})
|
||||
reveal_type(ExtendedColor) # revealed: <class 'ExtendedColor'>
|
||||
|
||||
# Even empty enums fail - EnumMeta requires special dict handling
|
||||
# TODO: We should emit a diagnostic here too
|
||||
ValidExtension = type("ValidExtension", (EmptyEnum,), {})
|
||||
reveal_type(ValidExtension) # revealed: <class 'ValidExtension'>
|
||||
# Empty enums fail because EnumMeta requires special dict handling
|
||||
# error: [invalid-base] "Invalid base for class created via `type()`"
|
||||
InvalidExtension = type("InvalidExtension", (EmptyEnum,), {})
|
||||
```
|
||||
|
||||
## `__init_subclass__` keyword arguments
|
||||
@@ -992,9 +1024,6 @@ class Child(Base, required_arg="value"):
|
||||
# The dynamically assigned attribute has Unknown in its type
|
||||
reveal_type(Child.config) # revealed: Unknown | str
|
||||
|
||||
# Dynamic class creation - keyword arguments are not yet supported
|
||||
# TODO: This should work: type("DynamicChild", (Base,), {}, required_arg="value")
|
||||
# error: [no-matching-overload]
|
||||
DynamicChild = type("DynamicChild", (Base,), {}, required_arg="value")
|
||||
```
|
||||
|
||||
@@ -1034,3 +1063,27 @@ reveal_type(Dynamic) # revealed: <class 'Dynamic'>
|
||||
# Metaclass attributes are accessible on the class
|
||||
reveal_type(Dynamic.custom_attr) # revealed: str
|
||||
```
|
||||
|
||||
## `final()` on dynamic classes
|
||||
|
||||
Using `final()` as a function (not a decorator) on dynamic classes will not be understood by type
|
||||
checkers. The class is passed through unchanged, and subclassing will not be prevented:
|
||||
|
||||
```py
|
||||
from typing import final
|
||||
|
||||
# error: [ineffective-final]
|
||||
FinalClass = final(type("FinalClass", (), {}))
|
||||
reveal_type(FinalClass) # revealed: <class 'FinalClass'>
|
||||
|
||||
# Subclassing is allowed because type checkers don't understand `final()` called as a function
|
||||
class Child(FinalClass): ...
|
||||
|
||||
# Same with base classes
|
||||
class Base: ...
|
||||
|
||||
# error: [ineffective-final]
|
||||
FinalDerived = final(type("FinalDerived", (Base,), {}))
|
||||
|
||||
class Child2(FinalDerived): ...
|
||||
```
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
# Unsupported base for dynamic `type()` classes
|
||||
|
||||
<!-- snapshot-diagnostics -->
|
||||
|
||||
## `@final` class
|
||||
|
||||
Classes decorated with `@final` cannot be subclassed:
|
||||
|
||||
```py
|
||||
from typing import final
|
||||
|
||||
@final
|
||||
class FinalClass:
|
||||
pass
|
||||
|
||||
X = type("X", (FinalClass,), {}) # error: [subclass-of-final-class]
|
||||
```
|
||||
|
||||
## `Generic` base
|
||||
|
||||
Dynamic classes created via `type()` cannot inherit from `Generic`:
|
||||
|
||||
```py
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
X = type("X", (Generic[T],), {}) # error: [invalid-base]
|
||||
```
|
||||
|
||||
## `Protocol` base
|
||||
|
||||
Dynamic classes created via `type()` cannot inherit from `Protocol`:
|
||||
|
||||
```py
|
||||
from typing import Protocol
|
||||
|
||||
X = type("X", (Protocol,), {}) # error: [unsupported-dynamic-base]
|
||||
```
|
||||
|
||||
## `TypedDict` base
|
||||
|
||||
Dynamic classes created via `type()` cannot inherit from `TypedDict` directly. Use
|
||||
`TypedDict("Name", ...)` instead:
|
||||
|
||||
```py
|
||||
from typing_extensions import TypedDict
|
||||
|
||||
X = type("X", (TypedDict,), {}) # error: [invalid-base]
|
||||
```
|
||||
|
||||
## Enum base
|
||||
|
||||
Dynamic classes created via `type()` cannot inherit from Enum classes because `EnumMeta` expects
|
||||
special dict attributes that `type()` doesn't provide:
|
||||
|
||||
```py
|
||||
from enum import Enum
|
||||
|
||||
class MyEnum(Enum):
|
||||
pass
|
||||
|
||||
X = type("X", (MyEnum,), {}) # error: [invalid-base]
|
||||
```
|
||||
|
||||
## Enum with members
|
||||
|
||||
Enums with members are final and cannot be subclassed at all:
|
||||
|
||||
```py
|
||||
from enum import Enum
|
||||
|
||||
class Color(Enum):
|
||||
RED = 1
|
||||
GREEN = 2
|
||||
|
||||
X = type("X", (Color,), {}) # error: [subclass-of-final-class]
|
||||
```
|
||||
@@ -84,17 +84,490 @@ alice.id = 42
|
||||
bob.age = None
|
||||
```
|
||||
|
||||
Alternative functional syntax:
|
||||
Alternative functional syntax with a list of tuples:
|
||||
|
||||
```py
|
||||
Person2 = NamedTuple("Person", [("id", int), ("name", str)])
|
||||
alice2 = Person2(1, "Alice")
|
||||
|
||||
# TODO: should be an error
|
||||
# error: [missing-argument]
|
||||
Person2(1)
|
||||
|
||||
reveal_type(alice2.id) # revealed: @Todo(functional `NamedTuple` syntax)
|
||||
reveal_type(alice2.name) # revealed: @Todo(functional `NamedTuple` syntax)
|
||||
reveal_type(alice2.id) # revealed: int
|
||||
reveal_type(alice2.name) # revealed: str
|
||||
```
|
||||
|
||||
Functional syntax with a tuple of tuples:
|
||||
|
||||
```py
|
||||
Person3 = NamedTuple("Person", (("id", int), ("name", str)))
|
||||
alice3 = Person3(1, "Alice")
|
||||
|
||||
reveal_type(alice3.id) # revealed: int
|
||||
reveal_type(alice3.name) # revealed: str
|
||||
```
|
||||
|
||||
Functional syntax with a tuple of lists:
|
||||
|
||||
```py
|
||||
Person4 = NamedTuple("Person", (["id", int], ["name", str]))
|
||||
alice4 = Person4(1, "Alice")
|
||||
|
||||
reveal_type(alice4.id) # revealed: int
|
||||
reveal_type(alice4.name) # revealed: str
|
||||
```
|
||||
|
||||
Functional syntax with a list of lists:
|
||||
|
||||
```py
|
||||
Person5 = NamedTuple("Person", [["id", int], ["name", str]])
|
||||
alice5 = Person5(1, "Alice")
|
||||
|
||||
reveal_type(alice5.id) # revealed: int
|
||||
reveal_type(alice5.name) # revealed: str
|
||||
```
|
||||
|
||||
### Functional syntax with variable name
|
||||
|
||||
When the typename is passed via a variable, we can extract it from the inferred literal string type:
|
||||
|
||||
```py
|
||||
from typing import NamedTuple
|
||||
|
||||
name = "Person"
|
||||
Person = NamedTuple(name, [("id", int), ("name", str)])
|
||||
|
||||
p = Person(1, "Alice")
|
||||
reveal_type(p.id) # revealed: int
|
||||
reveal_type(p.name) # revealed: str
|
||||
```
|
||||
|
||||
### Functional syntax with tuple variable fields
|
||||
|
||||
When fields are passed via a tuple variable, we can extract the literal field names and types from
|
||||
the inferred tuple type:
|
||||
|
||||
```py
|
||||
from typing import NamedTuple
|
||||
from ty_extensions import static_assert, is_subtype_of, reveal_mro
|
||||
|
||||
fields = (("host", str), ("port", int))
|
||||
Url = NamedTuple("Url", fields)
|
||||
|
||||
url = Url("localhost", 8080)
|
||||
reveal_type(url.host) # revealed: str
|
||||
reveal_type(url.port) # revealed: int
|
||||
|
||||
# Generic types are also correctly converted to instance types.
|
||||
generic_fields = (("items", list[int]), ("mapping", dict[str, bool]))
|
||||
Container = NamedTuple("Container", generic_fields)
|
||||
container = Container([1, 2, 3], {"a": True})
|
||||
reveal_type(container.items) # revealed: list[int]
|
||||
reveal_type(container.mapping) # revealed: dict[str, bool]
|
||||
|
||||
# MRO includes the properly specialized tuple type.
|
||||
# revealed: (<class 'Url'>, <class 'tuple[str, int]'>, <class 'object'>)
|
||||
reveal_mro(Url)
|
||||
|
||||
static_assert(is_subtype_of(Url, tuple[str, int]))
|
||||
|
||||
# Invalid type expressions in fields produce a diagnostic.
|
||||
invalid_fields = (("x", 42),) # 42 is not a valid type
|
||||
# error: [invalid-type-form] "Invalid type `Literal[42]` in `NamedTuple` field type"
|
||||
InvalidNT = NamedTuple("InvalidNT", invalid_fields)
|
||||
reveal_type(InvalidNT) # revealed: <class 'InvalidNT'>
|
||||
|
||||
# Unpacking works correctly with the field types.
|
||||
host, port = url
|
||||
reveal_type(host) # revealed: str
|
||||
reveal_type(port) # revealed: int
|
||||
|
||||
# error: [invalid-assignment] "Too many values to unpack: Expected 1"
|
||||
(only_one,) = url
|
||||
|
||||
# error: [invalid-assignment] "Not enough values to unpack: Expected 3"
|
||||
a, b, c = url
|
||||
|
||||
# Indexing works correctly.
|
||||
reveal_type(url[0]) # revealed: str
|
||||
reveal_type(url[1]) # revealed: int
|
||||
|
||||
# error: [index-out-of-bounds]
|
||||
url[2]
|
||||
```
|
||||
|
||||
### Functional syntax with variadic tuple fields
|
||||
|
||||
When fields are passed as a variadic tuple (e.g., `tuple[..., *tuple[T, ...]]`), we cannot determine
|
||||
the exact field count statically. In this case, we fall back to unknown fields:
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python-version = "3.11"
|
||||
```
|
||||
|
||||
```py
|
||||
from typing import NamedTuple
|
||||
from ty_extensions import reveal_mro
|
||||
|
||||
# Variadic tuple - we can't determine the exact fields statically.
|
||||
def get_fields() -> tuple[tuple[str, type[int]], *tuple[tuple[str, type[str]], ...]]:
|
||||
return (("x", int), ("y", str))
|
||||
|
||||
fields = get_fields()
|
||||
NT = NamedTuple("NT", fields)
|
||||
|
||||
# Fields are unknown, so attribute access returns Any and MRO has Unknown tuple.
|
||||
reveal_type(NT) # revealed: <class 'NT'>
|
||||
reveal_mro(NT) # revealed: (<class 'NT'>, <class 'tuple[Unknown, ...]'>, <class 'object'>)
|
||||
reveal_type(NT(1, "a").x) # revealed: Any
|
||||
```
|
||||
|
||||
Similarly for `collections.namedtuple`:
|
||||
|
||||
```py
|
||||
import collections
|
||||
from ty_extensions import reveal_mro
|
||||
|
||||
def get_field_names() -> tuple[str, *tuple[str, ...]]:
|
||||
return ("x", "y")
|
||||
|
||||
field_names = get_field_names()
|
||||
NT = collections.namedtuple("NT", field_names)
|
||||
|
||||
# Fields are unknown, so attribute access returns Any and MRO has Unknown tuple.
|
||||
reveal_type(NT) # revealed: <class 'NT'>
|
||||
reveal_mro(NT) # revealed: (<class 'NT'>, <class 'tuple[Unknown, ...]'>, <class 'object'>)
|
||||
reveal_type(NT(1, 2).x) # revealed: Any
|
||||
```
|
||||
|
||||
### Class inheriting from functional NamedTuple
|
||||
|
||||
Classes can inherit from functional namedtuples. The constructor parameters and field types are
|
||||
properly inherited:
|
||||
|
||||
```py
|
||||
from typing import NamedTuple
|
||||
from ty_extensions import reveal_mro
|
||||
|
||||
class Url(NamedTuple("Url", [("host", str), ("path", str)])):
|
||||
pass
|
||||
|
||||
reveal_type(Url) # revealed: <class 'Url'>
|
||||
# revealed: (<class 'mdtest_snippet.Url @ src/mdtest_snippet.py:4:7'>, <class 'mdtest_snippet.Url @ src/mdtest_snippet.py:4:11'>, <class 'tuple[str, str]'>, <class 'object'>)
|
||||
reveal_mro(Url)
|
||||
reveal_type(Url.__new__) # revealed: (cls: type, host: str, path: str) -> Url
|
||||
|
||||
# Constructor works with the inherited fields.
|
||||
url = Url("example.com", "/path")
|
||||
reveal_type(url) # revealed: Url
|
||||
reveal_type(url.host) # revealed: str
|
||||
reveal_type(url.path) # revealed: str
|
||||
|
||||
# Error handling works correctly.
|
||||
# error: [missing-argument]
|
||||
Url("example.com")
|
||||
|
||||
# error: [too-many-positional-arguments]
|
||||
Url("example.com", "/path", "extra")
|
||||
```
|
||||
|
||||
Subclasses can add methods that use inherited fields:
|
||||
|
||||
```py
|
||||
from typing import NamedTuple
|
||||
from typing_extensions import Self
|
||||
|
||||
class Url(NamedTuple("Url", [("host", str), ("port", int)])):
|
||||
def with_port(self, port: int) -> Self:
|
||||
reveal_type(self.host) # revealed: str
|
||||
reveal_type(self.port) # revealed: int
|
||||
return self._replace(port=port)
|
||||
|
||||
url = Url("localhost", 8080)
|
||||
reveal_type(url.with_port(9000)) # revealed: Url
|
||||
```
|
||||
|
||||
For `class Foo(namedtuple("Foo", ...)): ...`, the inner call creates a namedtuple class, but the
|
||||
outer class is just a regular class inheriting from it. This is equivalent to:
|
||||
|
||||
```py
|
||||
class _Foo(NamedTuple): ...
|
||||
|
||||
class Foo(_Foo): # Regular class, not a namedtuple
|
||||
...
|
||||
```
|
||||
|
||||
Because the outer class is not itself a namedtuple, it can use `super()` and override `__new__`:
|
||||
|
||||
```py
|
||||
from collections import namedtuple
|
||||
from typing import NamedTuple
|
||||
|
||||
class ExtType(namedtuple("ExtType", "code data")):
|
||||
"""Override __new__ to add validation."""
|
||||
|
||||
def __new__(cls, code, data):
|
||||
if not isinstance(code, int):
|
||||
raise TypeError("code must be int")
|
||||
return super().__new__(cls, code, data)
|
||||
|
||||
class Url(NamedTuple("Url", [("host", str), ("path", str)])):
|
||||
"""Override __new__ to normalize the path."""
|
||||
|
||||
def __new__(cls, host, path):
|
||||
if path and not path.startswith("/"):
|
||||
path = "/" + path
|
||||
return super().__new__(cls, host, path)
|
||||
|
||||
# Both work correctly.
|
||||
ext = ExtType(42, b"hello")
|
||||
reveal_type(ext) # revealed: ExtType
|
||||
|
||||
url = Url("example.com", "path")
|
||||
reveal_type(url) # revealed: Url
|
||||
```
|
||||
|
||||
### Functional syntax with list variable fields
|
||||
|
||||
When fields are passed via a list variable (not a literal), the field names cannot be determined
|
||||
statically. Attribute access returns `Any` and the constructor accepts any arguments:
|
||||
|
||||
```py
|
||||
from typing import NamedTuple
|
||||
from typing_extensions import Self
|
||||
|
||||
fields = [("host", str), ("port", int)]
|
||||
|
||||
class Url(NamedTuple("Url", fields)):
|
||||
def with_port(self, port: int) -> Self:
|
||||
# Fields are unknown, so attribute access returns Any.
|
||||
reveal_type(self.host) # revealed: Any
|
||||
reveal_type(self.port) # revealed: Any
|
||||
reveal_type(self.unknown) # revealed: Any
|
||||
return self._replace(port=port)
|
||||
```
|
||||
|
||||
When constructing a namedtuple directly with dynamically-defined fields, keyword arguments are
|
||||
accepted because the constructor uses a gradual signature:
|
||||
|
||||
```py
|
||||
import collections
|
||||
from ty_extensions import reveal_mro
|
||||
|
||||
CheckerConfig = ["duration", "video_fps", "audio_sample_rate"]
|
||||
GroundTruth = collections.namedtuple("GroundTruth", " ".join(CheckerConfig))
|
||||
|
||||
# No error - fields are unknown, so any keyword arguments are accepted
|
||||
config = GroundTruth(duration=0, video_fps=30, audio_sample_rate=44100)
|
||||
reveal_type(config) # revealed: GroundTruth
|
||||
reveal_type(config.duration) # revealed: Any
|
||||
|
||||
# Namedtuples with unknown fields inherit from tuple[Unknown, ...] to avoid false positives.
|
||||
# revealed: (<class 'GroundTruth'>, <class 'tuple[Unknown, ...]'>, <class 'object'>)
|
||||
reveal_mro(GroundTruth)
|
||||
|
||||
# No index-out-of-bounds error since the tuple length is unknown.
|
||||
reveal_type(config[0]) # revealed: Unknown
|
||||
reveal_type(config[100]) # revealed: Unknown
|
||||
```
|
||||
|
||||
### Functional syntax signature validation
|
||||
|
||||
The `collections.namedtuple` function accepts `str | Iterable[str]` for `field_names`:
|
||||
|
||||
```py
|
||||
import collections
|
||||
from ty_extensions import reveal_mro
|
||||
|
||||
# String field names (space-separated)
|
||||
Point1 = collections.namedtuple("Point", "x y")
|
||||
reveal_type(Point1) # revealed: <class 'Point'>
|
||||
reveal_mro(Point1) # revealed: (<class 'Point'>, <class 'tuple[Any, Any]'>, <class 'object'>)
|
||||
|
||||
# String field names with multiple spaces
|
||||
Point1a = collections.namedtuple("Point", "x y")
|
||||
reveal_type(Point1a) # revealed: <class 'Point'>
|
||||
reveal_mro(Point1a) # revealed: (<class 'Point'>, <class 'tuple[Any, Any]'>, <class 'object'>)
|
||||
|
||||
# String field names (comma-separated also works at runtime)
|
||||
Point2 = collections.namedtuple("Point", "x, y")
|
||||
reveal_type(Point2) # revealed: <class 'Point'>
|
||||
reveal_mro(Point2) # revealed: (<class 'Point'>, <class 'tuple[Any, Any]'>, <class 'object'>)
|
||||
|
||||
# List of strings
|
||||
Point3 = collections.namedtuple("Point", ["x", "y"])
|
||||
reveal_type(Point3) # revealed: <class 'Point'>
|
||||
reveal_mro(Point3) # revealed: (<class 'Point'>, <class 'tuple[Any, Any]'>, <class 'object'>)
|
||||
|
||||
# Tuple of strings
|
||||
Point4 = collections.namedtuple("Point", ("x", "y"))
|
||||
reveal_type(Point4) # revealed: <class 'Point'>
|
||||
reveal_mro(Point4) # revealed: (<class 'Point'>, <class 'tuple[Any, Any]'>, <class 'object'>)
|
||||
|
||||
# Invalid: integer is not a valid typename
|
||||
# error: [invalid-argument-type]
|
||||
Invalid = collections.namedtuple(123, ["x", "y"])
|
||||
reveal_type(Invalid) # revealed: <class '<unknown>'>
|
||||
reveal_mro(Invalid) # revealed: (<class '<unknown>'>, <class 'tuple[Any, Any]'>, <class 'object'>)
|
||||
|
||||
# Invalid: too many positional arguments
|
||||
# error: [too-many-positional-arguments] "Too many positional arguments to function `namedtuple`: expected 2, got 4"
|
||||
TooMany = collections.namedtuple("TooMany", "x", "y", "z")
|
||||
reveal_type(TooMany) # revealed: <class 'TooMany'>
|
||||
```
|
||||
|
||||
The `typing.NamedTuple` function accepts `Iterable[tuple[str, Any]]` for `fields`:
|
||||
|
||||
```py
|
||||
from typing import NamedTuple
|
||||
|
||||
# List of tuples
|
||||
Person1 = NamedTuple("Person", [("name", str), ("age", int)])
|
||||
reveal_type(Person1) # revealed: <class 'Person'>
|
||||
|
||||
# Tuple of tuples
|
||||
Person2 = NamedTuple("Person", (("name", str), ("age", int)))
|
||||
reveal_type(Person2) # revealed: <class 'Person'>
|
||||
|
||||
# Invalid: integer is not a valid typename
|
||||
# error: [invalid-argument-type]
|
||||
NamedTuple(123, [("name", str)])
|
||||
|
||||
# Invalid: too many positional arguments
|
||||
# error: [too-many-positional-arguments] "Too many positional arguments to function `NamedTuple`: expected 2, got 4"
|
||||
TooMany = NamedTuple("TooMany", [("x", int)], "extra", "args")
|
||||
reveal_type(TooMany) # revealed: <class 'TooMany'>
|
||||
```
|
||||
|
||||
### Keyword arguments for `collections.namedtuple`
|
||||
|
||||
The `collections.namedtuple` function accepts `rename`, `defaults`, and `module` keyword arguments:
|
||||
|
||||
```py
|
||||
import collections
|
||||
from ty_extensions import reveal_mro
|
||||
|
||||
# `rename=True` replaces invalid identifiers with positional names
|
||||
Point = collections.namedtuple("Point", ["x", "class", "_y", "z", "z"], rename=True)
|
||||
reveal_type(Point) # revealed: <class 'Point'>
|
||||
reveal_type(Point.__new__) # revealed: (cls: type, x: Any, _1: Any, _2: Any, z: Any, _4: Any) -> Point
|
||||
reveal_mro(Point) # revealed: (<class 'Point'>, <class 'tuple[Any, Any, Any, Any, Any]'>, <class 'object'>)
|
||||
p = Point(1, 2, 3, 4, 5)
|
||||
reveal_type(p.x) # revealed: Any
|
||||
reveal_type(p._1) # revealed: Any
|
||||
reveal_type(p._2) # revealed: Any
|
||||
reveal_type(p.z) # revealed: Any
|
||||
reveal_type(p._4) # revealed: Any
|
||||
|
||||
# Truthy non-bool values for `rename` are also handled, but emit a diagnostic
|
||||
# error: [invalid-argument-type] "Invalid argument to parameter `rename` of `namedtuple()`"
|
||||
Point2 = collections.namedtuple("Point2", ["_x", "class"], rename=1)
|
||||
reveal_type(Point2) # revealed: <class 'Point2'>
|
||||
reveal_type(Point2.__new__) # revealed: (cls: type, _0: Any, _1: Any) -> Point2
|
||||
|
||||
# `defaults` provides default values for the rightmost fields
|
||||
Person = collections.namedtuple("Person", ["name", "age", "city"], defaults=["Unknown"])
|
||||
reveal_type(Person) # revealed: <class 'Person'>
|
||||
reveal_type(Person.__new__) # revealed: (cls: type, name: Any, age: Any, city: Any = "Unknown") -> Person
|
||||
|
||||
reveal_mro(Person) # revealed: (<class 'Person'>, <class 'tuple[Any, Any, Any]'>, <class 'object'>)
|
||||
# Can create with all fields
|
||||
person1 = Person("Alice", 30, "NYC")
|
||||
# Can omit the field with default
|
||||
person2 = Person("Bob", 25)
|
||||
reveal_type(person1.city) # revealed: Any
|
||||
reveal_type(person2.city) # revealed: Any
|
||||
|
||||
# `module` is valid but doesn't affect type checking
|
||||
Config = collections.namedtuple("Config", ["host", "port"], module="myapp")
|
||||
reveal_type(Config) # revealed: <class 'Config'>
|
||||
|
||||
# When more defaults are provided than fields, we treat all fields as having defaults.
|
||||
# TODO: This should emit a diagnostic since it would fail at runtime.
|
||||
TooManyDefaults = collections.namedtuple("TooManyDefaults", ["x", "y"], defaults=("a", "b", "c"))
|
||||
reveal_type(TooManyDefaults) # revealed: <class 'TooManyDefaults'>
|
||||
reveal_type(TooManyDefaults.__new__) # revealed: (cls: type, x: Any = "a", y: Any = "b") -> TooManyDefaults
|
||||
|
||||
# Unknown keyword arguments produce an error
|
||||
# error: [unknown-argument]
|
||||
Bad1 = collections.namedtuple("Bad1", ["x", "y"], foobarbaz=42)
|
||||
reveal_type(Bad1) # revealed: <class 'Bad1'>
|
||||
reveal_mro(Bad1) # revealed: (<class 'Bad1'>, <class 'tuple[Any, Any]'>, <class 'object'>)
|
||||
|
||||
# Multiple unknown keyword arguments
|
||||
# error: [unknown-argument]
|
||||
# error: [unknown-argument]
|
||||
Bad2 = collections.namedtuple("Bad2", ["x"], invalid1=True, invalid2=False)
|
||||
reveal_type(Bad2) # revealed: <class 'Bad2'>
|
||||
reveal_mro(Bad2) # revealed: (<class 'Bad2'>, <class 'tuple[Any]'>, <class 'object'>)
|
||||
|
||||
# Invalid type for `defaults` (not Iterable[Any] | None)
|
||||
# error: [invalid-argument-type] "Invalid argument to parameter `defaults` of `namedtuple()`"
|
||||
Bad3 = collections.namedtuple("Bad3", ["x"], defaults=123)
|
||||
reveal_type(Bad3) # revealed: <class 'Bad3'>
|
||||
|
||||
# Invalid type for `module` (not str | None)
|
||||
# error: [invalid-argument-type] "Invalid argument to parameter `module` of `namedtuple()`"
|
||||
Bad4 = collections.namedtuple("Bad4", ["x"], module=456)
|
||||
reveal_type(Bad4) # revealed: <class 'Bad4'>
|
||||
|
||||
# Invalid type for `field_names` (not str | Iterable[str])
|
||||
# error: [invalid-argument-type] "Invalid argument to parameter `field_names` of `namedtuple()`"
|
||||
Bad5 = collections.namedtuple("Bad5", 12345)
|
||||
reveal_type(Bad5) # revealed: <class 'Bad5'>
|
||||
```
|
||||
|
||||
### Keyword arguments for `typing.NamedTuple`
|
||||
|
||||
The `typing.NamedTuple` function does not accept any keyword arguments:
|
||||
|
||||
```py
|
||||
from typing import NamedTuple
|
||||
|
||||
# error: [unknown-argument]
|
||||
Bad3 = NamedTuple("Bad3", [("x", int)], rename=True)
|
||||
|
||||
# error: [unknown-argument]
|
||||
Bad4 = NamedTuple("Bad4", [("x", int)], defaults=[0])
|
||||
|
||||
# error: [unknown-argument]
|
||||
Bad5 = NamedTuple("Bad5", [("x", int)], foobarbaz=42)
|
||||
|
||||
# Invalid type for `fields` (not an iterable)
|
||||
# error: [invalid-argument-type] "Invalid argument to parameter `fields` of `NamedTuple()`"
|
||||
Bad6 = NamedTuple("Bad6", 12345)
|
||||
reveal_type(Bad6) # revealed: <class 'Bad6'>
|
||||
```
|
||||
|
||||
### Starred and double-starred arguments
|
||||
|
||||
When starred (`*args`) or double-starred (`**kwargs`) arguments are used, we fall back to normal
|
||||
call binding since we can't statically determine the arguments. This results in `NamedTupleFallback`
|
||||
being returned:
|
||||
|
||||
```py
|
||||
import collections
|
||||
from typing import NamedTuple
|
||||
|
||||
args = ("Point", ["x", "y"])
|
||||
kwargs = {"rename": True}
|
||||
|
||||
# Starred positional arguments - falls back to NamedTupleFallback
|
||||
Point1 = collections.namedtuple(*args)
|
||||
reveal_type(Point1) # revealed: type[NamedTupleFallback]
|
||||
|
||||
# Ideally we'd catch this false negative,
|
||||
# but it's unclear if it's worth the added complexity
|
||||
Point2 = NamedTuple(*args)
|
||||
reveal_type(Point2) # revealed: type[NamedTupleFallback]
|
||||
|
||||
# Double-starred keyword arguments - falls back to NamedTupleFallback
|
||||
Point3 = collections.namedtuple("Point", ["x", "y"], **kwargs)
|
||||
reveal_type(Point3) # revealed: type[NamedTupleFallback]
|
||||
|
||||
Point4 = NamedTuple("Point", [("x", int), ("y", int)], **kwargs)
|
||||
reveal_type(Point4) # revealed: type[NamedTupleFallback]
|
||||
```
|
||||
|
||||
### Definition
|
||||
@@ -154,6 +627,84 @@ class D(
|
||||
class E(NamedTuple, Protocol): ...
|
||||
```
|
||||
|
||||
However, as explained above, for `class Foo(namedtuple("Foo", ...)): ...` the outer class is not
|
||||
itself a namedtuple—it just inherits from one. So it can use multiple inheritance freely:
|
||||
|
||||
```py
|
||||
from abc import ABC
|
||||
from collections import namedtuple
|
||||
from typing import NamedTuple
|
||||
|
||||
class Point(namedtuple("Point", ["x", "y"]), ABC):
|
||||
"""No error - functional namedtuple inheritance allows multiple inheritance."""
|
||||
|
||||
class Url(NamedTuple("Url", [("host", str), ("port", int)]), ABC):
|
||||
"""No error - typing.NamedTuple functional syntax also allows multiple inheritance."""
|
||||
|
||||
p = Point(1, 2)
|
||||
reveal_type(p.x) # revealed: Any
|
||||
reveal_type(p.y) # revealed: Any
|
||||
|
||||
u = Url("localhost", 8080)
|
||||
reveal_type(u.host) # revealed: str
|
||||
reveal_type(u.port) # revealed: int
|
||||
```
|
||||
|
||||
### Inherited tuple methods
|
||||
|
||||
Namedtuples inherit methods from their tuple base class, including `count`, `index`, and comparison
|
||||
methods (`__lt__`, `__le__`, `__gt__`, `__ge__`).
|
||||
|
||||
```py
|
||||
from collections import namedtuple
|
||||
from typing import NamedTuple
|
||||
|
||||
# typing.NamedTuple inherits tuple methods
|
||||
class Point(NamedTuple):
|
||||
x: int
|
||||
y: int
|
||||
|
||||
p = Point(1, 2)
|
||||
reveal_type(p.count(1)) # revealed: int
|
||||
reveal_type(p.index(2)) # revealed: int
|
||||
|
||||
# collections.namedtuple also inherits tuple methods
|
||||
Person = namedtuple("Person", ["name", "age"])
|
||||
alice = Person("Alice", 30)
|
||||
reveal_type(alice.count("Alice")) # revealed: int
|
||||
reveal_type(alice.index(30)) # revealed: int
|
||||
```
|
||||
|
||||
The `@total_ordering` decorator should not emit a diagnostic, since the required `__lt__` method is
|
||||
already present:
|
||||
|
||||
```py
|
||||
from collections import namedtuple
|
||||
from functools import total_ordering
|
||||
from typing import NamedTuple
|
||||
|
||||
# No error - __lt__ is inherited from the tuple base class
|
||||
@total_ordering
|
||||
class Point(namedtuple("Point", "x y")): ...
|
||||
|
||||
p1 = Point(1, 2)
|
||||
p2 = Point(3, 4)
|
||||
# TODO: should be `bool`, not `Any | Literal[False]`
|
||||
reveal_type(p1 < p2) # revealed: Any | Literal[False]
|
||||
reveal_type(p1 <= p2) # revealed: Any | Literal[True]
|
||||
|
||||
# Same for typing.NamedTuple - no error
|
||||
@total_ordering
|
||||
class Person(NamedTuple):
|
||||
name: str
|
||||
age: int
|
||||
|
||||
alice = Person("Alice", 30)
|
||||
bob = Person("Bob", 25)
|
||||
reveal_type(alice < bob) # revealed: bool
|
||||
reveal_type(alice >= bob) # revealed: bool
|
||||
```
|
||||
|
||||
### Inheriting from a `NamedTuple`
|
||||
|
||||
Inheriting from a `NamedTuple` is supported, but new fields on the subclass will not be part of the
|
||||
@@ -254,6 +805,34 @@ reveal_type(LegacyProperty[str].value.fget) # revealed: (self, /) -> str
|
||||
reveal_type(LegacyProperty("height", 3.4).value) # revealed: int | float
|
||||
```
|
||||
|
||||
Generic namedtuples can also be defined using the functional syntax with type variables in the field
|
||||
types. We don't currently support this, but mypy does:
|
||||
|
||||
```py
|
||||
from typing import NamedTuple, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
# TODO: ideally this would create a generic namedtuple class
|
||||
Pair = NamedTuple("Pair", [("first", T), ("second", T)])
|
||||
|
||||
# For now, the TypeVar is not specialized, so the field types remain as `T@Pair` and argument type
|
||||
# errors are emitted when calling the constructor.
|
||||
reveal_type(Pair) # revealed: <class 'Pair'>
|
||||
|
||||
# error: [invalid-argument-type]
|
||||
# error: [invalid-argument-type]
|
||||
reveal_type(Pair(1, 2)) # revealed: Pair
|
||||
|
||||
# error: [invalid-argument-type]
|
||||
# error: [invalid-argument-type]
|
||||
reveal_type(Pair(1, 2).first) # revealed: T@Pair
|
||||
|
||||
# error: [invalid-argument-type]
|
||||
# error: [invalid-argument-type]
|
||||
reveal_type(Pair(1, 2).second) # revealed: T@Pair
|
||||
```
|
||||
|
||||
## Attributes on `NamedTuple`
|
||||
|
||||
The following attributes are available on `NamedTuple` classes / instances:
|
||||
@@ -267,6 +846,7 @@ class Person(NamedTuple):
|
||||
|
||||
reveal_type(Person._field_defaults) # revealed: dict[str, Any]
|
||||
reveal_type(Person._fields) # revealed: tuple[Literal["name"], Literal["age"]]
|
||||
reveal_type(Person.__slots__) # revealed: tuple[()]
|
||||
reveal_type(Person._make) # revealed: bound method <class 'Person'>._make(iterable: Iterable[Any]) -> Person
|
||||
reveal_type(Person._asdict) # revealed: def _asdict(self) -> dict[str, Any]
|
||||
reveal_type(Person._replace) # revealed: (self: Self, *, name: str = ..., age: int | None = ...) -> Self
|
||||
@@ -309,6 +889,75 @@ Person = namedtuple("Person", ["id", "name", "age"], defaults=[None])
|
||||
|
||||
alice = Person(1, "Alice", 42)
|
||||
bob = Person(2, "Bob")
|
||||
|
||||
reveal_type(Person.__slots__) # revealed: tuple[()]
|
||||
```
|
||||
|
||||
## `collections.namedtuple` with tuple variable field names
|
||||
|
||||
When field names are passed via a tuple variable, we can extract the literal field names from the
|
||||
inferred tuple type. The class is properly synthesized (not a fallback), but field types are `Any`
|
||||
since `collections.namedtuple` doesn't include type annotations:
|
||||
|
||||
```py
|
||||
from collections import namedtuple
|
||||
|
||||
field_names = ("name", "age")
|
||||
Person = namedtuple("Person", field_names)
|
||||
|
||||
reveal_type(Person) # revealed: <class 'Person'>
|
||||
|
||||
alice = Person("Alice", 42)
|
||||
reveal_type(alice) # revealed: Person
|
||||
reveal_type(alice.name) # revealed: Any
|
||||
reveal_type(alice.age) # revealed: Any
|
||||
```
|
||||
|
||||
## `collections.namedtuple` with list variable field names
|
||||
|
||||
When field names are passed via a list variable (not a literal), we fall back to
|
||||
`NamedTupleFallback` which allows any attribute access. This is a regression test for accessing
|
||||
`Self` attributes in methods of classes that inherit from namedtuples with dynamic fields:
|
||||
|
||||
```py
|
||||
from collections import namedtuple
|
||||
from typing_extensions import Self
|
||||
|
||||
field_names = ["host", "port"]
|
||||
|
||||
class Url(namedtuple("Url", field_names)):
|
||||
def with_port(self, port: int) -> Self:
|
||||
# Fields are unknown, so attribute access returns `Any`.
|
||||
reveal_type(self.host) # revealed: Any
|
||||
reveal_type(self.port) # revealed: Any
|
||||
reveal_type(self.unknown) # revealed: Any
|
||||
return self._replace(port=port)
|
||||
```
|
||||
|
||||
## `collections.namedtuple` attributes
|
||||
|
||||
Functional namedtuples have synthesized attributes similar to class-based namedtuples:
|
||||
|
||||
```py
|
||||
from collections import namedtuple
|
||||
|
||||
Person = namedtuple("Person", ["name", "age"])
|
||||
|
||||
reveal_type(Person._fields) # revealed: tuple[Literal["name"], Literal["age"]]
|
||||
reveal_type(Person._field_defaults) # revealed: dict[str, Any]
|
||||
reveal_type(Person._make) # revealed: bound method <class 'Person'>._make(iterable: Iterable[Any]) -> Person
|
||||
reveal_type(Person._asdict) # revealed: def _asdict(self) -> dict[str, Any]
|
||||
reveal_type(Person._replace) # revealed: (self: Self, *, name: Any = ..., age: Any = ...) -> Self
|
||||
|
||||
# _make creates instances from an iterable.
|
||||
reveal_type(Person._make(["Alice", 30])) # revealed: Person
|
||||
|
||||
# _asdict converts to a dictionary.
|
||||
person = Person("Alice", 30)
|
||||
reveal_type(person._asdict()) # revealed: dict[str, Any]
|
||||
|
||||
# _replace creates a copy with replaced fields.
|
||||
reveal_type(person._replace(name="Bob")) # revealed: Person
|
||||
```
|
||||
|
||||
## The symbol `NamedTuple` itself
|
||||
|
||||
@@ -210,12 +210,15 @@ Narrowing does not occur in the same way if `type` is used to dynamically create
|
||||
|
||||
```py
|
||||
def _(x: str | int):
|
||||
# Inline type() calls fall back to regular type overload matching.
|
||||
# TODO: Once inline type() calls synthesize class types, this should narrow x to Never.
|
||||
# The following diagnostic is valid, since the three-argument form of `type`
|
||||
# can only be called with `str` as the first argument.
|
||||
#
|
||||
# error: 13 [invalid-argument-type] "Argument to class `type` is incorrect: Expected `str`, found `str | int`"
|
||||
# error: [invalid-argument-type] "Invalid argument to parameter 1 (`name`) of `type()`: Expected `str`, found `str | int`"
|
||||
if type(x, (), {}) is str:
|
||||
reveal_type(x) # revealed: str | int
|
||||
# But we synthesize a new class object as the result of a three-argument call to `type`,
|
||||
# and we know that this synthesized class object is not the same object as the `str` class object,
|
||||
# so here the type is narrowed to `Never`!
|
||||
reveal_type(x) # revealed: Never
|
||||
else:
|
||||
reveal_type(x) # revealed: str | int
|
||||
```
|
||||
|
||||
@@ -30,6 +30,23 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/named_tuple.md
|
||||
15 |
|
||||
16 | # error: [invalid-named-tuple]
|
||||
17 | class E(NamedTuple, Protocol): ...
|
||||
18 | from abc import ABC
|
||||
19 | from collections import namedtuple
|
||||
20 | from typing import NamedTuple
|
||||
21 |
|
||||
22 | class Point(namedtuple("Point", ["x", "y"]), ABC):
|
||||
23 | """No error - functional namedtuple inheritance allows multiple inheritance."""
|
||||
24 |
|
||||
25 | class Url(NamedTuple("Url", [("host", str), ("port", int)]), ABC):
|
||||
26 | """No error - typing.NamedTuple functional syntax also allows multiple inheritance."""
|
||||
27 |
|
||||
28 | p = Point(1, 2)
|
||||
29 | reveal_type(p.x) # revealed: Any
|
||||
30 | reveal_type(p.y) # revealed: Any
|
||||
31 |
|
||||
32 | u = Url("localhost", 8080)
|
||||
33 | reveal_type(u.host) # revealed: str
|
||||
34 | reveal_type(u.port) # revealed: int
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
@@ -68,6 +85,8 @@ error[invalid-named-tuple]: NamedTuple class `E` cannot use multiple inheritance
|
||||
16 | # error: [invalid-named-tuple]
|
||||
17 | class E(NamedTuple, Protocol): ...
|
||||
| ^^^^^^^^
|
||||
18 | from abc import ABC
|
||||
19 | from collections import namedtuple
|
||||
|
|
||||
info: rule `invalid-named-tuple` is enabled by default
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ error[no-matching-overload]: No overload of class `type` matches arguments
|
||||
1 | type() # error: [no-matching-overload]
|
||||
| ^^^^^^
|
||||
|
|
||||
help: `builtins.type()` can either be called with one or three positional arguments (got 0)
|
||||
info: rule `no-matching-overload` is enabled by default
|
||||
|
||||
```
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
|
||||
---
|
||||
mdtest name: type.md - Calls to `type()` - MRO error highlighting (snapshot)
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/call/type.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## mdtest_snippet.py
|
||||
|
||||
```
|
||||
1 | class A: ...
|
||||
2 |
|
||||
3 | Dup = type("Dup", (A, A), {}) # error: [duplicate-base]
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[duplicate-base]: Duplicate base class <class 'A'> in class `Dup`
|
||||
--> src/mdtest_snippet.py:3:7
|
||||
|
|
||||
1 | class A: ...
|
||||
2 |
|
||||
3 | Dup = type("Dup", (A, A), {}) # error: [duplicate-base]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
info: rule `duplicate-base` is enabled by default
|
||||
|
||||
```
|
||||
@@ -0,0 +1,39 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
|
||||
---
|
||||
mdtest name: unsupported_base_dynamic_type.md - Unsupported base for dynamic `type()` classes - Enum base
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unsupported_base_dynamic_type.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## mdtest_snippet.py
|
||||
|
||||
```
|
||||
1 | from enum import Enum
|
||||
2 |
|
||||
3 | class MyEnum(Enum):
|
||||
4 | pass
|
||||
5 |
|
||||
6 | X = type("X", (MyEnum,), {}) # error: [invalid-base]
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[invalid-base]: Invalid base for class created via `type()`
|
||||
--> src/mdtest_snippet.py:6:16
|
||||
|
|
||||
4 | pass
|
||||
5 |
|
||||
6 | X = type("X", (MyEnum,), {}) # error: [invalid-base]
|
||||
| ^^^^^^ Has type `<class 'MyEnum'>`
|
||||
|
|
||||
info: Creating an enum class via `type()` is not supported
|
||||
info: Consider using `Enum("X", [])` instead
|
||||
info: rule `invalid-base` is enabled by default
|
||||
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
|
||||
---
|
||||
mdtest name: unsupported_base_dynamic_type.md - Unsupported base for dynamic `type()` classes - Enum with members
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unsupported_base_dynamic_type.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## mdtest_snippet.py
|
||||
|
||||
```
|
||||
1 | from enum import Enum
|
||||
2 |
|
||||
3 | class Color(Enum):
|
||||
4 | RED = 1
|
||||
5 | GREEN = 2
|
||||
6 |
|
||||
7 | X = type("X", (Color,), {}) # error: [subclass-of-final-class]
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[subclass-of-final-class]: Class `X` cannot inherit from final class `Color`
|
||||
--> src/mdtest_snippet.py:7:16
|
||||
|
|
||||
5 | GREEN = 2
|
||||
6 |
|
||||
7 | X = type("X", (Color,), {}) # error: [subclass-of-final-class]
|
||||
| ^^^^^
|
||||
|
|
||||
info: rule `subclass-of-final-class` is enabled by default
|
||||
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
|
||||
---
|
||||
mdtest name: unsupported_base_dynamic_type.md - Unsupported base for dynamic `type()` classes - `@final` class
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unsupported_base_dynamic_type.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## mdtest_snippet.py
|
||||
|
||||
```
|
||||
1 | from typing import final
|
||||
2 |
|
||||
3 | @final
|
||||
4 | class FinalClass:
|
||||
5 | pass
|
||||
6 |
|
||||
7 | X = type("X", (FinalClass,), {}) # error: [subclass-of-final-class]
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[subclass-of-final-class]: Class `X` cannot inherit from final class `FinalClass`
|
||||
--> src/mdtest_snippet.py:7:16
|
||||
|
|
||||
5 | pass
|
||||
6 |
|
||||
7 | X = type("X", (FinalClass,), {}) # error: [subclass-of-final-class]
|
||||
| ^^^^^^^^^^
|
||||
|
|
||||
info: rule `subclass-of-final-class` is enabled by default
|
||||
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
|
||||
---
|
||||
mdtest name: unsupported_base_dynamic_type.md - Unsupported base for dynamic `type()` classes - `Generic` base
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unsupported_base_dynamic_type.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## mdtest_snippet.py
|
||||
|
||||
```
|
||||
1 | from typing import Generic, TypeVar
|
||||
2 |
|
||||
3 | T = TypeVar("T")
|
||||
4 |
|
||||
5 | X = type("X", (Generic[T],), {}) # error: [invalid-base]
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[invalid-base]: Invalid base for class created via `type()`
|
||||
--> src/mdtest_snippet.py:5:16
|
||||
|
|
||||
3 | T = TypeVar("T")
|
||||
4 |
|
||||
5 | X = type("X", (Generic[T],), {}) # error: [invalid-base]
|
||||
| ^^^^^^^^^^ Has type `<special-form 'typing.Generic[T]'>`
|
||||
|
|
||||
info: Classes created via `type()` cannot be generic
|
||||
info: Consider using `class X(Generic[...]): ...` instead
|
||||
info: rule `invalid-base` is enabled by default
|
||||
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
|
||||
---
|
||||
mdtest name: unsupported_base_dynamic_type.md - Unsupported base for dynamic `type()` classes - `Protocol` base
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unsupported_base_dynamic_type.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## mdtest_snippet.py
|
||||
|
||||
```
|
||||
1 | from typing import Protocol
|
||||
2 |
|
||||
3 | X = type("X", (Protocol,), {}) # error: [unsupported-dynamic-base]
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
info[unsupported-dynamic-base]: Unsupported base for class created via `type()`
|
||||
--> src/mdtest_snippet.py:3:16
|
||||
|
|
||||
1 | from typing import Protocol
|
||||
2 |
|
||||
3 | X = type("X", (Protocol,), {}) # error: [unsupported-dynamic-base]
|
||||
| ^^^^^^^^ Has type `<special-form 'typing.Protocol'>`
|
||||
|
|
||||
info: Classes created via `type()` cannot be protocols
|
||||
info: Consider using `class X(Protocol): ...` instead
|
||||
info: rule `unsupported-dynamic-base` is enabled by default
|
||||
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
|
||||
---
|
||||
mdtest name: unsupported_base_dynamic_type.md - Unsupported base for dynamic `type()` classes - `TypedDict` base
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unsupported_base_dynamic_type.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## mdtest_snippet.py
|
||||
|
||||
```
|
||||
1 | from typing_extensions import TypedDict
|
||||
2 |
|
||||
3 | X = type("X", (TypedDict,), {}) # error: [invalid-base]
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[invalid-base]: Invalid base for class created via `type()`
|
||||
--> src/mdtest_snippet.py:3:16
|
||||
|
|
||||
1 | from typing_extensions import TypedDict
|
||||
2 |
|
||||
3 | X = type("X", (TypedDict,), {}) # error: [invalid-base]
|
||||
| ^^^^^^^^^ Has type `<special-form 'typing.TypedDict'>`
|
||||
|
|
||||
info: Classes created via `type()` cannot be TypedDicts
|
||||
info: Consider using `TypedDict("X", {})` instead
|
||||
info: rule `invalid-base` is enabled by default
|
||||
|
||||
```
|
||||
@@ -938,6 +938,18 @@ impl DefinitionKind<'_> {
|
||||
| DefinitionKind::ExceptHandler(_) => DefinitionCategory::Binding,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the value expression for assignment-based definitions.
|
||||
///
|
||||
/// Returns `Some` for `Assignment` and `AnnotatedAssignment` (if it has a value),
|
||||
/// `None` for all other definition kinds.
|
||||
pub(crate) fn value<'ast>(&self, module: &'ast ParsedModuleRef) -> Option<&'ast ast::Expr> {
|
||||
match self {
|
||||
DefinitionKind::Assignment(assignment) => Some(assignment.value(module)),
|
||||
DefinitionKind::AnnotatedAssignment(assignment) => assignment.value(module),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Hash, get_size2::GetSize)]
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::ops::Range;
|
||||
|
||||
use ruff_db::{files::File, parsed::ParsedModuleRef};
|
||||
use ruff_index::newtype_index;
|
||||
use ruff_python_ast as ast;
|
||||
use ruff_python_ast::{self as ast, NodeIndex};
|
||||
|
||||
use crate::{
|
||||
Db,
|
||||
@@ -463,6 +463,27 @@ impl NodeWithScopeKind {
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the anchor node index for this scope, or `None` for the module scope.
|
||||
///
|
||||
/// This is used to compute relative node indices for expressions within the scope,
|
||||
/// providing a stable anchor that only changes when the scope-introducing node changes.
|
||||
pub(crate) fn node_index(&self) -> Option<NodeIndex> {
|
||||
match self {
|
||||
Self::Module => None,
|
||||
Self::Class(class) => Some(class.index()),
|
||||
Self::ClassTypeParameters(class) => Some(class.index()),
|
||||
Self::Function(function) => Some(function.index()),
|
||||
Self::FunctionTypeParameters(function) => Some(function.index()),
|
||||
Self::TypeAlias(type_alias) => Some(type_alias.index()),
|
||||
Self::TypeAliasTypeParameters(type_alias) => Some(type_alias.index()),
|
||||
Self::Lambda(lambda) => Some(lambda.index()),
|
||||
Self::ListComprehension(comp) => Some(comp.index()),
|
||||
Self::SetComprehension(comp) => Some(comp.index()),
|
||||
Self::DictComprehension(comp) => Some(comp.index()),
|
||||
Self::GeneratorExpression(generator) => Some(generator.index()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, get_size2::GetSize)]
|
||||
|
||||
@@ -4111,55 +4111,6 @@ impl<'db> Type<'db> {
|
||||
.into()
|
||||
}
|
||||
|
||||
Some(KnownClass::Type) => {
|
||||
let str_instance = KnownClass::Str.to_instance(db);
|
||||
let type_instance = KnownClass::Type.to_instance(db);
|
||||
|
||||
// ```py
|
||||
// class type:
|
||||
// @overload
|
||||
// def __init__(self, o: object, /) -> None: ...
|
||||
// @overload
|
||||
// def __init__(self, name: str, bases: tuple[type, ...], dict: dict[str, Any], /, **kwds: Any) -> None: ...
|
||||
// ```
|
||||
CallableBinding::from_overloads(
|
||||
self,
|
||||
[
|
||||
Signature::new(
|
||||
Parameters::new(
|
||||
db,
|
||||
[Parameter::positional_only(Some(Name::new_static("o")))
|
||||
.with_annotated_type(Type::any())],
|
||||
),
|
||||
type_instance,
|
||||
),
|
||||
Signature::new(
|
||||
Parameters::new(
|
||||
db,
|
||||
[
|
||||
Parameter::positional_only(Some(Name::new_static("name")))
|
||||
.with_annotated_type(str_instance),
|
||||
Parameter::positional_only(Some(Name::new_static("bases")))
|
||||
.with_annotated_type(Type::homogeneous_tuple(
|
||||
db,
|
||||
type_instance,
|
||||
)),
|
||||
Parameter::positional_only(Some(Name::new_static("dict")))
|
||||
.with_annotated_type(
|
||||
KnownClass::Dict.to_specialized_instance(
|
||||
db,
|
||||
&[str_instance, Type::any()],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
type_instance,
|
||||
),
|
||||
],
|
||||
)
|
||||
.into()
|
||||
}
|
||||
|
||||
Some(KnownClass::Object) => {
|
||||
// ```py
|
||||
// class object:
|
||||
@@ -4451,10 +4402,6 @@ impl<'db> Type<'db> {
|
||||
.into()
|
||||
}
|
||||
|
||||
Type::SpecialForm(SpecialFormType::NamedTuple) => {
|
||||
Binding::single(self, Signature::todo("functional `NamedTuple` syntax")).into()
|
||||
}
|
||||
|
||||
Type::GenericAlias(_) => {
|
||||
// TODO annotated return type on `__new__` or metaclass `__call__`
|
||||
// TODO check call vs signatures of `__new__` and/or `__init__`
|
||||
@@ -6559,9 +6506,9 @@ impl<'db> Type<'db> {
|
||||
Some(TypeDefinition::Function(function.definition(db)))
|
||||
}
|
||||
Self::ModuleLiteral(module) => Some(TypeDefinition::Module(module.module(db))),
|
||||
Self::ClassLiteral(class_literal) => Some(class_literal.type_definition(db)),
|
||||
Self::ClassLiteral(class_literal) => class_literal.type_definition(db),
|
||||
Self::GenericAlias(alias) => Some(TypeDefinition::StaticClass(alias.definition(db))),
|
||||
Self::NominalInstance(instance) => Some(instance.class(db).type_definition(db)),
|
||||
Self::NominalInstance(instance) => instance.class(db).type_definition(db),
|
||||
Self::KnownInstance(instance) => match instance {
|
||||
KnownInstanceType::TypeVar(var) => {
|
||||
Some(TypeDefinition::TypeVar(var.definition(db)?))
|
||||
@@ -6575,7 +6522,7 @@ impl<'db> Type<'db> {
|
||||
|
||||
Self::SubclassOf(subclass_of_type) => match subclass_of_type.subclass_of() {
|
||||
SubclassOfInner::Dynamic(_) => None,
|
||||
SubclassOfInner::Class(class) => Some(class.type_definition(db)),
|
||||
SubclassOfInner::Class(class) => class.type_definition(db),
|
||||
SubclassOfInner::TypeVar(bound_typevar) => {
|
||||
Some(TypeDefinition::TypeVar(bound_typevar.typevar(db).definition(db)?))
|
||||
}
|
||||
@@ -6605,7 +6552,7 @@ impl<'db> Type<'db> {
|
||||
Self::TypeVar(bound_typevar) => Some(TypeDefinition::TypeVar(bound_typevar.typevar(db).definition(db)?)),
|
||||
|
||||
Self::ProtocolInstance(protocol) => match protocol.inner {
|
||||
Protocol::FromClass(class) => Some(class.type_definition(db)),
|
||||
Protocol::FromClass(class) => class.type_definition(db),
|
||||
Protocol::Synthesized(_) => None,
|
||||
},
|
||||
|
||||
|
||||
@@ -1187,11 +1187,6 @@ impl<'db> Bindings<'db> {
|
||||
}
|
||||
}
|
||||
|
||||
Some(KnownFunction::NamedTuple) => {
|
||||
overload
|
||||
.set_return_type(todo_type!("Support for functional `namedtuple`"));
|
||||
}
|
||||
|
||||
_ => {
|
||||
// Ideally, either the implementation, or exactly one of the overloads
|
||||
// of the function can have the dataclass_transform decorator applied.
|
||||
@@ -1443,12 +1438,6 @@ impl<'db> Bindings<'db> {
|
||||
}
|
||||
}
|
||||
|
||||
Some(KnownClass::Type) if overload_index == 0 => {
|
||||
if let [Some(arg)] = overload.parameter_types() {
|
||||
overload.set_return_type(arg.dunder_class(db));
|
||||
}
|
||||
}
|
||||
|
||||
Some(KnownClass::Property) => {
|
||||
if let [getter, setter, ..] = overload.parameter_types() {
|
||||
overload.set_return_type(Type::PropertyInstance(
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -104,6 +104,7 @@ pub(crate) fn register_lints(registry: &mut LintRegistryBuilder) {
|
||||
registry.register_lint(&POSSIBLY_UNRESOLVED_REFERENCE);
|
||||
registry.register_lint(&SUBCLASS_OF_FINAL_CLASS);
|
||||
registry.register_lint(&OVERRIDE_OF_FINAL_METHOD);
|
||||
registry.register_lint(&INEFFECTIVE_FINAL);
|
||||
registry.register_lint(&TYPE_ASSERTION_FAILURE);
|
||||
registry.register_lint(&TOO_MANY_POSITIONAL_ARGUMENTS);
|
||||
registry.register_lint(&UNAVAILABLE_IMPLICIT_SUPER_ARGUMENTS);
|
||||
@@ -1789,6 +1790,34 @@ declare_lint! {
|
||||
}
|
||||
}
|
||||
|
||||
declare_lint! {
|
||||
/// ## What it does
|
||||
/// Checks for calls to `final()` that type checkers cannot interpret.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// The `final()` function is designed to be used as a decorator. When called directly
|
||||
/// as a function (e.g., `final(type(...))`), type checkers will not understand the
|
||||
/// application of `final` and will not prevent subclassing.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```python
|
||||
/// from typing import final
|
||||
///
|
||||
/// # Incorrect: type checkers will not prevent subclassing
|
||||
/// MyClass = final(type("MyClass", (), {}))
|
||||
///
|
||||
/// # Correct: use `final` as a decorator
|
||||
/// @final
|
||||
/// class MyClass: ...
|
||||
/// ```
|
||||
pub(crate) static INEFFECTIVE_FINAL = {
|
||||
summary: "detects calls to `final()` that type checkers cannot interpret",
|
||||
status: LintStatus::preview("0.0.1-alpha.33"),
|
||||
default_level: Level::Warn,
|
||||
}
|
||||
}
|
||||
|
||||
declare_lint! {
|
||||
/// ## What it does
|
||||
/// Checks for methods that are decorated with `@override` but do not override any method in a superclass.
|
||||
|
||||
@@ -67,9 +67,9 @@ pub(crate) fn enum_metadata<'db>(
|
||||
//
|
||||
// MyEnum = type("MyEnum", (BaseEnum,), {"A": 1, "B": 2})
|
||||
// ```
|
||||
// TODO: Add a diagnostic for including an enum in a `type(...)` call.
|
||||
return None;
|
||||
}
|
||||
ClassLiteral::DynamicNamedTuple(..) => return None,
|
||||
};
|
||||
|
||||
// This is a fast path to avoid traversing the MRO of known classes
|
||||
|
||||
@@ -96,7 +96,7 @@ pub(crate) fn typing_self<'db>(
|
||||
let identity = TypeVarIdentity::new(
|
||||
db,
|
||||
ast::name::Name::new_static("Self"),
|
||||
Some(class.definition(db)),
|
||||
class.definition(db),
|
||||
TypeVarKind::TypingSelf,
|
||||
);
|
||||
let bounds = TypeVarBoundOrConstraints::UpperBound(Type::instance(
|
||||
|
||||
@@ -169,9 +169,9 @@ pub fn definitions_for_name<'db>(
|
||||
// instead of `int` (hover only shows the docstring of the first definition).
|
||||
.rev()
|
||||
.filter_map(|ty| ty.as_nominal_instance())
|
||||
.map(|instance| {
|
||||
let definition = instance.class_literal(db).definition(db);
|
||||
ResolvedDefinition::Definition(definition)
|
||||
.filter_map(|instance| {
|
||||
let definition = instance.class_literal(db).definition(db)?;
|
||||
Some(ResolvedDefinition::Definition(definition))
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -42,6 +42,9 @@ impl<'db> Type<'db> {
|
||||
ClassLiteral::Dynamic(_) => {
|
||||
Type::NominalInstance(NominalInstanceType(NominalInstanceInner::NonTuple(class)))
|
||||
}
|
||||
ClassLiteral::DynamicNamedTuple(_) => {
|
||||
Type::NominalInstance(NominalInstanceType(NominalInstanceInner::NonTuple(class)))
|
||||
}
|
||||
ClassLiteral::Static(class_literal) => {
|
||||
let specialization = class.into_generic_alias().map(|g| g.specialization(db));
|
||||
match class_literal.known(db) {
|
||||
|
||||
@@ -500,6 +500,9 @@ impl<'db> MroIterator<'db> {
|
||||
ClassLiteral::Dynamic(literal) => {
|
||||
ClassBase::Class(ClassType::NonGeneric(literal.into()))
|
||||
}
|
||||
ClassLiteral::DynamicNamedTuple(literal) => {
|
||||
ClassBase::Class(ClassType::NonGeneric(literal.into()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -524,6 +527,11 @@ impl<'db> MroIterator<'db> {
|
||||
full_mro_iter.next();
|
||||
full_mro_iter
|
||||
}
|
||||
ClassLiteral::DynamicNamedTuple(literal) => {
|
||||
let mut full_mro_iter = literal.mro(self.db).iter();
|
||||
full_mro_iter.next();
|
||||
full_mro_iter
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,14 +303,14 @@ impl<'db> TypedDictType<'db> {
|
||||
|
||||
pub fn definition(self, db: &'db dyn Db) -> Option<Definition<'db>> {
|
||||
match self {
|
||||
TypedDictType::Class(defining_class) => Some(defining_class.definition(db)),
|
||||
TypedDictType::Class(defining_class) => defining_class.definition(db),
|
||||
TypedDictType::Synthesized(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn type_definition(self, db: &'db dyn Db) -> Option<TypeDefinition<'db>> {
|
||||
match self {
|
||||
TypedDictType::Class(defining_class) => Some(defining_class.type_definition(db)),
|
||||
TypedDictType::Class(defining_class) => defining_class.type_definition(db),
|
||||
TypedDictType::Synthesized(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,6 +163,13 @@ export default function Chrome({
|
||||
[workspace, files.index, onRemoveFile],
|
||||
);
|
||||
|
||||
const handleChange = useCallback(
|
||||
(content: string) => {
|
||||
onChangeFile(workspace, content);
|
||||
},
|
||||
[onChangeFile, workspace],
|
||||
);
|
||||
|
||||
const { defaultLayout, onLayoutChange } = useDefaultLayout({
|
||||
groupId: "editor-diagnostics",
|
||||
storage: localStorage,
|
||||
@@ -221,7 +228,7 @@ export default function Chrome({
|
||||
diagnostics={checkResult.diagnostics}
|
||||
workspace={workspace}
|
||||
onMount={handleEditorMount}
|
||||
onChange={(content) => onChangeFile(workspace, content)}
|
||||
onChange={handleChange}
|
||||
onOpenFile={onSelectFile}
|
||||
onVendoredFileChange={onSelectVendoredFile}
|
||||
onBackToUserFile={handleBackToUserFile}
|
||||
|
||||
@@ -58,7 +58,7 @@ export default function Playground() {
|
||||
}
|
||||
}, [files]);
|
||||
|
||||
const handleFileAdded = (workspace: Workspace, name: string) => {
|
||||
const handleFileAdded = useCallback((workspace: Workspace, name: string) => {
|
||||
let handle = null;
|
||||
|
||||
if (name === SETTINGS_FILE_NAME) {
|
||||
@@ -68,69 +68,74 @@ export default function Playground() {
|
||||
}
|
||||
|
||||
dispatchFiles({ type: "add", name, handle, content: "" });
|
||||
};
|
||||
}, []);
|
||||
|
||||
const handleFileChanged = (workspace: Workspace, content: string) => {
|
||||
if (files.selected == null) {
|
||||
return;
|
||||
}
|
||||
const handleFileChanged = useCallback(
|
||||
(workspace: Workspace, content: string) => {
|
||||
if (files.selected == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
dispatchFiles({
|
||||
type: "change",
|
||||
id: files.selected,
|
||||
content,
|
||||
});
|
||||
const handle = files.handles[files.selected];
|
||||
|
||||
const handle = files.handles[files.selected];
|
||||
if (handle != null) {
|
||||
updateFile(workspace, handle, content, setError);
|
||||
} else if (fileName === SETTINGS_FILE_NAME) {
|
||||
updateOptions(workspace, content, setError);
|
||||
}
|
||||
|
||||
if (handle != null) {
|
||||
updateFile(workspace, handle, content, setError);
|
||||
} else if (fileName === SETTINGS_FILE_NAME) {
|
||||
updateOptions(workspace, content, setError);
|
||||
}
|
||||
};
|
||||
dispatchFiles({
|
||||
type: "change",
|
||||
id: files.selected,
|
||||
content,
|
||||
});
|
||||
},
|
||||
[fileName, files.handles, files.selected],
|
||||
);
|
||||
|
||||
const handleFileRenamed = (
|
||||
workspace: Workspace,
|
||||
file: FileId,
|
||||
newName: string,
|
||||
) => {
|
||||
if (newName.startsWith("/")) {
|
||||
setError("File names cannot start with '/'.");
|
||||
return;
|
||||
}
|
||||
if (newName.startsWith("vendored:")) {
|
||||
setError("File names cannot start with 'vendored:'.");
|
||||
return;
|
||||
}
|
||||
const handleFileRenamed = useCallback(
|
||||
(workspace: Workspace, file: FileId, newName: string) => {
|
||||
if (newName.startsWith("/")) {
|
||||
setError("File names cannot start with '/'.");
|
||||
return;
|
||||
}
|
||||
if (newName.startsWith("vendored:")) {
|
||||
setError("File names cannot start with 'vendored:'.");
|
||||
return;
|
||||
}
|
||||
|
||||
const handle = files.handles[file];
|
||||
let newHandle: FileHandle | null = null;
|
||||
if (handle == null) {
|
||||
updateOptions(workspace, null, setError);
|
||||
} else {
|
||||
workspace.closeFile(handle);
|
||||
}
|
||||
const handle = files.handles[file];
|
||||
let newHandle: FileHandle | null = null;
|
||||
if (handle == null) {
|
||||
updateOptions(workspace, null, setError);
|
||||
} else {
|
||||
workspace.closeFile(handle);
|
||||
}
|
||||
|
||||
if (newName === SETTINGS_FILE_NAME) {
|
||||
updateOptions(workspace, files.contents[file], setError);
|
||||
} else {
|
||||
newHandle = workspace.openFile(newName, files.contents[file]);
|
||||
}
|
||||
if (newName === SETTINGS_FILE_NAME) {
|
||||
updateOptions(workspace, files.contents[file], setError);
|
||||
} else {
|
||||
newHandle = workspace.openFile(newName, files.contents[file]);
|
||||
}
|
||||
|
||||
dispatchFiles({ type: "rename", id: file, to: newName, newHandle });
|
||||
};
|
||||
dispatchFiles({ type: "rename", id: file, to: newName, newHandle });
|
||||
},
|
||||
[files.contents, files.handles],
|
||||
);
|
||||
|
||||
const handleFileRemoved = (workspace: Workspace, file: FileId) => {
|
||||
const handle = files.handles[file];
|
||||
if (handle == null) {
|
||||
updateOptions(workspace, null, setError);
|
||||
} else {
|
||||
workspace.closeFile(handle);
|
||||
}
|
||||
const handleFileRemoved = useCallback(
|
||||
(workspace: Workspace, file: FileId) => {
|
||||
const handle = files.handles[file];
|
||||
if (handle == null) {
|
||||
updateOptions(workspace, null, setError);
|
||||
} else {
|
||||
workspace.closeFile(handle);
|
||||
}
|
||||
|
||||
dispatchFiles({ type: "remove", id: file });
|
||||
};
|
||||
dispatchFiles({ type: "remove", id: file });
|
||||
},
|
||||
[files.handles],
|
||||
);
|
||||
|
||||
const handleFileSelected = useCallback((file: FileId) => {
|
||||
dispatchFiles({ type: "selectFile", id: file });
|
||||
|
||||
10
ty.schema.json
generated
10
ty.schema.json
generated
@@ -520,6 +520,16 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"ineffective-final": {
|
||||
"title": "detects calls to `final()` that type checkers cannot interpret",
|
||||
"description": "## What it does\nChecks for calls to `final()` that type checkers cannot interpret.\n\n## Why is this bad?\nThe `final()` function is designed to be used as a decorator. When called directly\nas a function (e.g., `final(type(...))`), type checkers will not understand the\napplication of `final` and will not prevent subclassing.\n\n## Example\n\n```python\nfrom typing import final\n\n# Incorrect: type checkers will not prevent subclassing\nMyClass = final(type(\"MyClass\", (), {}))\n\n# Correct: use `final` as a decorator\n@final\nclass MyClass: ...\n```",
|
||||
"default": "warn",
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/definitions/Level"
|
||||
}
|
||||
]
|
||||
},
|
||||
"instance-layout-conflict": {
|
||||
"title": "detects class definitions that raise `TypeError` due to instance layout conflict",
|
||||
"description": "## What it does\nChecks for classes definitions which will fail at runtime due to\n\"instance memory layout conflicts\".\n\nThis error is usually caused by attempting to combine multiple classes\nthat define non-empty `__slots__` in a class's [Method Resolution Order]\n(MRO), or by attempting to combine multiple builtin classes in a class's\nMRO.\n\n## Why is this bad?\nInheriting from bases with conflicting instance memory layouts\nwill lead to a `TypeError` at runtime.\n\nAn instance memory layout conflict occurs when CPython cannot determine\nthe memory layout instances of a class should have, because the instance\nmemory layout of one of its bases conflicts with the instance memory layout\nof one or more of its other bases.\n\nFor example, if a Python class defines non-empty `__slots__`, this will\nimpact the memory layout of instances of that class. Multiple inheritance\nfrom more than one different class defining non-empty `__slots__` is not\nallowed:\n\n```python\nclass A:\n __slots__ = (\"a\", \"b\")\n\nclass B:\n __slots__ = (\"a\", \"b\") # Even if the values are the same\n\n# TypeError: multiple bases have instance lay-out conflict\nclass C(A, B): ...\n```\n\nAn instance layout conflict can also be caused by attempting to use\nmultiple inheritance with two builtin classes, due to the way that these\nclasses are implemented in a CPython C extension:\n\n```python\nclass A(int, float): ... # TypeError: multiple bases have instance lay-out conflict\n```\n\nNote that pure-Python classes with no `__slots__`, or pure-Python classes\nwith empty `__slots__`, are always compatible:\n\n```python\nclass A: ...\nclass B:\n __slots__ = ()\nclass C:\n __slots__ = (\"a\", \"b\")\n\n# fine\nclass D(A, B, C): ...\n```\n\n## Known problems\nClasses that have \"dynamic\" definitions of `__slots__` (definitions do not consist\nof string literals, or tuples of string literals) are not currently considered disjoint\nbases by ty.\n\nAdditionally, this check is not exhaustive: many C extensions (including several in\nthe standard library) define classes that use extended memory layouts and thus cannot\ncoexist in a single MRO. Since it is currently not possible to represent this fact in\nstub files, having a full knowledge of these classes is also impossible. When it comes\nto classes that do not define `__slots__` at the Python level, therefore, ty, currently\nonly hard-codes a number of cases where it knows that a class will produce instances with\nan atypical memory layout.\n\n## Further reading\n- [CPython documentation: `__slots__`](https://docs.python.org/3/reference/datamodel.html#slots)\n- [CPython documentation: Method Resolution Order](https://docs.python.org/3/glossary.html#term-method-resolution-order)\n\n[Method Resolution Order]: https://docs.python.org/3/glossary.html#term-method-resolution-order",
|
||||
|
||||
Reference in New Issue
Block a user