Compare commits
13 Commits
micha/lsp-
...
david/gene
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
96e7ebb588 | ||
|
|
54c88b599d | ||
|
|
8ed96b04e4 | ||
|
|
0a2536736b | ||
|
|
6aaa9d784a | ||
|
|
d85469e94c | ||
|
|
f184132d69 | ||
|
|
96c491099f | ||
|
|
c1e6ecccc0 | ||
|
|
343c6b6287 | ||
|
|
f40ab81093 | ||
|
|
eee6f25f2e | ||
|
|
013d43a2dd |
2
.github/workflows/ty-ecosystem-analyzer.yaml
vendored
2
.github/workflows/ty-ecosystem-analyzer.yaml
vendored
@@ -67,7 +67,7 @@ jobs:
|
||||
|
||||
cd ..
|
||||
|
||||
uv tool install "git+https://github.com/astral-sh/ecosystem-analyzer@55df3c868f3fa9ab34cff0498dd6106722aac205"
|
||||
uv tool install "git+https://github.com/astral-sh/ecosystem-analyzer@e26ebfb78d372b8b091e1cb1d6fc522e135474c1"
|
||||
|
||||
ecosystem-analyzer \
|
||||
--repository ruff \
|
||||
|
||||
2
.github/workflows/ty-ecosystem-report.yaml
vendored
2
.github/workflows/ty-ecosystem-report.yaml
vendored
@@ -52,7 +52,7 @@ jobs:
|
||||
|
||||
cd ..
|
||||
|
||||
uv tool install "git+https://github.com/astral-sh/ecosystem-analyzer@55df3c868f3fa9ab34cff0498dd6106722aac205"
|
||||
uv tool install "git+https://github.com/astral-sh/ecosystem-analyzer@e26ebfb78d372b8b091e1cb1d6fc522e135474c1"
|
||||
|
||||
ecosystem-analyzer \
|
||||
--verbose \
|
||||
|
||||
3
Cargo.lock
generated
3
Cargo.lock
generated
@@ -4474,7 +4474,6 @@ dependencies = [
|
||||
"quickcheck_macros",
|
||||
"ruff_annotate_snippets",
|
||||
"ruff_db",
|
||||
"ruff_diagnostics",
|
||||
"ruff_index",
|
||||
"ruff_macros",
|
||||
"ruff_memory_usage",
|
||||
@@ -4520,7 +4519,6 @@ dependencies = [
|
||||
"lsp-types",
|
||||
"regex",
|
||||
"ruff_db",
|
||||
"ruff_diagnostics",
|
||||
"ruff_macros",
|
||||
"ruff_notebook",
|
||||
"ruff_python_ast",
|
||||
@@ -4602,7 +4600,6 @@ dependencies = [
|
||||
"js-sys",
|
||||
"log",
|
||||
"ruff_db",
|
||||
"ruff_diagnostics",
|
||||
"ruff_notebook",
|
||||
"ruff_python_formatter",
|
||||
"ruff_source_file",
|
||||
|
||||
@@ -223,7 +223,7 @@ static STATIC_FRAME: Benchmark = Benchmark::new(
|
||||
max_dep_date: "2025-08-09",
|
||||
python_version: PythonVersion::PY311,
|
||||
},
|
||||
950,
|
||||
900,
|
||||
);
|
||||
|
||||
#[track_caller]
|
||||
|
||||
@@ -45,22 +45,3 @@ urllib.request.urlopen(urllib.request.Request(url))
|
||||
# https://github.com/astral-sh/ruff/issues/15522
|
||||
map(urllib.request.urlopen, [])
|
||||
foo = urllib.request.urlopen
|
||||
|
||||
# https://github.com/astral-sh/ruff/issues/21462
|
||||
path = "https://example.com/data.csv"
|
||||
urllib.request.urlretrieve(path, "data.csv")
|
||||
url = "https://example.com/api"
|
||||
urllib.request.Request(url)
|
||||
|
||||
# Test resolved f-strings and concatenated string literals
|
||||
fstring_url = f"https://example.com/data.csv"
|
||||
urllib.request.urlopen(fstring_url)
|
||||
urllib.request.Request(fstring_url)
|
||||
|
||||
concatenated_url = "https://" + "example.com/data.csv"
|
||||
urllib.request.urlopen(concatenated_url)
|
||||
urllib.request.Request(concatenated_url)
|
||||
|
||||
nested_concatenated = "http://" + "example.com" + "/data.csv"
|
||||
urllib.request.urlopen(nested_concatenated)
|
||||
urllib.request.Request(nested_concatenated)
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
import abc
|
||||
import typing
|
||||
|
||||
|
||||
class User: # Test normal class properties
|
||||
@property
|
||||
def name(self): # ERROR: No return
|
||||
f"{self.first_name} {self.last_name}"
|
||||
|
||||
@property
|
||||
def age(self): # OK: Returning something
|
||||
return 100
|
||||
|
||||
def method(self): # OK: Not a property
|
||||
x = 1
|
||||
|
||||
@property
|
||||
def nested(self): # ERROR: Property itself doesn't return
|
||||
def inner():
|
||||
return 0
|
||||
|
||||
@property
|
||||
def stub(self): ... # OK: A stub; doesn't return anything
|
||||
|
||||
|
||||
class UserMeta(metaclass=abc.ABCMeta): # Test properies inside of an ABC class
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def abstr_prop1(self): ... # OK: Abstract methods doesn't need to return anything
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def abstr_prop2(self): # OK: Abstract methods doesn't need to return anything
|
||||
"""
|
||||
A cool docstring
|
||||
"""
|
||||
|
||||
@property
|
||||
def prop1(self): # OK: Returning a value
|
||||
return 1
|
||||
|
||||
@property
|
||||
def prop2(self): # ERROR: Not returning something (even when we are inside an ABC)
|
||||
50
|
||||
|
||||
def method(self): # OK: Not a property
|
||||
x = 1
|
||||
|
||||
|
||||
def func(): # OK: Not a property
|
||||
x = 1
|
||||
|
||||
|
||||
class Proto(typing.Protocol): # Tests for a Protocol class
|
||||
@property
|
||||
def prop1(self) -> int: ... # OK: A stub property
|
||||
|
||||
|
||||
class File: # Extra tests for things like yield/yield from/raise
|
||||
@property
|
||||
def stream1(self): # OK: Yields something
|
||||
yield
|
||||
|
||||
@property
|
||||
def stream2(self): # OK: Yields from something
|
||||
yield from self.stream1
|
||||
|
||||
@property
|
||||
def children(self): # OK: Raises
|
||||
raise ValueError("File does not have children")
|
||||
@@ -347,9 +347,6 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
if checker.is_rule_enabled(Rule::InvalidArgumentName) {
|
||||
pep8_naming::rules::invalid_argument_name_function(checker, function_def);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::PropertyWithoutReturn) {
|
||||
ruff::rules::property_without_return(checker, function_def);
|
||||
}
|
||||
}
|
||||
Stmt::Return(_) => {
|
||||
if checker.is_rule_enabled(Rule::ReturnInInit) {
|
||||
|
||||
@@ -1058,7 +1058,6 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
|
||||
(Ruff, "063") => rules::ruff::rules::AccessAnnotationsFromClassDict,
|
||||
(Ruff, "064") => rules::ruff::rules::NonOctalPermissions,
|
||||
(Ruff, "065") => rules::ruff::rules::LoggingEagerConversion,
|
||||
(Ruff, "066") => rules::ruff::rules::PropertyWithoutReturn,
|
||||
|
||||
(Ruff, "100") => rules::ruff::rules::UnusedNOQA,
|
||||
(Ruff, "101") => rules::ruff::rules::RedirectedNOQA,
|
||||
|
||||
@@ -279,10 +279,3 @@ pub(crate) const fn is_extended_snmp_api_path_detection_enabled(settings: &Linte
|
||||
pub(crate) const fn is_enumerate_for_loop_int_index_enabled(settings: &LinterSettings) -> bool {
|
||||
settings.preview.is_enabled()
|
||||
}
|
||||
|
||||
// https://github.com/astral-sh/ruff/pull/21469
|
||||
pub(crate) const fn is_s310_resolve_string_literal_bindings_enabled(
|
||||
settings: &LinterSettings,
|
||||
) -> bool {
|
||||
settings.preview.is_enabled()
|
||||
}
|
||||
|
||||
@@ -10,11 +10,11 @@ mod tests {
|
||||
use anyhow::Result;
|
||||
use test_case::test_case;
|
||||
|
||||
use crate::assert_diagnostics;
|
||||
use crate::registry::Rule;
|
||||
use crate::settings::LinterSettings;
|
||||
use crate::settings::types::PreviewMode;
|
||||
use crate::test::test_path;
|
||||
use crate::{assert_diagnostics, assert_diagnostics_diff};
|
||||
|
||||
#[test_case(Rule::Assert, Path::new("S101.py"))]
|
||||
#[test_case(Rule::BadFilePermissions, Path::new("S103.py"))]
|
||||
@@ -112,19 +112,14 @@ mod tests {
|
||||
rule_code.noqa_code(),
|
||||
path.to_string_lossy()
|
||||
);
|
||||
|
||||
assert_diagnostics_diff!(
|
||||
snapshot,
|
||||
let diagnostics = test_path(
|
||||
Path::new("flake8_bandit").join(path).as_path(),
|
||||
&LinterSettings {
|
||||
preview: PreviewMode::Disabled,
|
||||
..LinterSettings::for_rule(rule_code)
|
||||
},
|
||||
&LinterSettings {
|
||||
preview: PreviewMode::Enabled,
|
||||
..LinterSettings::for_rule(rule_code)
|
||||
}
|
||||
);
|
||||
},
|
||||
)?;
|
||||
assert_diagnostics!(snapshot, diagnostics);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -4,16 +4,11 @@
|
||||
use itertools::Either;
|
||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_python_ast::{self as ast, Arguments, Decorator, Expr, ExprCall, Operator};
|
||||
use ruff_python_semantic::SemanticModel;
|
||||
use ruff_python_semantic::analyze::typing::find_binding_value;
|
||||
use ruff_text_size::{Ranged, TextRange};
|
||||
|
||||
use crate::Violation;
|
||||
use crate::checkers::ast::Checker;
|
||||
use crate::preview::{
|
||||
is_s310_resolve_string_literal_bindings_enabled, is_suspicious_function_reference_enabled,
|
||||
};
|
||||
use crate::settings::LinterSettings;
|
||||
use crate::preview::is_suspicious_function_reference_enabled;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for calls to `pickle` functions or modules that wrap them.
|
||||
@@ -1021,25 +1016,6 @@ fn suspicious_function(
|
||||
|| has_prefix(chars.skip_while(|c| c.is_whitespace()), "https://")
|
||||
}
|
||||
|
||||
/// Resolves `expr` to its binding and checks if the resolved expression starts with an HTTP or HTTPS prefix.
|
||||
fn expression_starts_with_http_prefix(
|
||||
expr: &Expr,
|
||||
semantic: &SemanticModel,
|
||||
settings: &LinterSettings,
|
||||
) -> bool {
|
||||
let resolved_expression = if is_s310_resolve_string_literal_bindings_enabled(settings)
|
||||
&& let Some(name_expr) = expr.as_name_expr()
|
||||
&& let Some(binding_id) = semantic.only_binding(name_expr)
|
||||
&& let Some(value) = find_binding_value(semantic.binding(binding_id), semantic)
|
||||
{
|
||||
value
|
||||
} else {
|
||||
expr
|
||||
};
|
||||
|
||||
leading_chars(resolved_expression).is_some_and(has_http_prefix)
|
||||
}
|
||||
|
||||
/// Return the leading characters for an expression, if it's a string literal, f-string, or
|
||||
/// string concatenation.
|
||||
fn leading_chars(expr: &Expr) -> Option<impl Iterator<Item = char> + Clone + '_> {
|
||||
@@ -1163,19 +1139,17 @@ fn suspicious_function(
|
||||
// URLOpen (`Request`)
|
||||
["urllib", "request", "Request"] | ["six", "moves", "urllib", "request", "Request"] => {
|
||||
if let Some(arguments) = arguments {
|
||||
// If the `url` argument is a string literal (including resolved bindings), allow `http` and `https` schemes.
|
||||
// If the `url` argument is a string literal or an f-string, allow `http` and `https` schemes.
|
||||
if arguments.args.iter().all(|arg| !arg.is_starred_expr())
|
||||
&& arguments
|
||||
.keywords
|
||||
.iter()
|
||||
.all(|keyword| keyword.arg.is_some())
|
||||
{
|
||||
if let Some(url_expr) = arguments.find_argument_value("url", 0)
|
||||
&& expression_starts_with_http_prefix(
|
||||
url_expr,
|
||||
checker.semantic(),
|
||||
checker.settings(),
|
||||
)
|
||||
if arguments
|
||||
.find_argument_value("url", 0)
|
||||
.and_then(leading_chars)
|
||||
.is_some_and(has_http_prefix)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -1212,25 +1186,19 @@ fn suspicious_function(
|
||||
name.segments() == ["urllib", "request", "Request"]
|
||||
})
|
||||
{
|
||||
if let Some(url_expr) = arguments.find_argument_value("url", 0)
|
||||
&& expression_starts_with_http_prefix(
|
||||
url_expr,
|
||||
checker.semantic(),
|
||||
checker.settings(),
|
||||
)
|
||||
if arguments
|
||||
.find_argument_value("url", 0)
|
||||
.and_then(leading_chars)
|
||||
.is_some_and(has_http_prefix)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If the `url` argument is a string literal (including resolved bindings), allow `http` and `https` schemes.
|
||||
// If the `url` argument is a string literal, allow `http` and `https` schemes.
|
||||
Some(expr) => {
|
||||
if expression_starts_with_http_prefix(
|
||||
expr,
|
||||
checker.semantic(),
|
||||
checker.settings(),
|
||||
) {
|
||||
if leading_chars(expr).is_some_and(has_http_prefix) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,84 +254,3 @@ S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom sch
|
||||
42 | urllib.request.urlopen(urllib.request.Request(url))
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:51:1
|
||||
|
|
||||
49 | # https://github.com/astral-sh/ruff/issues/21462
|
||||
50 | path = "https://example.com/data.csv"
|
||||
51 | urllib.request.urlretrieve(path, "data.csv")
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
52 | url = "https://example.com/api"
|
||||
53 | urllib.request.Request(url)
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:53:1
|
||||
|
|
||||
51 | urllib.request.urlretrieve(path, "data.csv")
|
||||
52 | url = "https://example.com/api"
|
||||
53 | urllib.request.Request(url)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
54 |
|
||||
55 | # Test resolved f-strings and concatenated string literals
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:57:1
|
||||
|
|
||||
55 | # Test resolved f-strings and concatenated string literals
|
||||
56 | fstring_url = f"https://example.com/data.csv"
|
||||
57 | urllib.request.urlopen(fstring_url)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
58 | urllib.request.Request(fstring_url)
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:58:1
|
||||
|
|
||||
56 | fstring_url = f"https://example.com/data.csv"
|
||||
57 | urllib.request.urlopen(fstring_url)
|
||||
58 | urllib.request.Request(fstring_url)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
59 |
|
||||
60 | concatenated_url = "https://" + "example.com/data.csv"
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:61:1
|
||||
|
|
||||
60 | concatenated_url = "https://" + "example.com/data.csv"
|
||||
61 | urllib.request.urlopen(concatenated_url)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
62 | urllib.request.Request(concatenated_url)
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:62:1
|
||||
|
|
||||
60 | concatenated_url = "https://" + "example.com/data.csv"
|
||||
61 | urllib.request.urlopen(concatenated_url)
|
||||
62 | urllib.request.Request(concatenated_url)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
63 |
|
||||
64 | nested_concatenated = "http://" + "example.com" + "/data.csv"
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:65:1
|
||||
|
|
||||
64 | nested_concatenated = "http://" + "example.com" + "/data.csv"
|
||||
65 | urllib.request.urlopen(nested_concatenated)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
66 | urllib.request.Request(nested_concatenated)
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:66:1
|
||||
|
|
||||
64 | nested_concatenated = "http://" + "example.com" + "/data.csv"
|
||||
65 | urllib.request.urlopen(nested_concatenated)
|
||||
66 | urllib.request.Request(nested_concatenated)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
|
||||
---
|
||||
--- Linter settings ---
|
||||
-linter.preview = disabled
|
||||
+linter.preview = enabled
|
||||
S301 `pickle` and modules that wrap it can be unsafe when used to deserialize untrusted data, possible security issue
|
||||
--> S301.py:3:1
|
||||
|
|
||||
1 | import pickle
|
||||
2 |
|
||||
3 | pickle.loads()
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
|
||||
|
||||
--- Summary ---
|
||||
Removed: 0
|
||||
Added: 2
|
||||
|
||||
--- Added ---
|
||||
S301 `pickle` and modules that wrap it can be unsafe when used to deserialize untrusted data, possible security issue
|
||||
--> S301.py:7:5
|
||||
|
|
||||
@@ -19,7 +19,6 @@ S301 `pickle` and modules that wrap it can be unsafe when used to deserialize un
|
||||
8 | foo = pickle.load
|
||||
|
|
||||
|
||||
|
||||
S301 `pickle` and modules that wrap it can be unsafe when used to deserialize untrusted data, possible security issue
|
||||
--> S301.py:8:7
|
||||
|
|
||||
|
||||
@@ -1,15 +1,24 @@
|
||||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
|
||||
---
|
||||
--- Linter settings ---
|
||||
-linter.preview = disabled
|
||||
+linter.preview = enabled
|
||||
S307 Use of possibly insecure function; consider using `ast.literal_eval`
|
||||
--> S307.py:3:7
|
||||
|
|
||||
1 | import os
|
||||
2 |
|
||||
3 | print(eval("1+1")) # S307
|
||||
| ^^^^^^^^^^^
|
||||
4 | print(eval("os.getcwd()")) # S307
|
||||
|
|
||||
|
||||
--- Summary ---
|
||||
Removed: 0
|
||||
Added: 2
|
||||
S307 Use of possibly insecure function; consider using `ast.literal_eval`
|
||||
--> S307.py:4:7
|
||||
|
|
||||
3 | print(eval("1+1")) # S307
|
||||
4 | print(eval("os.getcwd()")) # S307
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
|
||||
--- Added ---
|
||||
S307 Use of possibly insecure function; consider using `ast.literal_eval`
|
||||
--> S307.py:16:5
|
||||
|
|
||||
@@ -19,7 +28,6 @@ S307 Use of possibly insecure function; consider using `ast.literal_eval`
|
||||
17 | foo = eval
|
||||
|
|
||||
|
||||
|
||||
S307 Use of possibly insecure function; consider using `ast.literal_eval`
|
||||
--> S307.py:17:7
|
||||
|
|
||||
|
||||
@@ -1,37 +1,60 @@
|
||||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
|
||||
---
|
||||
--- Linter settings ---
|
||||
-linter.preview = disabled
|
||||
+linter.preview = enabled
|
||||
|
||||
--- Summary ---
|
||||
Removed: 2
|
||||
Added: 4
|
||||
|
||||
--- Removed ---
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:16:1
|
||||
|
|
||||
16 | @mark_safe
|
||||
| ^^^^^^^^^^
|
||||
17 | def some_func():
|
||||
18 | return '<script>alert("evil!")</script>'
|
||||
|
|
||||
|
||||
--> S308.py:6:5
|
||||
|
|
||||
4 | def bad_func():
|
||||
5 | inject = "harmful_input"
|
||||
6 | mark_safe(inject)
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
7 | mark_safe("I will add" + inject + "to my string")
|
||||
8 | mark_safe("I will add %s to my string" % inject)
|
||||
|
|
||||
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:36:1
|
||||
--> S308.py:7:5
|
||||
|
|
||||
5 | inject = "harmful_input"
|
||||
6 | mark_safe(inject)
|
||||
7 | mark_safe("I will add" + inject + "to my string")
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
8 | mark_safe("I will add %s to my string" % inject)
|
||||
9 | mark_safe("I will add {} to my string".format(inject))
|
||||
|
|
||||
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:8:5
|
||||
|
|
||||
36 | @mark_safe
|
||||
| ^^^^^^^^^^
|
||||
37 | def some_func():
|
||||
38 | return '<script>alert("evil!")</script>'
|
||||
6 | mark_safe(inject)
|
||||
7 | mark_safe("I will add" + inject + "to my string")
|
||||
8 | mark_safe("I will add %s to my string" % inject)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
9 | mark_safe("I will add {} to my string".format(inject))
|
||||
10 | mark_safe(f"I will add {inject} to my string")
|
||||
|
|
||||
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:9:5
|
||||
|
|
||||
7 | mark_safe("I will add" + inject + "to my string")
|
||||
8 | mark_safe("I will add %s to my string" % inject)
|
||||
9 | mark_safe("I will add {} to my string".format(inject))
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
10 | mark_safe(f"I will add {inject} to my string")
|
||||
|
|
||||
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:10:5
|
||||
|
|
||||
8 | mark_safe("I will add %s to my string" % inject)
|
||||
9 | mark_safe("I will add {} to my string".format(inject))
|
||||
10 | mark_safe(f"I will add {inject} to my string")
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
11 |
|
||||
12 | def good_func():
|
||||
|
|
||||
|
||||
--- Added ---
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:16:2
|
||||
|
|
||||
@@ -41,6 +64,59 @@ S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
18 | return '<script>alert("evil!")</script>'
|
||||
|
|
||||
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:26:5
|
||||
|
|
||||
24 | def bad_func():
|
||||
25 | inject = "harmful_input"
|
||||
26 | mark_safe(inject)
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
27 | mark_safe("I will add" + inject + "to my string")
|
||||
28 | mark_safe("I will add %s to my string" % inject)
|
||||
|
|
||||
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:27:5
|
||||
|
|
||||
25 | inject = "harmful_input"
|
||||
26 | mark_safe(inject)
|
||||
27 | mark_safe("I will add" + inject + "to my string")
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
28 | mark_safe("I will add %s to my string" % inject)
|
||||
29 | mark_safe("I will add {} to my string".format(inject))
|
||||
|
|
||||
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:28:5
|
||||
|
|
||||
26 | mark_safe(inject)
|
||||
27 | mark_safe("I will add" + inject + "to my string")
|
||||
28 | mark_safe("I will add %s to my string" % inject)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
29 | mark_safe("I will add {} to my string".format(inject))
|
||||
30 | mark_safe(f"I will add {inject} to my string")
|
||||
|
|
||||
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:29:5
|
||||
|
|
||||
27 | mark_safe("I will add" + inject + "to my string")
|
||||
28 | mark_safe("I will add %s to my string" % inject)
|
||||
29 | mark_safe("I will add {} to my string".format(inject))
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
30 | mark_safe(f"I will add {inject} to my string")
|
||||
|
|
||||
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:30:5
|
||||
|
|
||||
28 | mark_safe("I will add %s to my string" % inject)
|
||||
29 | mark_safe("I will add {} to my string".format(inject))
|
||||
30 | mark_safe(f"I will add {inject} to my string")
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
31 |
|
||||
32 | def good_func():
|
||||
|
|
||||
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:36:2
|
||||
@@ -51,7 +127,6 @@ S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
38 | return '<script>alert("evil!")</script>'
|
||||
|
|
||||
|
||||
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:42:5
|
||||
|
|
||||
@@ -61,7 +136,6 @@ S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
43 | foo = mark_safe
|
||||
|
|
||||
|
||||
|
||||
S308 Use of `mark_safe` may expose cross-site scripting vulnerabilities
|
||||
--> S308.py:43:7
|
||||
|
|
||||
|
||||
@@ -1,106 +1,260 @@
|
||||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
|
||||
---
|
||||
--- Linter settings ---
|
||||
-linter.preview = disabled
|
||||
+linter.preview = enabled
|
||||
|
||||
--- Summary ---
|
||||
Removed: 8
|
||||
Added: 2
|
||||
|
||||
--- Removed ---
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:51:1
|
||||
|
|
||||
49 | # https://github.com/astral-sh/ruff/issues/21462
|
||||
50 | path = "https://example.com/data.csv"
|
||||
51 | urllib.request.urlretrieve(path, "data.csv")
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
52 | url = "https://example.com/api"
|
||||
53 | urllib.request.Request(url)
|
||||
|
|
||||
|
||||
--> S310.py:6:1
|
||||
|
|
||||
4 | urllib.request.urlopen(url=f'http://www.google.com')
|
||||
5 | urllib.request.urlopen(url='http://' + 'www' + '.google.com')
|
||||
6 | urllib.request.urlopen(url='http://www.google.com', **kwargs)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
7 | urllib.request.urlopen(url=f'http://www.google.com', **kwargs)
|
||||
8 | urllib.request.urlopen('http://www.google.com')
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:53:1
|
||||
--> S310.py:7:1
|
||||
|
|
||||
5 | urllib.request.urlopen(url='http://' + 'www' + '.google.com')
|
||||
6 | urllib.request.urlopen(url='http://www.google.com', **kwargs)
|
||||
7 | urllib.request.urlopen(url=f'http://www.google.com', **kwargs)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
8 | urllib.request.urlopen('http://www.google.com')
|
||||
9 | urllib.request.urlopen(f'http://www.google.com')
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:10:1
|
||||
|
|
||||
51 | urllib.request.urlretrieve(path, "data.csv")
|
||||
52 | url = "https://example.com/api"
|
||||
53 | urllib.request.Request(url)
|
||||
8 | urllib.request.urlopen('http://www.google.com')
|
||||
9 | urllib.request.urlopen(f'http://www.google.com')
|
||||
10 | urllib.request.urlopen('file:///foo/bar/baz')
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
11 | urllib.request.urlopen(url)
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:11:1
|
||||
|
|
||||
9 | urllib.request.urlopen(f'http://www.google.com')
|
||||
10 | urllib.request.urlopen('file:///foo/bar/baz')
|
||||
11 | urllib.request.urlopen(url)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
54 |
|
||||
55 | # Test resolved f-strings and concatenated string literals
|
||||
12 |
|
||||
13 | urllib.request.Request(url='http://www.google.com')
|
||||
|
|
||||
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:57:1
|
||||
--> S310.py:16:1
|
||||
|
|
||||
55 | # Test resolved f-strings and concatenated string literals
|
||||
56 | fstring_url = f"https://example.com/data.csv"
|
||||
57 | urllib.request.urlopen(fstring_url)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
58 | urllib.request.Request(fstring_url)
|
||||
14 | urllib.request.Request(url=f'http://www.google.com')
|
||||
15 | urllib.request.Request(url='http://' + 'www' + '.google.com')
|
||||
16 | urllib.request.Request(url='http://www.google.com', **kwargs)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
17 | urllib.request.Request(url=f'http://www.google.com', **kwargs)
|
||||
18 | urllib.request.Request('http://www.google.com')
|
||||
|
|
||||
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:58:1
|
||||
--> S310.py:17:1
|
||||
|
|
||||
56 | fstring_url = f"https://example.com/data.csv"
|
||||
57 | urllib.request.urlopen(fstring_url)
|
||||
58 | urllib.request.Request(fstring_url)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
59 |
|
||||
60 | concatenated_url = "https://" + "example.com/data.csv"
|
||||
15 | urllib.request.Request(url='http://' + 'www' + '.google.com')
|
||||
16 | urllib.request.Request(url='http://www.google.com', **kwargs)
|
||||
17 | urllib.request.Request(url=f'http://www.google.com', **kwargs)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
18 | urllib.request.Request('http://www.google.com')
|
||||
19 | urllib.request.Request(f'http://www.google.com')
|
||||
|
|
||||
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:61:1
|
||||
--> S310.py:20:1
|
||||
|
|
||||
60 | concatenated_url = "https://" + "example.com/data.csv"
|
||||
61 | urllib.request.urlopen(concatenated_url)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
62 | urllib.request.Request(concatenated_url)
|
||||
18 | urllib.request.Request('http://www.google.com')
|
||||
19 | urllib.request.Request(f'http://www.google.com')
|
||||
20 | urllib.request.Request('file:///foo/bar/baz')
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
21 | urllib.request.Request(url)
|
||||
|
|
||||
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:62:1
|
||||
--> S310.py:21:1
|
||||
|
|
||||
60 | concatenated_url = "https://" + "example.com/data.csv"
|
||||
61 | urllib.request.urlopen(concatenated_url)
|
||||
62 | urllib.request.Request(concatenated_url)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
63 |
|
||||
64 | nested_concatenated = "http://" + "example.com" + "/data.csv"
|
||||
19 | urllib.request.Request(f'http://www.google.com')
|
||||
20 | urllib.request.Request('file:///foo/bar/baz')
|
||||
21 | urllib.request.Request(url)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
22 |
|
||||
23 | urllib.request.URLopener().open(fullurl='http://www.google.com')
|
||||
|
|
||||
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:65:1
|
||||
--> S310.py:23:1
|
||||
|
|
||||
64 | nested_concatenated = "http://" + "example.com" + "/data.csv"
|
||||
65 | urllib.request.urlopen(nested_concatenated)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
66 | urllib.request.Request(nested_concatenated)
|
||||
21 | urllib.request.Request(url)
|
||||
22 |
|
||||
23 | urllib.request.URLopener().open(fullurl='http://www.google.com')
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
24 | urllib.request.URLopener().open(fullurl=f'http://www.google.com')
|
||||
25 | urllib.request.URLopener().open(fullurl='http://' + 'www' + '.google.com')
|
||||
|
|
||||
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:66:1
|
||||
--> S310.py:24:1
|
||||
|
|
||||
64 | nested_concatenated = "http://" + "example.com" + "/data.csv"
|
||||
65 | urllib.request.urlopen(nested_concatenated)
|
||||
66 | urllib.request.Request(nested_concatenated)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
23 | urllib.request.URLopener().open(fullurl='http://www.google.com')
|
||||
24 | urllib.request.URLopener().open(fullurl=f'http://www.google.com')
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
25 | urllib.request.URLopener().open(fullurl='http://' + 'www' + '.google.com')
|
||||
26 | urllib.request.URLopener().open(fullurl='http://www.google.com', **kwargs)
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:25:1
|
||||
|
|
||||
23 | urllib.request.URLopener().open(fullurl='http://www.google.com')
|
||||
24 | urllib.request.URLopener().open(fullurl=f'http://www.google.com')
|
||||
25 | urllib.request.URLopener().open(fullurl='http://' + 'www' + '.google.com')
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
26 | urllib.request.URLopener().open(fullurl='http://www.google.com', **kwargs)
|
||||
27 | urllib.request.URLopener().open(fullurl=f'http://www.google.com', **kwargs)
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:26:1
|
||||
|
|
||||
24 | urllib.request.URLopener().open(fullurl=f'http://www.google.com')
|
||||
25 | urllib.request.URLopener().open(fullurl='http://' + 'www' + '.google.com')
|
||||
26 | urllib.request.URLopener().open(fullurl='http://www.google.com', **kwargs)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
27 | urllib.request.URLopener().open(fullurl=f'http://www.google.com', **kwargs)
|
||||
28 | urllib.request.URLopener().open('http://www.google.com')
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:27:1
|
||||
|
|
||||
25 | urllib.request.URLopener().open(fullurl='http://' + 'www' + '.google.com')
|
||||
26 | urllib.request.URLopener().open(fullurl='http://www.google.com', **kwargs)
|
||||
27 | urllib.request.URLopener().open(fullurl=f'http://www.google.com', **kwargs)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
28 | urllib.request.URLopener().open('http://www.google.com')
|
||||
29 | urllib.request.URLopener().open(f'http://www.google.com')
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:28:1
|
||||
|
|
||||
26 | urllib.request.URLopener().open(fullurl='http://www.google.com', **kwargs)
|
||||
27 | urllib.request.URLopener().open(fullurl=f'http://www.google.com', **kwargs)
|
||||
28 | urllib.request.URLopener().open('http://www.google.com')
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
29 | urllib.request.URLopener().open(f'http://www.google.com')
|
||||
30 | urllib.request.URLopener().open('http://' + 'www' + '.google.com')
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:29:1
|
||||
|
|
||||
27 | urllib.request.URLopener().open(fullurl=f'http://www.google.com', **kwargs)
|
||||
28 | urllib.request.URLopener().open('http://www.google.com')
|
||||
29 | urllib.request.URLopener().open(f'http://www.google.com')
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
30 | urllib.request.URLopener().open('http://' + 'www' + '.google.com')
|
||||
31 | urllib.request.URLopener().open('file:///foo/bar/baz')
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:30:1
|
||||
|
|
||||
28 | urllib.request.URLopener().open('http://www.google.com')
|
||||
29 | urllib.request.URLopener().open(f'http://www.google.com')
|
||||
30 | urllib.request.URLopener().open('http://' + 'www' + '.google.com')
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
31 | urllib.request.URLopener().open('file:///foo/bar/baz')
|
||||
32 | urllib.request.URLopener().open(url)
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:31:1
|
||||
|
|
||||
29 | urllib.request.URLopener().open(f'http://www.google.com')
|
||||
30 | urllib.request.URLopener().open('http://' + 'www' + '.google.com')
|
||||
31 | urllib.request.URLopener().open('file:///foo/bar/baz')
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
32 | urllib.request.URLopener().open(url)
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:32:1
|
||||
|
|
||||
30 | urllib.request.URLopener().open('http://' + 'www' + '.google.com')
|
||||
31 | urllib.request.URLopener().open('file:///foo/bar/baz')
|
||||
32 | urllib.request.URLopener().open(url)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
33 |
|
||||
34 | urllib.request.urlopen(url=urllib.request.Request('http://www.google.com'))
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:37:1
|
||||
|
|
||||
35 | urllib.request.urlopen(url=urllib.request.Request(f'http://www.google.com'))
|
||||
36 | urllib.request.urlopen(url=urllib.request.Request('http://' + 'www' + '.google.com'))
|
||||
37 | urllib.request.urlopen(url=urllib.request.Request('http://www.google.com'), **kwargs)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
38 | urllib.request.urlopen(url=urllib.request.Request(f'http://www.google.com'), **kwargs)
|
||||
39 | urllib.request.urlopen(urllib.request.Request('http://www.google.com'))
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:38:1
|
||||
|
|
||||
36 | urllib.request.urlopen(url=urllib.request.Request('http://' + 'www' + '.google.com'))
|
||||
37 | urllib.request.urlopen(url=urllib.request.Request('http://www.google.com'), **kwargs)
|
||||
38 | urllib.request.urlopen(url=urllib.request.Request(f'http://www.google.com'), **kwargs)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
39 | urllib.request.urlopen(urllib.request.Request('http://www.google.com'))
|
||||
40 | urllib.request.urlopen(urllib.request.Request(f'http://www.google.com'))
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:41:1
|
||||
|
|
||||
39 | urllib.request.urlopen(urllib.request.Request('http://www.google.com'))
|
||||
40 | urllib.request.urlopen(urllib.request.Request(f'http://www.google.com'))
|
||||
41 | urllib.request.urlopen(urllib.request.Request('file:///foo/bar/baz'))
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
42 | urllib.request.urlopen(urllib.request.Request(url))
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:41:24
|
||||
|
|
||||
39 | urllib.request.urlopen(urllib.request.Request('http://www.google.com'))
|
||||
40 | urllib.request.urlopen(urllib.request.Request(f'http://www.google.com'))
|
||||
41 | urllib.request.urlopen(urllib.request.Request('file:///foo/bar/baz'))
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
42 | urllib.request.urlopen(urllib.request.Request(url))
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:42:1
|
||||
|
|
||||
40 | urllib.request.urlopen(urllib.request.Request(f'http://www.google.com'))
|
||||
41 | urllib.request.urlopen(urllib.request.Request('file:///foo/bar/baz'))
|
||||
42 | urllib.request.urlopen(urllib.request.Request(url))
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:42:24
|
||||
|
|
||||
40 | urllib.request.urlopen(urllib.request.Request(f'http://www.google.com'))
|
||||
41 | urllib.request.urlopen(urllib.request.Request('file:///foo/bar/baz'))
|
||||
42 | urllib.request.urlopen(urllib.request.Request(url))
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
|
||||
--- Added ---
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:46:5
|
||||
|
|
||||
@@ -110,7 +264,6 @@ S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom sch
|
||||
47 | foo = urllib.request.urlopen
|
||||
|
|
||||
|
||||
|
||||
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
|
||||
--> S310.py:47:7
|
||||
|
|
||||
@@ -118,6 +271,4 @@ S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom sch
|
||||
46 | map(urllib.request.urlopen, [])
|
||||
47 | foo = urllib.request.urlopen
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
48 |
|
||||
49 | # https://github.com/astral-sh/ruff/issues/21462
|
||||
|
|
||||
|
||||
@@ -1,15 +1,103 @@
|
||||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
|
||||
---
|
||||
--- Linter settings ---
|
||||
-linter.preview = disabled
|
||||
+linter.preview = enabled
|
||||
S311 Standard pseudo-random generators are not suitable for cryptographic purposes
|
||||
--> S311.py:10:1
|
||||
|
|
||||
9 | # Errors
|
||||
10 | random.Random()
|
||||
| ^^^^^^^^^^^^^^^
|
||||
11 | random.random()
|
||||
12 | random.randrange()
|
||||
|
|
||||
|
||||
--- Summary ---
|
||||
Removed: 0
|
||||
Added: 2
|
||||
S311 Standard pseudo-random generators are not suitable for cryptographic purposes
|
||||
--> S311.py:11:1
|
||||
|
|
||||
9 | # Errors
|
||||
10 | random.Random()
|
||||
11 | random.random()
|
||||
| ^^^^^^^^^^^^^^^
|
||||
12 | random.randrange()
|
||||
13 | random.randint()
|
||||
|
|
||||
|
||||
S311 Standard pseudo-random generators are not suitable for cryptographic purposes
|
||||
--> S311.py:12:1
|
||||
|
|
||||
10 | random.Random()
|
||||
11 | random.random()
|
||||
12 | random.randrange()
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
13 | random.randint()
|
||||
14 | random.choice()
|
||||
|
|
||||
|
||||
S311 Standard pseudo-random generators are not suitable for cryptographic purposes
|
||||
--> S311.py:13:1
|
||||
|
|
||||
11 | random.random()
|
||||
12 | random.randrange()
|
||||
13 | random.randint()
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
14 | random.choice()
|
||||
15 | random.choices()
|
||||
|
|
||||
|
||||
S311 Standard pseudo-random generators are not suitable for cryptographic purposes
|
||||
--> S311.py:14:1
|
||||
|
|
||||
12 | random.randrange()
|
||||
13 | random.randint()
|
||||
14 | random.choice()
|
||||
| ^^^^^^^^^^^^^^^
|
||||
15 | random.choices()
|
||||
16 | random.uniform()
|
||||
|
|
||||
|
||||
S311 Standard pseudo-random generators are not suitable for cryptographic purposes
|
||||
--> S311.py:15:1
|
||||
|
|
||||
13 | random.randint()
|
||||
14 | random.choice()
|
||||
15 | random.choices()
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
16 | random.uniform()
|
||||
17 | random.triangular()
|
||||
|
|
||||
|
||||
S311 Standard pseudo-random generators are not suitable for cryptographic purposes
|
||||
--> S311.py:16:1
|
||||
|
|
||||
14 | random.choice()
|
||||
15 | random.choices()
|
||||
16 | random.uniform()
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
17 | random.triangular()
|
||||
18 | random.randbytes()
|
||||
|
|
||||
|
||||
S311 Standard pseudo-random generators are not suitable for cryptographic purposes
|
||||
--> S311.py:17:1
|
||||
|
|
||||
15 | random.choices()
|
||||
16 | random.uniform()
|
||||
17 | random.triangular()
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
18 | random.randbytes()
|
||||
|
|
||||
|
||||
S311 Standard pseudo-random generators are not suitable for cryptographic purposes
|
||||
--> S311.py:18:1
|
||||
|
|
||||
16 | random.uniform()
|
||||
17 | random.triangular()
|
||||
18 | random.randbytes()
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
19 |
|
||||
20 | # Unrelated
|
||||
|
|
||||
|
||||
--- Added ---
|
||||
S311 Standard pseudo-random generators are not suitable for cryptographic purposes
|
||||
--> S311.py:26:5
|
||||
|
|
||||
@@ -19,7 +107,6 @@ S311 Standard pseudo-random generators are not suitable for cryptographic purpos
|
||||
27 | foo = random.randrange
|
||||
|
|
||||
|
||||
|
||||
S311 Standard pseudo-random generators are not suitable for cryptographic purposes
|
||||
--> S311.py:27:7
|
||||
|
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
|
||||
---
|
||||
--- Linter settings ---
|
||||
-linter.preview = disabled
|
||||
+linter.preview = enabled
|
||||
S312 Telnet is considered insecure. Use SSH or some other encrypted protocol.
|
||||
--> S312.py:3:1
|
||||
|
|
||||
1 | from telnetlib import Telnet
|
||||
2 |
|
||||
3 | Telnet("localhost", 23)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
|
||||
--- Summary ---
|
||||
Removed: 0
|
||||
Added: 3
|
||||
|
||||
--- Added ---
|
||||
S312 Telnet is considered insecure. Use SSH or some other encrypted protocol.
|
||||
--> S312.py:7:5
|
||||
|
|
||||
@@ -19,7 +19,6 @@ S312 Telnet is considered insecure. Use SSH or some other encrypted protocol.
|
||||
8 | foo = Telnet
|
||||
|
|
||||
|
||||
|
||||
S312 Telnet is considered insecure. Use SSH or some other encrypted protocol.
|
||||
--> S312.py:8:7
|
||||
|
|
||||
@@ -31,7 +30,6 @@ S312 Telnet is considered insecure. Use SSH or some other encrypted protocol.
|
||||
10 | import telnetlib
|
||||
|
|
||||
|
||||
|
||||
S312 Telnet is considered insecure. Use SSH or some other encrypted protocol.
|
||||
--> S312.py:11:5
|
||||
|
|
||||
@@ -41,3 +39,13 @@ S312 Telnet is considered insecure. Use SSH or some other encrypted protocol.
|
||||
12 |
|
||||
13 | from typing import Annotated
|
||||
|
|
||||
|
||||
S312 Telnet is considered insecure. Use SSH or some other encrypted protocol.
|
||||
--> S312.py:14:24
|
||||
|
|
||||
13 | from typing import Annotated
|
||||
14 | foo: Annotated[Telnet, telnetlib.Telnet()]
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
15 |
|
||||
16 | def _() -> Telnet: ...
|
||||
|
|
||||
|
||||
@@ -1,15 +1,26 @@
|
||||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
|
||||
---
|
||||
--- Linter settings ---
|
||||
-linter.preview = disabled
|
||||
+linter.preview = enabled
|
||||
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
--> S508.py:3:25
|
||||
|
|
||||
1 | from pysnmp.hlapi import CommunityData
|
||||
2 |
|
||||
3 | CommunityData("public", mpModel=0) # S508
|
||||
| ^^^^^^^^^
|
||||
4 | CommunityData("public", mpModel=1) # S508
|
||||
|
|
||||
|
||||
--- Summary ---
|
||||
Removed: 0
|
||||
Added: 8
|
||||
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
--> S508.py:4:25
|
||||
|
|
||||
3 | CommunityData("public", mpModel=0) # S508
|
||||
4 | CommunityData("public", mpModel=1) # S508
|
||||
| ^^^^^^^^^
|
||||
5 |
|
||||
6 | CommunityData("public", mpModel=2) # OK
|
||||
|
|
||||
|
||||
--- Added ---
|
||||
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
--> S508.py:18:46
|
||||
|
|
||||
@@ -21,7 +32,6 @@ S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
20 | pysnmp.hlapi.v1arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||
|
|
||||
|
||||
|
||||
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
--> S508.py:19:58
|
||||
|
|
||||
@@ -32,7 +42,6 @@ S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
21 | pysnmp.hlapi.v1arch.CommunityData("public", mpModel=0) # S508
|
||||
|
|
||||
|
||||
|
||||
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
--> S508.py:20:53
|
||||
|
|
||||
@@ -44,7 +53,6 @@ S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
22 | pysnmp.hlapi.v3arch.asyncio.auth.CommunityData("public", mpModel=0) # S508
|
||||
|
|
||||
|
||||
|
||||
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
--> S508.py:21:45
|
||||
|
|
||||
@@ -56,7 +64,6 @@ S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
23 | pysnmp.hlapi.v3arch.asyncio.CommunityData("public", mpModel=0) # S508
|
||||
|
|
||||
|
||||
|
||||
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
--> S508.py:22:58
|
||||
|
|
||||
@@ -68,7 +75,6 @@ S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
24 | pysnmp.hlapi.v3arch.CommunityData("public", mpModel=0) # S508
|
||||
|
|
||||
|
||||
|
||||
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
--> S508.py:23:53
|
||||
|
|
||||
@@ -80,7 +86,6 @@ S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
25 | pysnmp.hlapi.auth.CommunityData("public", mpModel=0) # S508
|
||||
|
|
||||
|
||||
|
||||
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
--> S508.py:24:45
|
||||
|
|
||||
@@ -91,7 +96,6 @@ S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
25 | pysnmp.hlapi.auth.CommunityData("public", mpModel=0) # S508
|
||||
|
|
||||
|
||||
|
||||
S508 The use of SNMPv1 and SNMPv2 is insecure. Use SNMPv3 if able.
|
||||
--> S508.py:25:43
|
||||
|
|
||||
|
||||
@@ -1,15 +1,24 @@
|
||||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
|
||||
---
|
||||
--- Linter settings ---
|
||||
-linter.preview = disabled
|
||||
+linter.preview = enabled
|
||||
S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv` is insecure.
|
||||
--> S509.py:4:12
|
||||
|
|
||||
4 | insecure = UsmUserData("securityName") # S509
|
||||
| ^^^^^^^^^^^
|
||||
5 | auth_no_priv = UsmUserData("securityName", "authName") # S509
|
||||
|
|
||||
|
||||
--- Summary ---
|
||||
Removed: 0
|
||||
Added: 4
|
||||
S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv` is insecure.
|
||||
--> S509.py:5:16
|
||||
|
|
||||
4 | insecure = UsmUserData("securityName") # S509
|
||||
5 | auth_no_priv = UsmUserData("securityName", "authName") # S509
|
||||
| ^^^^^^^^^^^
|
||||
6 |
|
||||
7 | less_insecure = UsmUserData("securityName", "authName", "privName") # OK
|
||||
|
|
||||
|
||||
--- Added ---
|
||||
S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv` is insecure.
|
||||
--> S509.py:15:1
|
||||
|
|
||||
@@ -21,7 +30,6 @@ S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv`
|
||||
17 | pysnmp.hlapi.v3arch.asyncio.auth.UsmUserData("user") # S509
|
||||
|
|
||||
|
||||
|
||||
S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv` is insecure.
|
||||
--> S509.py:16:1
|
||||
|
|
||||
@@ -32,7 +40,6 @@ S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv`
|
||||
18 | pysnmp.hlapi.auth.UsmUserData("user") # S509
|
||||
|
|
||||
|
||||
|
||||
S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv` is insecure.
|
||||
--> S509.py:17:1
|
||||
|
|
||||
@@ -43,7 +50,6 @@ S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv`
|
||||
18 | pysnmp.hlapi.auth.UsmUserData("user") # S509
|
||||
|
|
||||
|
||||
|
||||
S509 You should not use SNMPv3 without encryption. `noAuthNoPriv` & `authNoPriv` is insecure.
|
||||
--> S509.py:18:1
|
||||
|
|
||||
|
||||
@@ -25,11 +25,6 @@ use crate::rules::flake8_boolean_trap::helpers::is_allowed_func_def;
|
||||
/// keyword-only argument, to force callers to be explicit when providing
|
||||
/// the argument.
|
||||
///
|
||||
/// This rule exempts methods decorated with [`@typing.override`][override],
|
||||
/// since changing the signature of a subclass method that overrides a
|
||||
/// superclass method may cause type checkers to complain about a violation of
|
||||
/// the Liskov Substitution Principle.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// from math import ceil, floor
|
||||
@@ -94,8 +89,6 @@ use crate::rules::flake8_boolean_trap::helpers::is_allowed_func_def;
|
||||
/// ## References
|
||||
/// - [Python documentation: Calls](https://docs.python.org/3/reference/expressions.html#calls)
|
||||
/// - [_How to Avoid “The Boolean Trap”_ by Adam Johnson](https://adamj.eu/tech/2021/07/10/python-type-hints-how-to-avoid-the-boolean-trap/)
|
||||
///
|
||||
/// [override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(stable_since = "v0.0.127")]
|
||||
pub(crate) struct BooleanDefaultValuePositionalArgument;
|
||||
|
||||
@@ -28,7 +28,7 @@ use crate::rules::flake8_boolean_trap::helpers::is_allowed_func_def;
|
||||
/// the argument.
|
||||
///
|
||||
/// Dunder methods that define operators are exempt from this rule, as are
|
||||
/// setters and [`@override`][override] definitions.
|
||||
/// setters and `@override` definitions.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
@@ -93,8 +93,6 @@ use crate::rules::flake8_boolean_trap::helpers::is_allowed_func_def;
|
||||
/// ## References
|
||||
/// - [Python documentation: Calls](https://docs.python.org/3/reference/expressions.html#calls)
|
||||
/// - [_How to Avoid “The Boolean Trap”_ by Adam Johnson](https://adamj.eu/tech/2021/07/10/python-type-hints-how-to-avoid-the-boolean-trap/)
|
||||
///
|
||||
/// [override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(stable_since = "v0.0.127")]
|
||||
pub(crate) struct BooleanTypeHintPositionalArgument;
|
||||
|
||||
@@ -17,8 +17,6 @@ use crate::rules::flake8_builtins::helpers::shadows_builtin;
|
||||
/// non-obvious errors, as readers may mistake the argument for the
|
||||
/// builtin and vice versa.
|
||||
///
|
||||
/// Function definitions decorated with [`@override`][override] or
|
||||
/// [`@overload`][overload] are exempt from this rule by default.
|
||||
/// Builtins can be marked as exceptions to this rule via the
|
||||
/// [`lint.flake8-builtins.ignorelist`] configuration option.
|
||||
///
|
||||
@@ -50,9 +48,6 @@ use crate::rules::flake8_builtins::helpers::shadows_builtin;
|
||||
/// ## References
|
||||
/// - [_Is it bad practice to use a built-in function name as an attribute or method identifier?_](https://stackoverflow.com/questions/9109333/is-it-bad-practice-to-use-a-built-in-function-name-as-an-attribute-or-method-ide)
|
||||
/// - [_Why is it a bad idea to name a variable `id` in Python?_](https://stackoverflow.com/questions/77552/id-is-a-bad-variable-name-in-python)
|
||||
///
|
||||
/// [override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
/// [overload]: https://docs.python.org/3/library/typing.html#typing.overload
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(stable_since = "v0.0.48")]
|
||||
pub(crate) struct BuiltinArgumentShadowing {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_python_ast::helpers::map_subscript;
|
||||
use ruff_python_ast::whitespace::trailing_comment_start_offset;
|
||||
use ruff_python_ast::{Expr, ExprStringLiteral, Stmt, StmtExpr};
|
||||
use ruff_python_semantic::{ScopeKind, SemanticModel};
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
use crate::checkers::ast::Checker;
|
||||
@@ -99,7 +101,7 @@ pub(crate) fn unnecessary_placeholder(checker: &Checker, body: &[Stmt]) {
|
||||
// Ellipses are significant in protocol methods and abstract methods.
|
||||
// Specifically, Pyright uses the presence of an ellipsis to indicate that
|
||||
// a method is a stub, rather than a default implementation.
|
||||
if checker.semantic().in_protocol_or_abstract_method() {
|
||||
if in_protocol_or_abstract_method(checker.semantic()) {
|
||||
return;
|
||||
}
|
||||
Placeholder::Ellipsis
|
||||
@@ -161,3 +163,21 @@ impl std::fmt::Display for Placeholder {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Return `true` if the [`SemanticModel`] is in a `typing.Protocol` subclass or an abstract
|
||||
/// method.
|
||||
fn in_protocol_or_abstract_method(semantic: &SemanticModel) -> bool {
|
||||
semantic.current_scopes().any(|scope| match scope.kind {
|
||||
ScopeKind::Class(class_def) => class_def
|
||||
.bases()
|
||||
.iter()
|
||||
.any(|base| semantic.match_typing_expr(map_subscript(base), "Protocol")),
|
||||
ScopeKind::Function(function_def) => {
|
||||
ruff_python_semantic::analyze::visibility::is_abstract(
|
||||
&function_def.decorator_list,
|
||||
semantic,
|
||||
)
|
||||
}
|
||||
_ => false,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -60,16 +60,6 @@ impl Violation for UnusedFunctionArgument {
|
||||
/// prefixed with an underscore, or some other value that adheres to the
|
||||
/// [`lint.dummy-variable-rgx`] pattern.
|
||||
///
|
||||
/// This rule exempts methods decorated with [`@typing.override`][override].
|
||||
/// Removing a parameter from a subclass method (or changing a parameter's
|
||||
/// name) may cause type checkers to complain about a violation of the Liskov
|
||||
/// Substitution Principle if it means that the method now incompatibly
|
||||
/// overrides a method defined on a superclass. Explicitly decorating an
|
||||
/// overriding method with `@override` signals to Ruff that the method is
|
||||
/// intended to override a superclass method and that a type checker will
|
||||
/// enforce that it does so; Ruff therefore knows that it should not enforce
|
||||
/// rules about unused arguments on such methods.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// class Class:
|
||||
@@ -86,8 +76,6 @@ impl Violation for UnusedFunctionArgument {
|
||||
///
|
||||
/// ## Options
|
||||
/// - `lint.dummy-variable-rgx`
|
||||
///
|
||||
/// [override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(stable_since = "v0.0.168")]
|
||||
pub(crate) struct UnusedMethodArgument {
|
||||
@@ -113,16 +101,6 @@ impl Violation for UnusedMethodArgument {
|
||||
/// prefixed with an underscore, or some other value that adheres to the
|
||||
/// [`lint.dummy-variable-rgx`] pattern.
|
||||
///
|
||||
/// This rule exempts methods decorated with [`@typing.override`][override].
|
||||
/// Removing a parameter from a subclass method (or changing a parameter's
|
||||
/// name) may cause type checkers to complain about a violation of the Liskov
|
||||
/// Substitution Principle if it means that the method now incompatibly
|
||||
/// overrides a method defined on a superclass. Explicitly decorating an
|
||||
/// overriding method with `@override` signals to Ruff that the method is
|
||||
/// intended to override a superclass method and that a type checker will
|
||||
/// enforce that it does so; Ruff therefore knows that it should not enforce
|
||||
/// rules about unused arguments on such methods.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// class Class:
|
||||
@@ -141,8 +119,6 @@ impl Violation for UnusedMethodArgument {
|
||||
///
|
||||
/// ## Options
|
||||
/// - `lint.dummy-variable-rgx`
|
||||
///
|
||||
/// [override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(stable_since = "v0.0.168")]
|
||||
pub(crate) struct UnusedClassMethodArgument {
|
||||
@@ -168,16 +144,6 @@ impl Violation for UnusedClassMethodArgument {
|
||||
/// prefixed with an underscore, or some other value that adheres to the
|
||||
/// [`lint.dummy-variable-rgx`] pattern.
|
||||
///
|
||||
/// This rule exempts methods decorated with [`@typing.override`][override].
|
||||
/// Removing a parameter from a subclass method (or changing a parameter's
|
||||
/// name) may cause type checkers to complain about a violation of the Liskov
|
||||
/// Substitution Principle if it means that the method now incompatibly
|
||||
/// overrides a method defined on a superclass. Explicitly decorating an
|
||||
/// overriding method with `@override` signals to Ruff that the method is
|
||||
/// intended to override a superclass method, and that a type checker will
|
||||
/// enforce that it does so; Ruff therefore knows that it should not enforce
|
||||
/// rules about unused arguments on such methods.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// class Class:
|
||||
@@ -196,8 +162,6 @@ impl Violation for UnusedClassMethodArgument {
|
||||
///
|
||||
/// ## Options
|
||||
/// - `lint.dummy-variable-rgx`
|
||||
///
|
||||
/// [override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(stable_since = "v0.0.168")]
|
||||
pub(crate) struct UnusedStaticMethodArgument {
|
||||
|
||||
@@ -23,7 +23,7 @@ use crate::checkers::ast::Checker;
|
||||
/// > mixedCase is allowed only in contexts where that’s already the
|
||||
/// > prevailing style (e.g. threading.py), to retain backwards compatibility.
|
||||
///
|
||||
/// Methods decorated with [`@typing.override`][override] are ignored.
|
||||
/// Methods decorated with `@typing.override` are ignored.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
@@ -43,8 +43,6 @@ use crate::checkers::ast::Checker;
|
||||
///
|
||||
/// [PEP 8]: https://peps.python.org/pep-0008/#function-and-method-arguments
|
||||
/// [preview]: https://docs.astral.sh/ruff/preview/
|
||||
///
|
||||
/// [override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(stable_since = "v0.0.77")]
|
||||
pub(crate) struct InvalidArgumentName {
|
||||
|
||||
@@ -24,11 +24,6 @@ use crate::rules::pep8_naming::settings::IgnoreNames;
|
||||
/// to ignore all functions starting with `test_` from this rule, set the
|
||||
/// [`lint.pep8-naming.extend-ignore-names`] option to `["test_*"]`.
|
||||
///
|
||||
/// This rule exempts methods decorated with [`@typing.override`][override].
|
||||
/// Explicitly decorating a method with `@override` signals to Ruff that the method is intended
|
||||
/// to override a superclass method, and that a type checker will enforce that it does so. Ruff
|
||||
/// therefore knows that it should not enforce naming conventions on such methods.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// def myFunction():
|
||||
@@ -46,7 +41,6 @@ use crate::rules::pep8_naming::settings::IgnoreNames;
|
||||
/// - `lint.pep8-naming.extend-ignore-names`
|
||||
///
|
||||
/// [PEP 8]: https://peps.python.org/pep-0008/#function-and-variable-names
|
||||
/// [override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(stable_since = "v0.0.77")]
|
||||
pub(crate) struct InvalidFunctionName {
|
||||
|
||||
@@ -169,12 +169,7 @@ impl Violation for UndocumentedPublicClass {
|
||||
/// If the codebase adheres to a standard format for method docstrings, follow
|
||||
/// that format for consistency.
|
||||
///
|
||||
/// This rule exempts methods decorated with [`@typing.override`][override],
|
||||
/// since it is a common practice to document a method on a superclass but not
|
||||
/// on an overriding method in a subclass.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```python
|
||||
/// class Cat(Animal):
|
||||
/// def greet(self, happy: bool = True):
|
||||
@@ -185,7 +180,6 @@ impl Violation for UndocumentedPublicClass {
|
||||
/// ```
|
||||
///
|
||||
/// Use instead (in the NumPy docstring format):
|
||||
///
|
||||
/// ```python
|
||||
/// class Cat(Animal):
|
||||
/// def greet(self, happy: bool = True):
|
||||
@@ -208,7 +202,6 @@ impl Violation for UndocumentedPublicClass {
|
||||
/// ```
|
||||
///
|
||||
/// Or (in the Google docstring format):
|
||||
///
|
||||
/// ```python
|
||||
/// class Cat(Animal):
|
||||
/// def greet(self, happy: bool = True):
|
||||
@@ -234,8 +227,6 @@ impl Violation for UndocumentedPublicClass {
|
||||
/// - [PEP 287 – reStructuredText Docstring Format](https://peps.python.org/pep-0287/)
|
||||
/// - [NumPy Style Guide](https://numpydoc.readthedocs.io/en/latest/format.html)
|
||||
/// - [Google Python Style Guide - Docstrings](https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings)
|
||||
///
|
||||
/// [override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(stable_since = "v0.0.70")]
|
||||
pub(crate) struct UndocumentedPublicMethod;
|
||||
|
||||
@@ -21,7 +21,7 @@ use crate::rules::pylint::helpers::is_known_dunder_method;
|
||||
///
|
||||
/// This rule will detect all methods starting and ending with at least
|
||||
/// one underscore (e.g., `_str_`), but ignores known dunder methods (like
|
||||
/// `__init__`), as well as methods that are marked with [`@override`][override].
|
||||
/// `__init__`), as well as methods that are marked with `@override`.
|
||||
///
|
||||
/// Additional dunder methods names can be allowed via the
|
||||
/// [`lint.pylint.allow-dunder-method-names`] setting.
|
||||
@@ -42,8 +42,6 @@ use crate::rules::pylint::helpers::is_known_dunder_method;
|
||||
///
|
||||
/// ## Options
|
||||
/// - `lint.pylint.allow-dunder-method-names`
|
||||
///
|
||||
/// [override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(preview_since = "v0.0.285")]
|
||||
pub(crate) struct BadDunderMethodName {
|
||||
|
||||
@@ -17,16 +17,6 @@ use crate::rules::flake8_unused_arguments::rules::is_not_implemented_stub_with_v
|
||||
/// Unused `self` parameters are usually a sign of a method that could be
|
||||
/// replaced by a function, class method, or static method.
|
||||
///
|
||||
/// This rule exempts methods decorated with [`@typing.override`][override].
|
||||
/// Converting an instance method into a static method or class method may
|
||||
/// cause type checkers to complain about a violation of the Liskov
|
||||
/// Substitution Principle if it means that the method now incompatibly
|
||||
/// overrides a method defined on a superclass. Explicitly decorating an
|
||||
/// overriding method with `@override` signals to Ruff that the method is
|
||||
/// intended to override a superclass method and that a type checker will
|
||||
/// enforce that it does so; Ruff therefore knows that it should not enforce
|
||||
/// rules about unused `self` parameters on such methods.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// class Person:
|
||||
@@ -48,8 +38,6 @@ use crate::rules::flake8_unused_arguments::rules::is_not_implemented_stub_with_v
|
||||
/// def greeting():
|
||||
/// print("Greetings friend!")
|
||||
/// ```
|
||||
///
|
||||
/// [override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(preview_since = "v0.0.286")]
|
||||
pub(crate) struct NoSelfUse {
|
||||
|
||||
@@ -12,16 +12,6 @@ use crate::checkers::ast::Checker;
|
||||
/// By default, this rule allows up to five arguments, as configured by the
|
||||
/// [`lint.pylint.max-args`] option.
|
||||
///
|
||||
/// This rule exempts methods decorated with [`@typing.override`][override].
|
||||
/// Changing the signature of a subclass method may cause type checkers to
|
||||
/// complain about a violation of the Liskov Substitution Principle if it
|
||||
/// means that the method now incompatibly overrides a method defined on a
|
||||
/// superclass. Explicitly decorating an overriding method with `@override`
|
||||
/// signals to Ruff that the method is intended to override a superclass
|
||||
/// method and that a type checker will enforce that it does so; Ruff
|
||||
/// therefore knows that it should not enforce rules about methods having
|
||||
/// too many arguments.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Functions with many arguments are harder to understand, maintain, and call.
|
||||
/// Consider refactoring functions with many arguments into smaller functions
|
||||
@@ -53,8 +43,6 @@ use crate::checkers::ast::Checker;
|
||||
///
|
||||
/// ## Options
|
||||
/// - `lint.pylint.max-args`
|
||||
///
|
||||
/// [override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(stable_since = "v0.0.238")]
|
||||
pub(crate) struct TooManyArguments {
|
||||
|
||||
@@ -21,16 +21,6 @@ use crate::checkers::ast::Checker;
|
||||
/// with fewer arguments, using objects to group related arguments, or migrating to
|
||||
/// [keyword-only arguments](https://docs.python.org/3/tutorial/controlflow.html#special-parameters).
|
||||
///
|
||||
/// This rule exempts methods decorated with [`@typing.override`][override].
|
||||
/// Changing the signature of a subclass method may cause type checkers to
|
||||
/// complain about a violation of the Liskov Substitution Principle if it
|
||||
/// means that the method now incompatibly overrides a method defined on a
|
||||
/// superclass. Explicitly decorating an overriding method with `@override`
|
||||
/// signals to Ruff that the method is intended to override a superclass
|
||||
/// method and that a type checker will enforce that it does so; Ruff
|
||||
/// therefore knows that it should not enforce rules about methods having
|
||||
/// too many arguments.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```python
|
||||
@@ -51,8 +41,6 @@ use crate::checkers::ast::Checker;
|
||||
///
|
||||
/// ## Options
|
||||
/// - `lint.pylint.max-positional-args`
|
||||
///
|
||||
/// [override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(preview_since = "v0.1.7")]
|
||||
pub(crate) struct TooManyPositionalArguments {
|
||||
|
||||
@@ -115,7 +115,6 @@ mod tests {
|
||||
#[test_case(Rule::NonOctalPermissions, Path::new("RUF064.py"))]
|
||||
#[test_case(Rule::LoggingEagerConversion, Path::new("RUF065_0.py"))]
|
||||
#[test_case(Rule::LoggingEagerConversion, Path::new("RUF065_1.py"))]
|
||||
#[test_case(Rule::PropertyWithoutReturn, Path::new("RUF066.py"))]
|
||||
#[test_case(Rule::RedirectedNOQA, Path::new("RUF101_0.py"))]
|
||||
#[test_case(Rule::RedirectedNOQA, Path::new("RUF101_1.py"))]
|
||||
#[test_case(Rule::InvalidRuleCode, Path::new("RUF102.py"))]
|
||||
|
||||
@@ -35,7 +35,6 @@ pub(crate) use non_octal_permissions::*;
|
||||
pub(crate) use none_not_at_end_of_union::*;
|
||||
pub(crate) use parenthesize_chained_operators::*;
|
||||
pub(crate) use post_init_default::*;
|
||||
pub(crate) use property_without_return::*;
|
||||
pub(crate) use pytest_raises_ambiguous_pattern::*;
|
||||
pub(crate) use quadratic_list_summation::*;
|
||||
pub(crate) use redirected_noqa::*;
|
||||
@@ -100,7 +99,6 @@ mod non_octal_permissions;
|
||||
mod none_not_at_end_of_union;
|
||||
mod parenthesize_chained_operators;
|
||||
mod post_init_default;
|
||||
mod property_without_return;
|
||||
mod pytest_raises_ambiguous_pattern;
|
||||
mod quadratic_list_summation;
|
||||
mod redirected_noqa;
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_python_ast::identifier::Identifier;
|
||||
use ruff_python_ast::visitor::{Visitor, walk_expr, walk_stmt};
|
||||
use ruff_python_ast::{Expr, Stmt, StmtFunctionDef};
|
||||
use ruff_python_semantic::analyze::{function_type, visibility};
|
||||
|
||||
use crate::checkers::ast::Checker;
|
||||
use crate::{FixAvailability, Violation};
|
||||
|
||||
/// ## What it does
|
||||
/// Detects class `@property` methods that does not have a `return` statement.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Property methods are expected to return a computed value, a missing return in a property usually indicates an implementation mistake.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// class User:
|
||||
/// @property
|
||||
/// def full_name(self):
|
||||
/// f"{self.first_name} {self.last_name}"
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// class User:
|
||||
/// @property
|
||||
/// def full_name(self):
|
||||
/// return f"{self.first_name} {self.last_name}"
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [Python documentation: The property class](https://docs.python.org/3/library/functions.html#property)
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(preview_since = "0.14.7")]
|
||||
pub(crate) struct PropertyWithoutReturn {
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl Violation for PropertyWithoutReturn {
|
||||
const FIX_AVAILABILITY: FixAvailability = FixAvailability::None;
|
||||
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
let Self { name } = self;
|
||||
format!("`{name}` is a property without a `return` statement")
|
||||
}
|
||||
}
|
||||
|
||||
/// RUF066
|
||||
pub(crate) fn property_without_return(checker: &Checker, function_def: &StmtFunctionDef) {
|
||||
let semantic = checker.semantic();
|
||||
|
||||
if checker.source_type.is_stub() || semantic.in_protocol_or_abstract_method() {
|
||||
return;
|
||||
}
|
||||
|
||||
let StmtFunctionDef {
|
||||
decorator_list,
|
||||
body,
|
||||
name,
|
||||
..
|
||||
} = function_def;
|
||||
|
||||
if !visibility::is_property(decorator_list, [], semantic)
|
||||
|| visibility::is_overload(decorator_list, semantic)
|
||||
|| function_type::is_stub(function_def, semantic)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let mut visitor = PropertyVisitor::default();
|
||||
visitor.visit_body(body);
|
||||
if visitor.found {
|
||||
return;
|
||||
}
|
||||
|
||||
checker.report_diagnostic(
|
||||
PropertyWithoutReturn {
|
||||
name: name.to_string(),
|
||||
},
|
||||
function_def.identifier(),
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct PropertyVisitor {
|
||||
found: bool,
|
||||
}
|
||||
|
||||
// NOTE: We are actually searching for the presence of
|
||||
// `yield`/`yield from`/`raise`/`return` statement/expression,
|
||||
// as having one of those indicates that there's likely no implementation mistake
|
||||
impl Visitor<'_> for PropertyVisitor {
|
||||
fn visit_expr(&mut self, expr: &Expr) {
|
||||
if self.found {
|
||||
return;
|
||||
}
|
||||
|
||||
match expr {
|
||||
Expr::Yield(_) | Expr::YieldFrom(_) => self.found = true,
|
||||
_ => walk_expr(self, expr),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_stmt(&mut self, stmt: &Stmt) {
|
||||
if self.found {
|
||||
return;
|
||||
}
|
||||
|
||||
match stmt {
|
||||
Stmt::Return(_) | Stmt::Raise(_) => self.found = true,
|
||||
Stmt::FunctionDef(_) => {
|
||||
// Do not recurse into nested functions; they're evaluated separately.
|
||||
}
|
||||
_ => walk_stmt(self, stmt),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
---
|
||||
source: crates/ruff_linter/src/rules/ruff/mod.rs
|
||||
---
|
||||
RUF066 `name` is a property without a `return` statement
|
||||
--> RUF066.py:7:9
|
||||
|
|
||||
5 | class User: # Test normal class properties
|
||||
6 | @property
|
||||
7 | def name(self): # ERROR: No return
|
||||
| ^^^^
|
||||
8 | f"{self.first_name} {self.last_name}"
|
||||
|
|
||||
|
||||
RUF066 `nested` is a property without a `return` statement
|
||||
--> RUF066.py:18:9
|
||||
|
|
||||
17 | @property
|
||||
18 | def nested(self): # ERROR: Property itself doesn't return
|
||||
| ^^^^^^
|
||||
19 | def inner():
|
||||
20 | return 0
|
||||
|
|
||||
|
||||
RUF066 `prop2` is a property without a `return` statement
|
||||
--> RUF066.py:43:9
|
||||
|
|
||||
42 | @property
|
||||
43 | def prop2(self): # ERROR: Not returning something (even when we are inside an ABC)
|
||||
| ^^^^^
|
||||
44 | 50
|
||||
|
|
||||
@@ -3,13 +3,12 @@ use std::path::Path;
|
||||
use bitflags::bitflags;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use ruff_python_ast::helpers::{from_relative_import, map_subscript};
|
||||
use ruff_python_ast::helpers::from_relative_import;
|
||||
use ruff_python_ast::name::{QualifiedName, UnqualifiedName};
|
||||
use ruff_python_ast::{self as ast, Expr, ExprContext, PySourceType, Stmt};
|
||||
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||
|
||||
use crate::Imported;
|
||||
use crate::analyze::visibility;
|
||||
use crate::binding::{
|
||||
Binding, BindingFlags, BindingId, BindingKind, Bindings, Exceptions, FromImport, Import,
|
||||
SubmoduleImport,
|
||||
@@ -2154,21 +2153,6 @@ impl<'a> SemanticModel<'a> {
|
||||
function.range() == function_def.range()
|
||||
})
|
||||
}
|
||||
|
||||
/// Return `true` if the model is in a `typing.Protocol` subclass or an abstract
|
||||
/// method.
|
||||
pub fn in_protocol_or_abstract_method(&self) -> bool {
|
||||
self.current_scopes().any(|scope| match scope.kind {
|
||||
ScopeKind::Class(class_def) => class_def
|
||||
.bases()
|
||||
.iter()
|
||||
.any(|base| self.match_typing_expr(map_subscript(base), "Protocol")),
|
||||
ScopeKind::Function(function_def) => {
|
||||
visibility::is_abstract(&function_def.decorator_list, self)
|
||||
}
|
||||
_ => false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ShadowedBinding {
|
||||
|
||||
282
crates/ty/docs/rules.md
generated
282
crates/ty/docs/rules.md
generated
@@ -39,7 +39,7 @@ def test(): -> "int":
|
||||
Default level: <a href="../rules.md#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#L132" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L127" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ Calling a non-callable object will raise a `TypeError` at runtime.
|
||||
Default level: <a href="../rules.md#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#L176" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L171" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -95,7 +95,7 @@ f(int) # error
|
||||
Default level: <a href="../rules.md#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#L202" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L197" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ a = 1
|
||||
Default level: <a href="../rules.md#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#L227" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L222" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -158,7 +158,7 @@ class C(A, B): ...
|
||||
Default level: <a href="../rules.md#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#L253" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L248" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -184,41 +184,13 @@ class B(A): ...
|
||||
|
||||
[method resolution order]: https://docs.python.org/3/glossary.html#term-method-resolution-order
|
||||
|
||||
## `cyclic-type-alias-definition`
|
||||
|
||||
<small>
|
||||
Default level: <a href="../rules.md#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#L279" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
**What it does**
|
||||
|
||||
Checks for type alias definitions that (directly or mutually) refer to themselves.
|
||||
|
||||
**Why is it bad?**
|
||||
|
||||
Although it is permitted to define a recursive type alias, it is not meaningful
|
||||
to have a type alias whose expansion can only result in itself, and is therefore not allowed.
|
||||
|
||||
**Examples**
|
||||
|
||||
```python
|
||||
type Itself = Itself
|
||||
|
||||
type A = B
|
||||
type B = A
|
||||
```
|
||||
|
||||
## `duplicate-base`
|
||||
|
||||
<small>
|
||||
Default level: <a href="../rules.md#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#L340" target="_blank">View source</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>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -245,7 +217,7 @@ class B(A, A): ...
|
||||
Default level: <a href="../rules.md#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#L361" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L334" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -357,7 +329,7 @@ def test(): -> "Literal[5]":
|
||||
Default level: <a href="../rules.md#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#L565" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L538" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -387,7 +359,7 @@ class C(A, B): ...
|
||||
Default level: <a href="../rules.md#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#L589" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L562" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -413,7 +385,7 @@ t[3] # IndexError: tuple index out of range
|
||||
Default level: <a href="../rules.md#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#L393" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L366" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -502,7 +474,7 @@ an atypical memory layout.
|
||||
Default level: <a href="../rules.md#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#L643" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L616" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -529,7 +501,7 @@ func("foo") # error: [invalid-argument-type]
|
||||
Default level: <a href="../rules.md#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#L683" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L656" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -557,7 +529,7 @@ a: int = ''
|
||||
Default level: <a href="../rules.md#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#L1919" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1815" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -591,7 +563,7 @@ C.instance_var = 3 # error: Cannot assign to instance variable
|
||||
Default level: <a href="../rules.md#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#L705" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L678" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -627,7 +599,7 @@ asyncio.run(main())
|
||||
Default level: <a href="../rules.md#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#L735" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L708" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -651,7 +623,7 @@ class A(42): ... # error: [invalid-base]
|
||||
Default level: <a href="../rules.md#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#L786" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L759" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -678,7 +650,7 @@ with 1:
|
||||
Default level: <a href="../rules.md#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#L807" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L780" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -707,7 +679,7 @@ a: str
|
||||
Default level: <a href="../rules.md#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#L830" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L803" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -745,55 +717,13 @@ except ZeroDivisionError:
|
||||
|
||||
This rule corresponds to Ruff's [`except-with-non-exception-classes` (`B030`)](https://docs.astral.sh/ruff/rules/except-with-non-exception-classes)
|
||||
|
||||
## `invalid-explicit-override`
|
||||
|
||||
<small>
|
||||
Default level: <a href="../rules.md#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#L1616" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
**What it does**
|
||||
|
||||
Checks for methods that are decorated with `@override` but do not override any method in a superclass.
|
||||
|
||||
**Why is this bad?**
|
||||
|
||||
Decorating a method with `@override` declares to the type checker that the intention is that it should
|
||||
override a method from a superclass.
|
||||
|
||||
**Example**
|
||||
|
||||
|
||||
```python
|
||||
from typing import override
|
||||
|
||||
class A:
|
||||
@override
|
||||
def foo(self): ... # Error raised here
|
||||
|
||||
class B(A):
|
||||
@override
|
||||
def ffooo(self): ... # Error raised here
|
||||
|
||||
class C:
|
||||
@override
|
||||
def __repr__(self): ... # fine: overrides `object.__repr__`
|
||||
|
||||
class D(A):
|
||||
@override
|
||||
def foo(self): ... # fine: overrides `A.foo`
|
||||
```
|
||||
|
||||
## `invalid-generic-class`
|
||||
|
||||
<small>
|
||||
Default level: <a href="../rules.md#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#L866" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L839" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -826,7 +756,7 @@ class C[U](Generic[T]): ...
|
||||
Default level: <a href="../rules.md#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#L610" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L583" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -865,7 +795,7 @@ carol = Person(name="Carol", age=25) # typo!
|
||||
Default level: <a href="../rules.md#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#L892" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L865" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -900,7 +830,7 @@ def f(t: TypeVar("U")): ...
|
||||
Default level: <a href="../rules.md#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#L989" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L962" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -934,7 +864,7 @@ class B(metaclass=f): ...
|
||||
Default level: <a href="../rules.md#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#L2047" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1943" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1012,28 +942,7 @@ classes in Python do indeed behave this way, the strongly held convention is tha
|
||||
be avoided wherever possible. As part of this check, therefore, ty enforces that `__eq__`
|
||||
and `__ne__` methods accept `object` as their second argument.
|
||||
|
||||
**Why does ty disagree with Ruff about how to write my method?**
|
||||
|
||||
|
||||
Ruff has several rules that will encourage you to rename a parameter, or change its type
|
||||
signature, if it thinks you're falling into a certain anti-pattern. For example, Ruff's
|
||||
[ARG002](https://docs.astral.sh/ruff/rules/unused-method-argument/) rule recommends that an
|
||||
unused parameter should either be removed or renamed to start with `_`. Applying either of
|
||||
these suggestions can cause ty to start reporting an `invalid-method-override` error if
|
||||
the function in question is a method on a subclass that overrides a method on a superclass,
|
||||
and the change would cause the subclass method to no longer accept all argument combinations
|
||||
that the superclass method accepts.
|
||||
|
||||
This can usually be resolved by adding [`@typing.override`][override] to your method
|
||||
definition. Ruff knows that a method decorated with `@typing.override` is intended to
|
||||
override a method by the same name on a superclass, and avoids reporting rules like ARG002
|
||||
for such methods; it knows that the changes recommended by ARG002 would violate the Liskov
|
||||
Substitution Principle.
|
||||
|
||||
Correct use of `@override` is enforced by ty's `invalid-explicit-override` rule.
|
||||
|
||||
[Liskov Substitution Principle]: https://en.wikipedia.org/wiki/Liskov_substitution_principle
|
||||
[override]: https://docs.python.org/3/library/typing.html#typing.override
|
||||
|
||||
## `invalid-named-tuple`
|
||||
|
||||
@@ -1041,7 +950,7 @@ Correct use of `@override` is enforced by ty's `invalid-explicit-override` rule.
|
||||
Default level: <a href="../rules.md#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#L539" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L512" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1073,7 +982,7 @@ TypeError: can only inherit from a NamedTuple type and Generic
|
||||
Default level: <a href="../rules.md#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#L965" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L938" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1103,7 +1012,7 @@ Baz = NewType("Baz", int | str) # error: invalid base for `typing.NewType`
|
||||
Default level: <a href="../rules.md#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#L1016" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L989" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1153,7 +1062,7 @@ def foo(x: int) -> int: ...
|
||||
Default level: <a href="../rules.md#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#L1115" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1088" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1179,7 +1088,7 @@ def f(a: int = ''): ...
|
||||
Default level: <a href="../rules.md#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#L920" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L893" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1210,7 +1119,7 @@ P2 = ParamSpec("S2") # error: ParamSpec name must match the variable it's assig
|
||||
Default level: <a href="../rules.md#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#L475" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L448" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1244,7 +1153,7 @@ TypeError: Protocols can only inherit from other protocols, got <class 'int'>
|
||||
Default level: <a href="../rules.md#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#L1135" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1108" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1293,7 +1202,7 @@ def g():
|
||||
Default level: <a href="../rules.md#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#L664" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L637" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1318,7 +1227,7 @@ def func() -> int:
|
||||
Default level: <a href="../rules.md#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#L1178" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1151" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1376,7 +1285,7 @@ TODO #14889
|
||||
Default level: <a href="../rules.md#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#L944" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L917" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1397,60 +1306,13 @@ IntOrStr = TypeAliasType("IntOrStr", int | str) # okay
|
||||
NewAlias = TypeAliasType(get_name(), int) # error: TypeAliasType name must be a string literal
|
||||
```
|
||||
|
||||
## `invalid-type-arguments`
|
||||
|
||||
<small>
|
||||
Default level: <a href="../rules.md#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#L1410" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
**What it does**
|
||||
|
||||
Checks for invalid type arguments in explicit type specialization.
|
||||
|
||||
**Why is this bad?**
|
||||
|
||||
Providing the wrong number of type arguments or type arguments that don't
|
||||
satisfy the type variable's bounds or constraints will lead to incorrect
|
||||
type inference and may indicate a misunderstanding of the generic type's
|
||||
interface.
|
||||
|
||||
**Examples**
|
||||
|
||||
|
||||
Using legacy type variables:
|
||||
```python
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
T1 = TypeVar('T1', int, str)
|
||||
T2 = TypeVar('T2', bound=int)
|
||||
|
||||
class Foo1(Generic[T1]): ...
|
||||
class Foo2(Generic[T2]): ...
|
||||
|
||||
Foo1[bytes] # error: bytes does not satisfy T1's constraints
|
||||
Foo2[str] # error: str does not satisfy T2's bound
|
||||
```
|
||||
|
||||
Using PEP 695 type variables:
|
||||
```python
|
||||
class Foo[T]: ...
|
||||
class Bar[T, U]: ...
|
||||
|
||||
Foo[int, str] # error: too many arguments
|
||||
Bar[int] # error: too few arguments
|
||||
```
|
||||
|
||||
## `invalid-type-checking-constant`
|
||||
|
||||
<small>
|
||||
Default level: <a href="../rules.md#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#L1217" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1190" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1480,7 +1342,7 @@ TYPE_CHECKING = ''
|
||||
Default level: <a href="../rules.md#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#L1241" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1214" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1510,7 +1372,7 @@ b: Annotated[int] # `Annotated` expects at least two arguments
|
||||
Default level: <a href="../rules.md#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#L1293" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1266" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1544,7 +1406,7 @@ f(10) # Error
|
||||
Default level: <a href="../rules.md#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#L1265" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1238" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1578,7 +1440,7 @@ class C:
|
||||
Default level: <a href="../rules.md#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#L1321" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1294" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1613,7 +1475,7 @@ T = TypeVar('T', bound=str) # valid bound TypeVar
|
||||
Default level: <a href="../rules.md#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#L1350" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1323" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1638,7 +1500,7 @@ func() # TypeError: func() missing 1 required positional argument: 'x'
|
||||
Default level: <a href="../rules.md#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#L2020" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1916" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1671,7 +1533,7 @@ alice["age"] # KeyError
|
||||
Default level: <a href="../rules.md#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#L1369" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1342" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1700,7 +1562,7 @@ func("string") # error: [no-matching-overload]
|
||||
Default level: <a href="../rules.md#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%20non-subscriptable" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1392" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1365" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1724,7 +1586,7 @@ Subscripting an object that does not support it will raise a `TypeError` at runt
|
||||
Default level: <a href="../rules.md#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#L1451" 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>
|
||||
|
||||
|
||||
@@ -1750,7 +1612,7 @@ for i in 34: # TypeError: 'int' object is not iterable
|
||||
Default level: <a href="../rules.md#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#L1502" target="_blank">View source</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>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1777,7 +1639,7 @@ f(1, x=2) # Error raised here
|
||||
Default level: <a href="../rules.md#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#L1773" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1669" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1835,7 +1697,7 @@ def test(): -> "int":
|
||||
Default level: <a href="../rules.md#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#L1895" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1791" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1865,7 +1727,7 @@ static_assert(int(2.0 * 3.0) == 6) # error: does not have a statically known tr
|
||||
Default level: <a href="../rules.md#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#L1593" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1525" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1894,7 +1756,7 @@ class B(A): ... # Error raised here
|
||||
Default level: <a href="../rules.md#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#L1674" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1570" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1921,7 +1783,7 @@ f("foo") # Error raised here
|
||||
Default level: <a href="../rules.md#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#L1652" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1548" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1949,7 +1811,7 @@ def _(x: int):
|
||||
Default level: <a href="../rules.md#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#L1695" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1591" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -1995,7 +1857,7 @@ class A:
|
||||
Default level: <a href="../rules.md#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#L1752" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1648" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2022,7 +1884,7 @@ f(x=1, y=2) # Error raised here
|
||||
Default level: <a href="../rules.md#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#L1794" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1690" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2050,7 +1912,7 @@ A().foo # AttributeError: 'A' object has no attribute 'foo'
|
||||
Default level: <a href="../rules.md#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#L1816" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1712" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2075,7 +1937,7 @@ import foo # ModuleNotFoundError: No module named 'foo'
|
||||
Default level: <a href="../rules.md#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#L1835" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1731" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2100,7 +1962,7 @@ print(x) # NameError: name 'x' is not defined
|
||||
Default level: <a href="../rules.md#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#L1471" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1403" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2137,7 +1999,7 @@ b1 < b2 < b1 # exception raised here
|
||||
Default level: <a href="../rules.md#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#L1854" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1750" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2165,7 +2027,7 @@ A() + A() # TypeError: unsupported operand type(s) for +: 'A' and 'A'
|
||||
Default level: <a href="../rules.md#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#L1876" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1772" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2190,7 +2052,7 @@ l[1:10:0] # ValueError: slice step cannot be zero
|
||||
Default level: <a href="../rules.md#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#L504" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L477" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2231,7 +2093,7 @@ class SubProto(BaseProto, Protocol):
|
||||
Default level: <a href="../rules.md#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#L319" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L292" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2258,7 +2120,7 @@ old_func() # emits [deprecated] diagnostic
|
||||
Default level: <a href="../rules.md#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%20ignore-comment-unknown-rule" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Fsuppression.rs#L47" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Fsuppression.rs#L40" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2289,7 +2151,7 @@ a = 20 / 0 # ty: ignore[division-by-zero]
|
||||
Default level: <a href="../rules.md#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%20invalid-ignore-comment" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Fsuppression.rs#L72" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Fsuppression.rs#L65" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2319,7 +2181,7 @@ a = 20 / 0 # type: ignore
|
||||
Default level: <a href="../rules.md#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#L1523" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1455" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2347,7 +2209,7 @@ A.c # AttributeError: type object 'A' has no attribute 'c'
|
||||
Default level: <a href="../rules.md#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#L150" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L145" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2379,7 +2241,7 @@ A()[0] # TypeError: 'A' object is not subscriptable
|
||||
Default level: <a href="../rules.md#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-import" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1545" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1477" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2411,7 +2273,7 @@ from module import a # ImportError: cannot import name 'a' from 'module'
|
||||
Default level: <a href="../rules.md#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#L1947" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1843" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2438,7 +2300,7 @@ cast(int, f()) # Redundant
|
||||
Default level: <a href="../rules.md#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#L1734" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1630" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2462,7 +2324,7 @@ reveal_type(1) # NameError: name 'reveal_type' is not defined
|
||||
Default level: <a href="../rules.md#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#L1968" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1864" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2520,7 +2382,7 @@ def g():
|
||||
Default level: <a href="../rules.md#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#L753" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L726" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2559,7 +2421,7 @@ class D(C): ... # error: [unsupported-base]
|
||||
Default level: <a href="../rules.md#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#L1059" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1032" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2622,7 +2484,7 @@ def foo(x: int | str) -> int | str:
|
||||
Default level: <a href="../rules.md#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#L301" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L274" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2646,7 +2508,7 @@ Dividing by zero raises a `ZeroDivisionError` at runtime.
|
||||
Default level: <a href="../rules.md#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#L1571" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L1503" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
@@ -2674,7 +2536,7 @@ print(x) # NameError: name 'x' is not defined
|
||||
Default level: <a href="../rules.md#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%20unused-ignore-comment" target="_blank">Related issues</a> ·
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Fsuppression.rs#L22" target="_blank">View source</a>
|
||||
<a href="https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Fsuppression.rs#L15" target="_blank">View source</a>
|
||||
</small>
|
||||
|
||||
|
||||
|
||||
@@ -313,8 +313,7 @@ impl MainLoop {
|
||||
let terminal_settings = db.project().settings(db).terminal();
|
||||
let display_config = DisplayDiagnosticConfig::default()
|
||||
.format(terminal_settings.output_format.into())
|
||||
.color(colored::control::SHOULD_COLORIZE.should_colorize())
|
||||
.show_fix_diff(true);
|
||||
.color(colored::control::SHOULD_COLORIZE.should_colorize());
|
||||
|
||||
if check_revision == revision {
|
||||
if db.project().files(db).is_empty() {
|
||||
|
||||
@@ -143,7 +143,7 @@ ABCDEFGHIJKLMNOP = 'https://api.example.com'
|
||||
|
||||
impl CursorTest {
|
||||
fn all_symbols(&self, query: &str) -> String {
|
||||
let symbols = all_symbols(&self.db, &QueryPattern::fuzzy(query));
|
||||
let symbols = all_symbols(&self.db, &QueryPattern::new(query));
|
||||
|
||||
if symbols.is_empty() {
|
||||
return "No symbols found".to_string();
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
use crate::{completion, find_node::covering_node};
|
||||
use ruff_db::{files::File, parsed::parsed_module};
|
||||
use ruff_diagnostics::Edit;
|
||||
use ruff_text_size::TextRange;
|
||||
use ty_project::Db;
|
||||
use ty_python_semantic::types::UNRESOLVED_REFERENCE;
|
||||
|
||||
/// A `QuickFix` Code Action
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct QuickFix {
|
||||
pub title: String,
|
||||
pub edits: Vec<Edit>,
|
||||
pub preferred: bool,
|
||||
}
|
||||
|
||||
pub fn code_actions(
|
||||
db: &dyn Db,
|
||||
file: File,
|
||||
diagnostic_range: TextRange,
|
||||
diagnostic_id: &str,
|
||||
) -> Option<Vec<QuickFix>> {
|
||||
let registry = db.lint_registry();
|
||||
let Ok(lint_id) = registry.get(diagnostic_id) else {
|
||||
return None;
|
||||
};
|
||||
if lint_id.name() == UNRESOLVED_REFERENCE.name() {
|
||||
let parsed = parsed_module(db, file).load(db);
|
||||
let node = covering_node(parsed.syntax().into(), diagnostic_range).node();
|
||||
let symbol = &node.expr_name()?.id;
|
||||
|
||||
let fixes = completion::missing_imports(db, file, &parsed, symbol, node)
|
||||
.into_iter()
|
||||
.map(|import| QuickFix {
|
||||
title: import.label,
|
||||
edits: vec![import.edit],
|
||||
preferred: true,
|
||||
})
|
||||
.collect();
|
||||
Some(fixes)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,8 @@ use ruff_db::files::File;
|
||||
use ruff_db::parsed::{ParsedModuleRef, parsed_module};
|
||||
use ruff_db::source::source_text;
|
||||
use ruff_diagnostics::Edit;
|
||||
use ruff_python_ast as ast;
|
||||
use ruff_python_ast::name::Name;
|
||||
use ruff_python_ast::{self as ast, AnyNodeRef};
|
||||
use ruff_python_codegen::Stylist;
|
||||
use ruff_python_parser::{Token, TokenAt, TokenKind, Tokens};
|
||||
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
|
||||
@@ -37,9 +37,9 @@ impl<'db> Completions<'db> {
|
||||
/// the user has typed as part of the next symbol they are writing.
|
||||
/// This collection will treat it as a query when present, and only
|
||||
/// add completions that match it.
|
||||
fn fuzzy(db: &'db dyn Db, typed: Option<&str>) -> Completions<'db> {
|
||||
fn new(db: &'db dyn Db, typed: Option<&str>) -> Completions<'db> {
|
||||
let query = typed
|
||||
.map(QueryPattern::fuzzy)
|
||||
.map(QueryPattern::new)
|
||||
.unwrap_or_else(QueryPattern::matches_all_symbols);
|
||||
Completions {
|
||||
db,
|
||||
@@ -48,15 +48,6 @@ impl<'db> Completions<'db> {
|
||||
}
|
||||
}
|
||||
|
||||
fn exactly(db: &'db dyn Db, symbol: &str) -> Completions<'db> {
|
||||
let query = QueryPattern::exactly(symbol);
|
||||
Completions {
|
||||
db,
|
||||
items: vec![],
|
||||
query,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert this collection into a simple
|
||||
/// sequence of completions.
|
||||
fn into_completions(mut self) -> Vec<Completion<'db>> {
|
||||
@@ -66,21 +57,6 @@ impl<'db> Completions<'db> {
|
||||
self.items
|
||||
}
|
||||
|
||||
fn into_imports(mut self) -> Vec<ImportEdit> {
|
||||
self.items.sort_by(compare_suggestions);
|
||||
self.items
|
||||
.dedup_by(|c1, c2| (&c1.name, c1.module_name) == (&c2.name, c2.module_name));
|
||||
self.items
|
||||
.into_iter()
|
||||
.filter_map(|item| {
|
||||
Some(ImportEdit {
|
||||
label: format!("import {}.{}", item.module_name?, item.name),
|
||||
edit: item.import?,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Attempts to adds the given completion to this collection.
|
||||
///
|
||||
/// When added, `true` is returned.
|
||||
@@ -393,7 +369,7 @@ pub fn completion<'db>(
|
||||
return vec![];
|
||||
}
|
||||
|
||||
let mut completions = Completions::fuzzy(db, typed.as_deref());
|
||||
let mut completions = Completions::new(db, typed.as_deref());
|
||||
|
||||
if let Some(import) = ImportStatement::detect(db, file, &parsed, tokens, typed.as_deref()) {
|
||||
import.add_completions(db, file, &mut completions);
|
||||
@@ -441,25 +417,6 @@ pub fn completion<'db>(
|
||||
completions.into_completions()
|
||||
}
|
||||
|
||||
pub(crate) struct ImportEdit {
|
||||
pub label: String,
|
||||
pub edit: Edit,
|
||||
}
|
||||
|
||||
pub(crate) fn missing_imports(
|
||||
db: &dyn Db,
|
||||
file: File,
|
||||
parsed: &ParsedModuleRef,
|
||||
symbol: &str,
|
||||
node: AnyNodeRef,
|
||||
) -> Vec<ImportEdit> {
|
||||
let mut completions = Completions::exactly(db, symbol);
|
||||
let scoped = ScopedTarget { node };
|
||||
add_unimported_completions(db, file, parsed, scoped, &mut completions);
|
||||
|
||||
completions.into_imports()
|
||||
}
|
||||
|
||||
/// Adds completions derived from keywords.
|
||||
///
|
||||
/// This should generally only be used when offering "scoped" completions.
|
||||
@@ -1358,8 +1315,7 @@ fn find_typed_text(
|
||||
if last.end() < offset || last.range().is_empty() {
|
||||
return None;
|
||||
}
|
||||
let range = TextRange::new(last.start(), offset);
|
||||
Some(source[range].to_string())
|
||||
Some(source[last.range()].to_string())
|
||||
}
|
||||
|
||||
/// Whether the last token is in a place where we should not provide completions.
|
||||
@@ -1450,24 +1406,6 @@ fn is_in_variable_binding(parsed: &ParsedModuleRef, offset: TextSize, typed: Opt
|
||||
type_param.name.range.contains_range(range)
|
||||
}
|
||||
ast::AnyNodeRef::StmtFor(stmt_for) => stmt_for.target.range().contains_range(range),
|
||||
// The AST does not produce `ast::AnyNodeRef::Parameter` nodes for keywords
|
||||
// or otherwise invalid syntax. Rather they are captured in a
|
||||
// `ast::AnyNodeRef::Parameters` node as "empty space". To ensure
|
||||
// we still suppress suggestions even when the syntax is technically
|
||||
// invalid we extract the token under the cursor and check if it makes
|
||||
// up that "empty space" inside the Parameters Node. If it does, we know
|
||||
// that we are still binding variables, just that the current state is
|
||||
// syntatically invalid. Hence we suppress autocomplete suggestons
|
||||
// also in those cases.
|
||||
ast::AnyNodeRef::Parameters(params) => {
|
||||
if !params.range.contains_range(range) {
|
||||
return false;
|
||||
}
|
||||
params
|
||||
.iter()
|
||||
.map(|param| param.range())
|
||||
.all(|r| !r.contains_range(range))
|
||||
}
|
||||
_ => false,
|
||||
})
|
||||
}
|
||||
@@ -1695,21 +1633,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inside_token() {
|
||||
let test = completion_test_builder(
|
||||
"\
|
||||
foo_bar_baz = 1
|
||||
x = foo<CURSOR>bad
|
||||
",
|
||||
);
|
||||
|
||||
assert_snapshot!(
|
||||
test.skip_builtins().build().snapshot(),
|
||||
@"foo_bar_baz",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn type_keyword_dedup() {
|
||||
let test = completion_test_builder(
|
||||
@@ -5424,45 +5347,6 @@ def foo(p<CURSOR>
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_completions_in_function_param_keyword() {
|
||||
let builder = completion_test_builder(
|
||||
"\
|
||||
def foo(in<CURSOR>
|
||||
",
|
||||
);
|
||||
assert_snapshot!(
|
||||
builder.build().snapshot(),
|
||||
@"<No completions found>",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_completions_in_function_param_multi_keyword() {
|
||||
let builder = completion_test_builder(
|
||||
"\
|
||||
def foo(param, in<CURSOR>
|
||||
",
|
||||
);
|
||||
assert_snapshot!(
|
||||
builder.build().snapshot(),
|
||||
@"<No completions found>",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_completions_in_function_param_multi_keyword_middle() {
|
||||
let builder = completion_test_builder(
|
||||
"\
|
||||
def foo(param, in<CURSOR>, param_two
|
||||
",
|
||||
);
|
||||
assert_snapshot!(
|
||||
builder.build().snapshot(),
|
||||
@"<No completions found>",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_completions_in_function_type_param() {
|
||||
let builder = completion_test_builder(
|
||||
|
||||
@@ -3,7 +3,6 @@ use crate::references::{ReferencesMode, references};
|
||||
use crate::{Db, ReferenceTarget};
|
||||
use ruff_db::files::File;
|
||||
use ruff_text_size::TextSize;
|
||||
use ty_python_semantic::SemanticModel;
|
||||
|
||||
/// Find all document highlights for a symbol at the given position.
|
||||
/// Document highlights are limited to the current file only.
|
||||
@@ -14,10 +13,9 @@ pub fn document_highlights(
|
||||
) -> Option<Vec<ReferenceTarget>> {
|
||||
let parsed = ruff_db::parsed::parsed_module(db, file);
|
||||
let module = parsed.load(db);
|
||||
let model = SemanticModel::new(db, file);
|
||||
|
||||
// Get the definitions for the symbol at the cursor position
|
||||
let goto_target = find_goto_target(&model, &module, offset)?;
|
||||
let goto_target = find_goto_target(&module, offset)?;
|
||||
|
||||
// Use DocumentHighlights mode which limits search to current file only
|
||||
references(db, file, &goto_target, ReferencesMode::DocumentHighlights)
|
||||
|
||||
@@ -190,24 +190,6 @@ pub(crate) enum GotoTarget<'a> {
|
||||
/// The call of the callable
|
||||
call: &'a ast::ExprCall,
|
||||
},
|
||||
|
||||
/// Go to on a sub-expression of a string annotation's sub-AST
|
||||
///
|
||||
/// ```py
|
||||
/// x: "int | None"
|
||||
/// ^^^^
|
||||
/// ```
|
||||
///
|
||||
/// This is equivalent to `GotoTarget::Expression` but the expression
|
||||
/// isn't actually in the AST.
|
||||
StringAnnotationSubexpr {
|
||||
/// The string literal that is a string annotation.
|
||||
string_expr: &'a ast::ExprStringLiteral,
|
||||
/// The range to query in the sub-AST for the sub-expression.
|
||||
subrange: TextRange,
|
||||
/// If the expression is a Name of some kind this is the name (just a cached result).
|
||||
name: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
/// The resolved definitions for a `GotoTarget`
|
||||
@@ -245,7 +227,7 @@ impl<'db> DefinitionsOrTargets<'db> {
|
||||
/// In this case it basically returns exactly what was found.
|
||||
pub(crate) fn declaration_targets(
|
||||
self,
|
||||
db: &'db dyn ty_python_semantic::Db,
|
||||
db: &'db dyn crate::Db,
|
||||
) -> Option<crate::NavigationTargets> {
|
||||
match self {
|
||||
DefinitionsOrTargets::Definitions(definitions) => {
|
||||
@@ -261,7 +243,7 @@ impl<'db> DefinitionsOrTargets<'db> {
|
||||
/// if the definition we have is found in a stub file.
|
||||
pub(crate) fn definition_targets(
|
||||
self,
|
||||
db: &'db dyn ty_python_semantic::Db,
|
||||
db: &'db dyn crate::Db,
|
||||
) -> Option<crate::NavigationTargets> {
|
||||
match self {
|
||||
DefinitionsOrTargets::Definitions(definitions) => {
|
||||
@@ -331,32 +313,6 @@ impl GotoTarget<'_> {
|
||||
let module = import_name(module_name, *component_index);
|
||||
model.resolve_module_type(Some(module), *level)?
|
||||
}
|
||||
GotoTarget::StringAnnotationSubexpr {
|
||||
string_expr,
|
||||
subrange,
|
||||
..
|
||||
} => {
|
||||
let (subast, _submodel) = model.enter_string_annotation(string_expr)?;
|
||||
let submod = subast.syntax();
|
||||
let subnode = covering_node(submod.into(), *subrange).node();
|
||||
|
||||
// The type checker knows the type of the full annotation but nothing else
|
||||
if AnyNodeRef::from(&*submod.body) == subnode {
|
||||
string_expr.inferred_type(model)
|
||||
} else {
|
||||
// TODO: force the typechecker to tell us its secrets
|
||||
// (it computes but then immediately discards these types)
|
||||
return None;
|
||||
}
|
||||
}
|
||||
GotoTarget::BinOp { expression, .. } => {
|
||||
let (_, ty) = ty_python_semantic::definitions_for_bin_op(model, expression)?;
|
||||
ty
|
||||
}
|
||||
GotoTarget::UnaryOp { expression, .. } => {
|
||||
let (_, ty) = ty_python_semantic::definitions_for_unary_op(model, expression)?;
|
||||
ty
|
||||
}
|
||||
// TODO: Support identifier targets
|
||||
GotoTarget::PatternMatchRest(_)
|
||||
| GotoTarget::PatternKeywordArgument(_)
|
||||
@@ -366,6 +322,16 @@ impl GotoTarget<'_> {
|
||||
| GotoTarget::TypeParamTypeVarTupleName(_)
|
||||
| GotoTarget::NonLocal { .. }
|
||||
| GotoTarget::Globals { .. } => return None,
|
||||
GotoTarget::BinOp { expression, .. } => {
|
||||
let (_, ty) =
|
||||
ty_python_semantic::definitions_for_bin_op(model.db(), model, expression)?;
|
||||
ty
|
||||
}
|
||||
GotoTarget::UnaryOp { expression, .. } => {
|
||||
let (_, ty) =
|
||||
ty_python_semantic::definitions_for_unary_op(model.db(), model, expression)?;
|
||||
ty
|
||||
}
|
||||
};
|
||||
|
||||
Some(ty)
|
||||
@@ -377,7 +343,7 @@ impl GotoTarget<'_> {
|
||||
model: &SemanticModel,
|
||||
) -> Option<String> {
|
||||
if let GotoTarget::Call { call, .. } = self {
|
||||
call_type_simplified_by_overloads(model, call)
|
||||
call_type_simplified_by_overloads(model.db(), model, call)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -401,9 +367,14 @@ impl GotoTarget<'_> {
|
||||
alias_resolution: ImportAliasResolution,
|
||||
) -> Option<DefinitionsOrTargets<'db>> {
|
||||
use crate::NavigationTarget;
|
||||
let db = model.db();
|
||||
let file = model.file();
|
||||
|
||||
match self {
|
||||
GotoTarget::Expression(expression) => definitions_for_expression(model, *expression)
|
||||
.map(DefinitionsOrTargets::Definitions),
|
||||
GotoTarget::Expression(expression) => {
|
||||
definitions_for_expression(model, expression).map(DefinitionsOrTargets::Definitions)
|
||||
}
|
||||
|
||||
// For already-defined symbols, they are their own definitions
|
||||
GotoTarget::FunctionDef(function) => Some(DefinitionsOrTargets::Definitions(vec![
|
||||
ResolvedDefinition::Definition(function.definition(model)),
|
||||
@@ -424,7 +395,8 @@ impl GotoTarget<'_> {
|
||||
let symbol_name = alias.name.as_str();
|
||||
Some(DefinitionsOrTargets::Definitions(
|
||||
definitions_for_imported_symbol(
|
||||
model,
|
||||
db,
|
||||
file,
|
||||
import_from,
|
||||
symbol_name,
|
||||
alias_resolution,
|
||||
@@ -451,7 +423,7 @@ impl GotoTarget<'_> {
|
||||
let alias_range = alias.asname.as_ref().unwrap().range;
|
||||
Some(DefinitionsOrTargets::Targets(
|
||||
crate::NavigationTargets::single(NavigationTarget {
|
||||
file: model.file(),
|
||||
file,
|
||||
focus_range: alias_range,
|
||||
full_range: alias.range(),
|
||||
}),
|
||||
@@ -464,7 +436,7 @@ impl GotoTarget<'_> {
|
||||
keyword,
|
||||
call_expression,
|
||||
} => Some(DefinitionsOrTargets::Definitions(
|
||||
definitions_for_keyword_argument(model, keyword, call_expression),
|
||||
definitions_for_keyword_argument(db, file, keyword, call_expression),
|
||||
)),
|
||||
|
||||
// For exception variables, they are their own definitions (like parameters)
|
||||
@@ -479,10 +451,7 @@ impl GotoTarget<'_> {
|
||||
if let Some(rest_name) = &pattern_mapping.rest {
|
||||
let range = rest_name.range;
|
||||
Some(DefinitionsOrTargets::Targets(
|
||||
crate::NavigationTargets::single(NavigationTarget::new(
|
||||
model.file(),
|
||||
range,
|
||||
)),
|
||||
crate::NavigationTargets::single(NavigationTarget::new(file, range)),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
@@ -494,10 +463,7 @@ impl GotoTarget<'_> {
|
||||
if let Some(name) = &pattern_as.name {
|
||||
let range = name.range;
|
||||
Some(DefinitionsOrTargets::Targets(
|
||||
crate::NavigationTargets::single(NavigationTarget::new(
|
||||
model.file(),
|
||||
range,
|
||||
)),
|
||||
crate::NavigationTargets::single(NavigationTarget::new(file, range)),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
@@ -510,7 +476,7 @@ impl GotoTarget<'_> {
|
||||
GotoTarget::Call { callable, call } => {
|
||||
let mut definitions = definitions_for_callable(model, call);
|
||||
let expr_definitions =
|
||||
definitions_for_expression(model, *callable).unwrap_or_default();
|
||||
definitions_for_expression(model, callable).unwrap_or_default();
|
||||
definitions.extend(expr_definitions);
|
||||
|
||||
if definitions.is_empty() {
|
||||
@@ -522,45 +488,19 @@ impl GotoTarget<'_> {
|
||||
|
||||
GotoTarget::BinOp { expression, .. } => {
|
||||
let (definitions, _) =
|
||||
ty_python_semantic::definitions_for_bin_op(model, expression)?;
|
||||
ty_python_semantic::definitions_for_bin_op(db, model, expression)?;
|
||||
|
||||
Some(DefinitionsOrTargets::Definitions(definitions))
|
||||
}
|
||||
|
||||
GotoTarget::UnaryOp { expression, .. } => {
|
||||
let (definitions, _) =
|
||||
ty_python_semantic::definitions_for_unary_op(model, expression)?;
|
||||
ty_python_semantic::definitions_for_unary_op(db, model, expression)?;
|
||||
|
||||
Some(DefinitionsOrTargets::Definitions(definitions))
|
||||
}
|
||||
|
||||
// String annotations sub-expressions require us to recurse into the sub-AST
|
||||
GotoTarget::StringAnnotationSubexpr {
|
||||
string_expr,
|
||||
subrange,
|
||||
..
|
||||
} => {
|
||||
let (subast, submodel) = model.enter_string_annotation(string_expr)?;
|
||||
let subexpr = covering_node(subast.syntax().into(), *subrange)
|
||||
.node()
|
||||
.as_expr_ref()?;
|
||||
definitions_for_expression(&submodel, subexpr)
|
||||
.map(DefinitionsOrTargets::Definitions)
|
||||
}
|
||||
GotoTarget::NonLocal { identifier } | GotoTarget::Globals { identifier } => {
|
||||
Some(DefinitionsOrTargets::Definitions(definitions_for_name(
|
||||
model,
|
||||
identifier.as_str(),
|
||||
AnyNodeRef::Identifier(identifier),
|
||||
)))
|
||||
}
|
||||
|
||||
// TODO: implement these
|
||||
GotoTarget::PatternKeywordArgument(..)
|
||||
| GotoTarget::PatternMatchStarName(..)
|
||||
| GotoTarget::TypeParamTypeVarName(..)
|
||||
| GotoTarget::TypeParamParamSpecName(..)
|
||||
| GotoTarget::TypeParamTypeVarTupleName(..) => None,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -579,7 +519,6 @@ impl GotoTarget<'_> {
|
||||
ast::ExprRef::Attribute(attr) => Some(Cow::Borrowed(attr.attr.as_str())),
|
||||
_ => None,
|
||||
},
|
||||
GotoTarget::StringAnnotationSubexpr { name, .. } => name.as_deref().map(Cow::Borrowed),
|
||||
GotoTarget::FunctionDef(function) => Some(Cow::Borrowed(function.name.as_str())),
|
||||
GotoTarget::ClassDef(class) => Some(Cow::Borrowed(class.name.as_str())),
|
||||
GotoTarget::Parameter(parameter) => Some(Cow::Borrowed(parameter.name.as_str())),
|
||||
@@ -640,7 +579,6 @@ impl GotoTarget<'_> {
|
||||
|
||||
/// Creates a `GotoTarget` from a `CoveringNode` and an offset within the node
|
||||
pub(crate) fn from_covering_node<'a>(
|
||||
model: &SemanticModel,
|
||||
covering_node: &crate::find_node::CoveringNode<'a>,
|
||||
offset: TextSize,
|
||||
tokens: &Tokens,
|
||||
@@ -840,31 +778,6 @@ impl GotoTarget<'_> {
|
||||
Some(GotoTarget::Expression(unary.into()))
|
||||
}
|
||||
|
||||
node @ AnyNodeRef::ExprStringLiteral(string_expr) => {
|
||||
// Check if we've clicked on a sub-GotoTarget inside a string annotation's sub-AST
|
||||
if let Some((subast, submodel)) = model.enter_string_annotation(string_expr)
|
||||
&& let Some(GotoTarget::Expression(subexpr)) = find_goto_target_impl(
|
||||
&submodel,
|
||||
subast.tokens(),
|
||||
subast.syntax().into(),
|
||||
offset,
|
||||
)
|
||||
{
|
||||
let name = match subexpr {
|
||||
ast::ExprRef::Name(name) => Some(name.id.to_string()),
|
||||
ast::ExprRef::Attribute(attr) => Some(attr.attr.to_string()),
|
||||
_ => None,
|
||||
};
|
||||
Some(GotoTarget::StringAnnotationSubexpr {
|
||||
string_expr,
|
||||
subrange: subexpr.range(),
|
||||
name,
|
||||
})
|
||||
} else {
|
||||
node.as_expr_ref().map(GotoTarget::Expression)
|
||||
}
|
||||
}
|
||||
|
||||
node => {
|
||||
// Check if this is seemingly a callable being invoked (the `x` in `x(...)`)
|
||||
let parent = covering_node.parent();
|
||||
@@ -900,7 +813,6 @@ impl Ranged for GotoTarget<'_> {
|
||||
GotoTarget::ImportModuleComponent {
|
||||
component_range, ..
|
||||
} => *component_range,
|
||||
GotoTarget::StringAnnotationSubexpr { subrange, .. } => *subrange,
|
||||
GotoTarget::ImportModuleAlias { alias } => alias.asname.as_ref().unwrap().range,
|
||||
GotoTarget::ExceptVariable(except) => except.name.as_ref().unwrap().range,
|
||||
GotoTarget::KeywordArgument { keyword, .. } => keyword.arg.as_ref().unwrap().range,
|
||||
@@ -920,9 +832,9 @@ impl Ranged for GotoTarget<'_> {
|
||||
}
|
||||
|
||||
/// Converts a collection of `ResolvedDefinition` items into `NavigationTarget` items.
|
||||
fn convert_resolved_definitions_to_targets<'db>(
|
||||
db: &'db dyn ty_python_semantic::Db,
|
||||
definitions: Vec<ty_python_semantic::ResolvedDefinition<'db>>,
|
||||
fn convert_resolved_definitions_to_targets(
|
||||
db: &dyn crate::Db,
|
||||
definitions: Vec<ty_python_semantic::ResolvedDefinition<'_>>,
|
||||
) -> Vec<crate::NavigationTarget> {
|
||||
definitions
|
||||
.into_iter()
|
||||
@@ -957,16 +869,14 @@ fn convert_resolved_definitions_to_targets<'db>(
|
||||
/// Shared helper to get definitions for an expr (that is presumably a name/attr)
|
||||
fn definitions_for_expression<'db>(
|
||||
model: &SemanticModel<'db>,
|
||||
expression: ruff_python_ast::ExprRef<'_>,
|
||||
expression: &ruff_python_ast::ExprRef<'_>,
|
||||
) -> Option<Vec<ResolvedDefinition<'db>>> {
|
||||
match expression {
|
||||
ast::ExprRef::Name(name) => Some(definitions_for_name(
|
||||
model,
|
||||
name.id.as_str(),
|
||||
expression.into(),
|
||||
)),
|
||||
ast::ExprRef::Name(name) => Some(definitions_for_name(model.db(), model.file(), name)),
|
||||
ast::ExprRef::Attribute(attribute) => Some(ty_python_semantic::definitions_for_attribute(
|
||||
model, attribute,
|
||||
model.db(),
|
||||
model.file(),
|
||||
attribute,
|
||||
)),
|
||||
_ => None,
|
||||
}
|
||||
@@ -977,7 +887,7 @@ fn definitions_for_callable<'db>(
|
||||
call: &ast::ExprCall,
|
||||
) -> Vec<ResolvedDefinition<'db>> {
|
||||
// Attempt to refine to a specific call
|
||||
let signature_info = call_signature_details(model, call);
|
||||
let signature_info = call_signature_details(model.db(), model, call);
|
||||
signature_info
|
||||
.into_iter()
|
||||
.filter_map(|signature| signature.definition.map(ResolvedDefinition::Definition))
|
||||
@@ -986,7 +896,7 @@ fn definitions_for_callable<'db>(
|
||||
|
||||
/// Shared helper to map and convert resolved definitions into navigation targets.
|
||||
fn definitions_to_navigation_targets<'db>(
|
||||
db: &dyn ty_python_semantic::Db,
|
||||
db: &dyn crate::Db,
|
||||
stub_mapper: Option<&StubMapper<'db>>,
|
||||
mut definitions: Vec<ty_python_semantic::ResolvedDefinition<'db>>,
|
||||
) -> Option<crate::NavigationTargets> {
|
||||
@@ -1001,21 +911,12 @@ fn definitions_to_navigation_targets<'db>(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn find_goto_target<'a>(
|
||||
model: &'a SemanticModel,
|
||||
parsed: &'a ParsedModuleRef,
|
||||
pub(crate) fn find_goto_target(
|
||||
parsed: &ParsedModuleRef,
|
||||
offset: TextSize,
|
||||
) -> Option<GotoTarget<'a>> {
|
||||
find_goto_target_impl(model, parsed.tokens(), parsed.syntax().into(), offset)
|
||||
}
|
||||
|
||||
pub(crate) fn find_goto_target_impl<'a>(
|
||||
model: &'a SemanticModel,
|
||||
tokens: &'a Tokens,
|
||||
syntax: AnyNodeRef<'a>,
|
||||
offset: TextSize,
|
||||
) -> Option<GotoTarget<'a>> {
|
||||
let token = tokens
|
||||
) -> Option<GotoTarget<'_>> {
|
||||
let token = parsed
|
||||
.tokens()
|
||||
.at_offset(offset)
|
||||
.max_by_key(|token| match token.kind() {
|
||||
TokenKind::Name
|
||||
@@ -1036,18 +937,18 @@ pub(crate) fn find_goto_target_impl<'a>(
|
||||
return None;
|
||||
}
|
||||
|
||||
let covering_node = covering_node(syntax, token.range())
|
||||
let covering_node = covering_node(parsed.syntax().into(), token.range())
|
||||
.find_first(|node| {
|
||||
node.is_identifier() || node.is_expression() || node.is_stmt_import_from()
|
||||
})
|
||||
.ok()?;
|
||||
|
||||
GotoTarget::from_covering_node(model, &covering_node, offset, tokens)
|
||||
GotoTarget::from_covering_node(&covering_node, offset, parsed.tokens())
|
||||
}
|
||||
|
||||
/// Helper function to resolve a module name and create a navigation target.
|
||||
fn definitions_for_module<'db>(
|
||||
model: &SemanticModel<'db>,
|
||||
model: &SemanticModel,
|
||||
module: Option<&str>,
|
||||
level: u32,
|
||||
) -> Option<DefinitionsOrTargets<'db>> {
|
||||
|
||||
@@ -16,9 +16,9 @@ pub fn goto_declaration(
|
||||
offset: TextSize,
|
||||
) -> Option<RangedValue<NavigationTargets>> {
|
||||
let module = parsed_module(db, file).load(db);
|
||||
let model = SemanticModel::new(db, file);
|
||||
let goto_target = find_goto_target(&model, &module, offset)?;
|
||||
let goto_target = find_goto_target(&module, offset)?;
|
||||
|
||||
let model = SemanticModel::new(db, file);
|
||||
let declaration_targets = goto_target
|
||||
.get_definition_targets(&model, ImportAliasResolution::ResolveAliases)?
|
||||
.declaration_targets(db)?;
|
||||
@@ -889,190 +889,6 @@ def another_helper(path):
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_string_annotation1() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCla<CURSOR>ss" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_declaration(), @r#"
|
||||
info[goto-declaration]: Declaration
|
||||
--> main.py:4:7
|
||||
|
|
||||
2 | a: "MyClass" = 1
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| ^^^^^^^
|
||||
5 | """some docs"""
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:5
|
||||
|
|
||||
2 | a: "MyClass" = 1
|
||||
| ^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_string_annotation2() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyCl<CURSOR>ass" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_declaration(), @r#"
|
||||
info[goto-declaration]: Declaration
|
||||
--> main.py:4:7
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| ^^^^^^^
|
||||
5 | """some docs"""
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:12
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
| ^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_string_annotation3() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None |<CURSOR> MyClass" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_declaration(), @"No goto target found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_string_annotation4() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyClass<CURSOR>" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_declaration(), @r#"
|
||||
info[goto-declaration]: Declaration
|
||||
--> main.py:4:7
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| ^^^^^^^
|
||||
5 | """some docs"""
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:12
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
| ^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_string_annotation5() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyClass"<CURSOR> = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_declaration(), @"No goto target found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_string_annotation_dangling1() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCl<CURSOR>ass |" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_declaration(), @"No goto target found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_string_annotation_dangling2() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCl<CURSOR>ass | No" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_declaration(), @r#"
|
||||
info[goto-declaration]: Declaration
|
||||
--> main.py:4:7
|
||||
|
|
||||
2 | a: "MyClass | No" = 1
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| ^^^^^^^
|
||||
5 | """some docs"""
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:5
|
||||
|
|
||||
2 | a: "MyClass | No" = 1
|
||||
| ^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_string_annotation_dangling3() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyClass | N<CURSOR>o" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_declaration(), @"No goto target found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_nested_instance_attribute() {
|
||||
let test = cursor_test(
|
||||
@@ -1254,45 +1070,6 @@ def outer():
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_nonlocal_stmt() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
def outer():
|
||||
xy = "outer_value"
|
||||
|
||||
def inner():
|
||||
nonlocal x<CURSOR>y
|
||||
xy = "modified"
|
||||
return x # Should find the nonlocal x declaration in outer scope
|
||||
|
||||
return inner
|
||||
"#,
|
||||
);
|
||||
|
||||
// Should find the variable declaration in the outer scope, not the nonlocal statement
|
||||
assert_snapshot!(test.goto_declaration(), @r#"
|
||||
info[goto-declaration]: Declaration
|
||||
--> main.py:3:5
|
||||
|
|
||||
2 | def outer():
|
||||
3 | xy = "outer_value"
|
||||
| ^^
|
||||
4 |
|
||||
5 | def inner():
|
||||
|
|
||||
info: Source
|
||||
--> main.py:6:18
|
||||
|
|
||||
5 | def inner():
|
||||
6 | nonlocal xy
|
||||
| ^^
|
||||
7 | xy = "modified"
|
||||
8 | return x # Should find the nonlocal x declaration in outer scope
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_global_binding() {
|
||||
let test = cursor_test(
|
||||
@@ -1327,41 +1104,6 @@ def function():
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_global_stmt() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
global_var = "global_value"
|
||||
|
||||
def function():
|
||||
global global_<CURSOR>var
|
||||
global_var = "modified"
|
||||
return global_var # Should find the global variable declaration
|
||||
"#,
|
||||
);
|
||||
|
||||
// Should find the global variable declaration, not the global statement
|
||||
assert_snapshot!(test.goto_declaration(), @r#"
|
||||
info[goto-declaration]: Declaration
|
||||
--> main.py:2:1
|
||||
|
|
||||
2 | global_var = "global_value"
|
||||
| ^^^^^^^^^^
|
||||
3 |
|
||||
4 | def function():
|
||||
|
|
||||
info: Source
|
||||
--> main.py:5:12
|
||||
|
|
||||
4 | def function():
|
||||
5 | global global_var
|
||||
| ^^^^^^^^^^
|
||||
6 | global_var = "modified"
|
||||
7 | return global_var # Should find the global variable declaration
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_inherited_attribute() {
|
||||
let test = cursor_test(
|
||||
|
||||
@@ -17,8 +17,8 @@ pub fn goto_definition(
|
||||
offset: TextSize,
|
||||
) -> Option<RangedValue<NavigationTargets>> {
|
||||
let module = parsed_module(db, file).load(db);
|
||||
let goto_target = find_goto_target(&module, offset)?;
|
||||
let model = SemanticModel::new(db, file);
|
||||
let goto_target = find_goto_target(&model, &module, offset)?;
|
||||
let definition_targets = goto_target
|
||||
.get_definition_targets(&model, ImportAliasResolution::ResolveAliases)?
|
||||
.definition_targets(db)?;
|
||||
|
||||
@@ -3,7 +3,6 @@ use crate::references::{ReferencesMode, references};
|
||||
use crate::{Db, ReferenceTarget};
|
||||
use ruff_db::files::File;
|
||||
use ruff_text_size::TextSize;
|
||||
use ty_python_semantic::SemanticModel;
|
||||
|
||||
/// Find all references to a symbol at the given position.
|
||||
/// Search for references across all files in the project.
|
||||
@@ -15,10 +14,9 @@ pub fn goto_references(
|
||||
) -> Option<Vec<ReferenceTarget>> {
|
||||
let parsed = ruff_db::parsed::parsed_module(db, file);
|
||||
let module = parsed.load(db);
|
||||
let model = SemanticModel::new(db, file);
|
||||
|
||||
// Get the definitions for the symbol at the cursor position
|
||||
let goto_target = find_goto_target(&model, &module, offset)?;
|
||||
let goto_target = find_goto_target(&module, offset)?;
|
||||
|
||||
let mode = if include_declaration {
|
||||
ReferencesMode::References
|
||||
@@ -149,6 +147,7 @@ result = calculate_sum(value=42)
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore] // TODO: Enable when nonlocal support is fully implemented in goto.rs
|
||||
fn test_nonlocal_variable_references() {
|
||||
let test = cursor_test(
|
||||
"
|
||||
@@ -182,7 +181,7 @@ def outer_function():
|
||||
2 | def outer_function():
|
||||
3 | counter = 0
|
||||
| ^^^^^^^
|
||||
4 |
|
||||
4 |
|
||||
5 | def increment():
|
||||
|
|
||||
|
||||
@@ -213,7 +212,7 @@ def outer_function():
|
||||
7 | counter += 1
|
||||
8 | return counter
|
||||
| ^^^^^^^
|
||||
9 |
|
||||
9 |
|
||||
10 | def decrement():
|
||||
|
|
||||
|
||||
@@ -244,7 +243,7 @@ def outer_function():
|
||||
12 | counter -= 1
|
||||
13 | return counter
|
||||
| ^^^^^^^
|
||||
14 |
|
||||
14 |
|
||||
15 | # Use counter in outer scope
|
||||
|
|
||||
|
||||
@@ -265,13 +264,14 @@ def outer_function():
|
||||
18 | decrement()
|
||||
19 | final = counter
|
||||
| ^^^^^^^
|
||||
20 |
|
||||
20 |
|
||||
21 | return increment, decrement
|
||||
|
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore] // TODO: Enable when global support is fully implemented in goto.rs
|
||||
fn test_global_variable_references() {
|
||||
let test = cursor_test(
|
||||
"
|
||||
@@ -710,194 +710,6 @@ cls = MyClass
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn references_string_annotation1() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCla<CURSOR>ss" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.references(), @r#"
|
||||
info[references]: Reference 1
|
||||
--> main.py:2:5
|
||||
|
|
||||
2 | a: "MyClass" = 1
|
||||
| ^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
|
||||
info[references]: Reference 2
|
||||
--> main.py:4:7
|
||||
|
|
||||
2 | a: "MyClass" = 1
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| ^^^^^^^
|
||||
5 | """some docs"""
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn references_string_annotation2() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyCl<CURSOR>ass" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.references(), @r#"
|
||||
info[references]: Reference 1
|
||||
--> main.py:2:12
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
| ^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
|
||||
info[references]: Reference 2
|
||||
--> main.py:4:7
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| ^^^^^^^
|
||||
5 | """some docs"""
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn references_string_annotation3() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None |<CURSOR> MyClass" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.references(), @"No references found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn references_string_annotation4() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyClass<CURSOR>" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.references(), @r#"
|
||||
info[references]: Reference 1
|
||||
--> main.py:2:12
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
| ^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
|
||||
info[references]: Reference 2
|
||||
--> main.py:4:7
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| ^^^^^^^
|
||||
5 | """some docs"""
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn references_string_annotation5() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyClass"<CURSOR> = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.references(), @"No references found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn references_string_annotation_dangling1() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCl<CURSOR>ass |" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.references(), @"No references found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn references_string_annotation_dangling2() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCl<CURSOR>ass | No" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.references(), @r#"
|
||||
info[references]: Reference 1
|
||||
--> main.py:2:5
|
||||
|
|
||||
2 | a: "MyClass | No" = 1
|
||||
| ^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
|
||||
info[references]: Reference 2
|
||||
--> main.py:4:7
|
||||
|
|
||||
2 | a: "MyClass | No" = 1
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| ^^^^^^^
|
||||
5 | """some docs"""
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn references_string_annotation_dangling3() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyClass | N<CURSOR>o" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.references(), @"No references found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multi_file_function_references() {
|
||||
let test = CursorTest::builder()
|
||||
|
||||
@@ -11,9 +11,9 @@ pub fn goto_type_definition(
|
||||
offset: TextSize,
|
||||
) -> Option<RangedValue<NavigationTargets>> {
|
||||
let module = parsed_module(db, file).load(db);
|
||||
let model = SemanticModel::new(db, file);
|
||||
let goto_target = find_goto_target(&model, &module, offset)?;
|
||||
let goto_target = find_goto_target(&module, offset)?;
|
||||
|
||||
let model = SemanticModel::new(db, file);
|
||||
let ty = goto_target.inferred_type(&model)?;
|
||||
|
||||
tracing::debug!("Inferred type of covering node is {}", ty.display(db));
|
||||
@@ -744,226 +744,6 @@ mod tests {
|
||||
assert_snapshot!(test.goto_type_definition(), @"No type definitions found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_string_annotation1() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCla<CURSOR>ss" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_type_definition(), @r#"
|
||||
info[goto-type-definition]: Type definition
|
||||
--> main.py:4:7
|
||||
|
|
||||
2 | a: "MyClass" = 1
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| ^^^^^^^
|
||||
5 | """some docs"""
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:5
|
||||
|
|
||||
2 | a: "MyClass" = 1
|
||||
| ^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_string_annotation2() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyCl<CURSOR>ass" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_type_definition(), @"No goto target found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_string_annotation3() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None |<CURSOR> MyClass" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_type_definition(), @r#"
|
||||
info[goto-type-definition]: Type definition
|
||||
--> main.py:4:7
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| ^^^^^^^
|
||||
5 | """some docs"""
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:4
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
|
||||
info[goto-type-definition]: Type definition
|
||||
--> stdlib/types.pyi:950:11
|
||||
|
|
||||
948 | if sys.version_info >= (3, 10):
|
||||
949 | @final
|
||||
950 | class NoneType:
|
||||
| ^^^^^^^^
|
||||
951 | """The type of the None singleton."""
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:4
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_string_annotation4() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyClass<CURSOR>" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_type_definition(), @"No goto target found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_string_annotation5() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyClass"<CURSOR> = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_type_definition(), @r#"
|
||||
info[goto-type-definition]: Type definition
|
||||
--> main.py:4:7
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| ^^^^^^^
|
||||
5 | """some docs"""
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:4
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
|
||||
info[goto-type-definition]: Type definition
|
||||
--> stdlib/types.pyi:950:11
|
||||
|
|
||||
948 | if sys.version_info >= (3, 10):
|
||||
949 | @final
|
||||
950 | class NoneType:
|
||||
| ^^^^^^^^
|
||||
951 | """The type of the None singleton."""
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:4
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_string_annotation_dangling1() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCl<CURSOR>ass |" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_type_definition(), @r#"
|
||||
info[goto-type-definition]: Type definition
|
||||
--> stdlib/ty_extensions.pyi:20:1
|
||||
|
|
||||
19 | # Types
|
||||
20 | Unknown = object()
|
||||
| ^^^^^^^
|
||||
21 | AlwaysTruthy = object()
|
||||
22 | AlwaysFalsy = object()
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:4
|
||||
|
|
||||
2 | a: "MyClass |" = 1
|
||||
| ^^^^^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_string_annotation_dangling2() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCl<CURSOR>ass | No" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_type_definition(), @"No goto target found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_string_annotation_dangling3() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyClass | N<CURSOR>o" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.goto_type_definition(), @"No goto target found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_on_keyword_argument() {
|
||||
let test = cursor_test(
|
||||
@@ -1062,118 +842,6 @@ f(**kwargs<CURSOR>)
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_nonlocal_binding() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
def outer():
|
||||
x = "outer_value"
|
||||
|
||||
def inner():
|
||||
nonlocal x
|
||||
x = "modified"
|
||||
return x<CURSOR> # Should find the nonlocal x declaration in outer scope
|
||||
|
||||
return inner
|
||||
"#,
|
||||
);
|
||||
|
||||
// Should find the variable declaration in the outer scope, not the nonlocal statement
|
||||
assert_snapshot!(test.goto_type_definition(), @r#"
|
||||
info[goto-type-definition]: Type definition
|
||||
--> stdlib/builtins.pyi:915:7
|
||||
|
|
||||
914 | @disjoint_base
|
||||
915 | class str(Sequence[str]):
|
||||
| ^^^
|
||||
916 | """str(object='') -> str
|
||||
917 | str(bytes_or_buffer[, encoding[, errors]]) -> str
|
||||
|
|
||||
info: Source
|
||||
--> main.py:8:16
|
||||
|
|
||||
6 | nonlocal x
|
||||
7 | x = "modified"
|
||||
8 | return x # Should find the nonlocal x declaration in outer scope
|
||||
| ^
|
||||
9 |
|
||||
10 | return inner
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_nonlocal_stmt() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
def outer():
|
||||
xy = "outer_value"
|
||||
|
||||
def inner():
|
||||
nonlocal x<CURSOR>y
|
||||
xy = "modified"
|
||||
return x # Should find the nonlocal x declaration in outer scope
|
||||
|
||||
return inner
|
||||
"#,
|
||||
);
|
||||
|
||||
// Should find the variable declaration in the outer scope, not the nonlocal statement
|
||||
assert_snapshot!(test.goto_type_definition(), @"No goto target found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_global_binding() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
global_var = "global_value"
|
||||
|
||||
def function():
|
||||
global global_var
|
||||
global_var = "modified"
|
||||
return global_<CURSOR>var # Should find the global variable declaration
|
||||
"#,
|
||||
);
|
||||
|
||||
// Should find the global variable declaration, not the global statement
|
||||
assert_snapshot!(test.goto_type_definition(), @r#"
|
||||
info[goto-type-definition]: Type definition
|
||||
--> stdlib/builtins.pyi:915:7
|
||||
|
|
||||
914 | @disjoint_base
|
||||
915 | class str(Sequence[str]):
|
||||
| ^^^
|
||||
916 | """str(object='') -> str
|
||||
917 | str(bytes_or_buffer[, encoding[, errors]]) -> str
|
||||
|
|
||||
info: Source
|
||||
--> main.py:7:12
|
||||
|
|
||||
5 | global global_var
|
||||
6 | global_var = "modified"
|
||||
7 | return global_var # Should find the global variable declaration
|
||||
| ^^^^^^^^^^
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_global_stmt() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
global_var = "global_value"
|
||||
|
||||
def function():
|
||||
global global_<CURSOR>var
|
||||
global_var = "modified"
|
||||
return global_var # Should find the global variable declaration
|
||||
"#,
|
||||
);
|
||||
|
||||
// Should find the global variable declaration, not the global statement
|
||||
assert_snapshot!(test.goto_type_definition(), @"No goto target found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_of_expression_with_builtin() {
|
||||
let test = cursor_test(
|
||||
|
||||
@@ -11,8 +11,7 @@ use ty_python_semantic::{DisplaySettings, SemanticModel};
|
||||
|
||||
pub fn hover(db: &dyn Db, file: File, offset: TextSize) -> Option<RangedValue<Hover<'_>>> {
|
||||
let parsed = parsed_module(db, file).load(db);
|
||||
let model = SemanticModel::new(db, file);
|
||||
let goto_target = find_goto_target(&model, &parsed, offset)?;
|
||||
let goto_target = find_goto_target(&parsed, offset)?;
|
||||
|
||||
if let GotoTarget::Expression(expr) = goto_target {
|
||||
if expr.is_literal_expr() {
|
||||
@@ -20,6 +19,7 @@ pub fn hover(db: &dyn Db, file: File, offset: TextSize) -> Option<RangedValue<Ho
|
||||
}
|
||||
}
|
||||
|
||||
let model = SemanticModel::new(db, file);
|
||||
let docs = goto_target
|
||||
.get_definition_targets(
|
||||
&model,
|
||||
@@ -904,191 +904,6 @@ mod tests {
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_string_annotation1() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCla<CURSOR>ss" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.hover(), @r#"
|
||||
MyClass
|
||||
---------------------------------------------
|
||||
some docs
|
||||
|
||||
---------------------------------------------
|
||||
```python
|
||||
MyClass
|
||||
```
|
||||
---
|
||||
some docs
|
||||
---------------------------------------------
|
||||
info[hover]: Hovered content is
|
||||
--> main.py:2:5
|
||||
|
|
||||
2 | a: "MyClass" = 1
|
||||
| ^^^^^-^
|
||||
| | |
|
||||
| | Cursor offset
|
||||
| source
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_string_annotation2() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyCl<CURSOR>ass" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.hover(), @r#"
|
||||
some docs
|
||||
|
||||
---------------------------------------------
|
||||
some docs
|
||||
---------------------------------------------
|
||||
info[hover]: Hovered content is
|
||||
--> main.py:2:12
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
| ^^^^-^^
|
||||
| | |
|
||||
| | Cursor offset
|
||||
| source
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_string_annotation3() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None |<CURSOR> MyClass" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.hover(), @"Hover provided no content");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_string_annotation4() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyClass<CURSOR>" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.hover(), @r#"
|
||||
some docs
|
||||
|
||||
---------------------------------------------
|
||||
some docs
|
||||
---------------------------------------------
|
||||
info[hover]: Hovered content is
|
||||
--> main.py:2:12
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
| ^^^^^^^- Cursor offset
|
||||
| |
|
||||
| source
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_string_annotation5() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyClass"<CURSOR> = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.hover(), @"Hover provided no content");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_string_annotation_dangling1() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCl<CURSOR>ass |" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.hover(), @"Hover provided no content");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_string_annotation_dangling2() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCl<CURSOR>ass | No" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.hover(), @r#"
|
||||
some docs
|
||||
|
||||
---------------------------------------------
|
||||
some docs
|
||||
---------------------------------------------
|
||||
info[hover]: Hovered content is
|
||||
--> main.py:2:5
|
||||
|
|
||||
2 | a: "MyClass | No" = 1
|
||||
| ^^^^-^^
|
||||
| | |
|
||||
| | Cursor offset
|
||||
| source
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_string_annotation_dangling3() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyClass | N<CURSOR>o" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.hover(), @"Hover provided no content");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_overload_type_disambiguated1() {
|
||||
let test = CursorTest::builder()
|
||||
@@ -1648,117 +1463,6 @@ def ab(a: int, *, c: int):
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_nonlocal_binding() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
def outer():
|
||||
x = "outer_value"
|
||||
|
||||
def inner():
|
||||
nonlocal x
|
||||
x = "modified"
|
||||
return x<CURSOR> # Should find the nonlocal x declaration in outer scope
|
||||
|
||||
return inner
|
||||
"#,
|
||||
);
|
||||
|
||||
// Should find the variable declaration in the outer scope, not the nonlocal statement
|
||||
assert_snapshot!(test.hover(), @r#"
|
||||
Literal["modified"]
|
||||
---------------------------------------------
|
||||
```python
|
||||
Literal["modified"]
|
||||
```
|
||||
---------------------------------------------
|
||||
info[hover]: Hovered content is
|
||||
--> main.py:8:16
|
||||
|
|
||||
6 | nonlocal x
|
||||
7 | x = "modified"
|
||||
8 | return x # Should find the nonlocal x declaration in outer scope
|
||||
| ^- Cursor offset
|
||||
| |
|
||||
| source
|
||||
9 |
|
||||
10 | return inner
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_nonlocal_stmt() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
def outer():
|
||||
xy = "outer_value"
|
||||
|
||||
def inner():
|
||||
nonlocal x<CURSOR>y
|
||||
xy = "modified"
|
||||
return x # Should find the nonlocal x declaration in outer scope
|
||||
|
||||
return inner
|
||||
"#,
|
||||
);
|
||||
|
||||
// Should find the variable declaration in the outer scope, not the nonlocal statement
|
||||
assert_snapshot!(test.hover(), @"Hover provided no content");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_global_binding() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
global_var = "global_value"
|
||||
|
||||
def function():
|
||||
global global_var
|
||||
global_var = "modified"
|
||||
return global_<CURSOR>var # Should find the global variable declaration
|
||||
"#,
|
||||
);
|
||||
|
||||
// Should find the global variable declaration, not the global statement
|
||||
assert_snapshot!(test.hover(), @r#"
|
||||
Literal["modified"]
|
||||
---------------------------------------------
|
||||
```python
|
||||
Literal["modified"]
|
||||
```
|
||||
---------------------------------------------
|
||||
info[hover]: Hovered content is
|
||||
--> main.py:7:12
|
||||
|
|
||||
5 | global global_var
|
||||
6 | global_var = "modified"
|
||||
7 | return global_var # Should find the global variable declaration
|
||||
| ^^^^^^^-^^
|
||||
| | |
|
||||
| | Cursor offset
|
||||
| source
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_global_stmt() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
global_var = "global_value"
|
||||
|
||||
def function():
|
||||
global global_<CURSOR>var
|
||||
global_var = "modified"
|
||||
return global_var # Should find the global variable declaration
|
||||
"#,
|
||||
);
|
||||
|
||||
// Should find the global variable declaration, not the global statement
|
||||
assert_snapshot!(test.hover(), @"Hover provided no content");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_module_import() {
|
||||
let mut test = cursor_test(
|
||||
|
||||
@@ -6294,22 +6294,6 @@ mod tests {
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_literal_type_alias_inlay_hint() {
|
||||
let mut test = inlay_hint_test(
|
||||
"
|
||||
from typing import Literal
|
||||
|
||||
a = Literal['a', 'b', 'c']",
|
||||
);
|
||||
|
||||
assert_snapshot!(test.inlay_hints(), @r"
|
||||
from typing import Literal
|
||||
|
||||
a[: <typing.Literal special form>] = Literal['a', 'b', 'c']
|
||||
");
|
||||
}
|
||||
|
||||
struct InlayHintLocationDiagnostic {
|
||||
source: FileRange,
|
||||
target: FileRange,
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
reason = "Prefer System trait methods over std methods in ty crates"
|
||||
)]
|
||||
mod all_symbols;
|
||||
mod code_action;
|
||||
mod completion;
|
||||
mod doc_highlights;
|
||||
mod docstring;
|
||||
@@ -28,7 +27,6 @@ mod symbols;
|
||||
mod workspace_symbols;
|
||||
|
||||
pub use all_symbols::{AllSymbolInfo, all_symbols};
|
||||
pub use code_action::{QuickFix, code_actions};
|
||||
pub use completion::{Completion, CompletionKind, CompletionSettings, completion};
|
||||
pub use doc_highlights::document_highlights;
|
||||
pub use document_symbols::document_symbols;
|
||||
|
||||
@@ -122,10 +122,10 @@ fn references_for_file(
|
||||
) {
|
||||
let parsed = ruff_db::parsed::parsed_module(db, file);
|
||||
let module = parsed.load(db);
|
||||
let model = SemanticModel::new(db, file);
|
||||
|
||||
let mut finder = LocalReferencesFinder {
|
||||
model: &model,
|
||||
db,
|
||||
file,
|
||||
target_definitions,
|
||||
references,
|
||||
mode,
|
||||
@@ -157,7 +157,8 @@ fn is_symbol_externally_visible(goto_target: &GotoTarget<'_>) -> bool {
|
||||
|
||||
/// AST visitor to find all references to a specific symbol by comparing semantic definitions
|
||||
struct LocalReferencesFinder<'a> {
|
||||
model: &'a SemanticModel<'a>,
|
||||
db: &'a dyn Db,
|
||||
file: File,
|
||||
tokens: &'a Tokens,
|
||||
target_definitions: &'a [NavigationTarget],
|
||||
references: &'a mut Vec<ReferenceTarget>,
|
||||
@@ -226,22 +227,6 @@ impl<'a> SourceOrderVisitor<'a> for LocalReferencesFinder<'a> {
|
||||
self.check_identifier_reference(rest_name);
|
||||
}
|
||||
}
|
||||
AnyNodeRef::ExprStringLiteral(string_expr) if self.should_include_declaration() => {
|
||||
// Highlight the sub-AST of a string annotation
|
||||
if let Some((sub_ast, sub_model)) = self.model.enter_string_annotation(string_expr)
|
||||
{
|
||||
let mut sub_finder = LocalReferencesFinder {
|
||||
model: &sub_model,
|
||||
target_definitions: self.target_definitions,
|
||||
references: self.references,
|
||||
mode: self.mode,
|
||||
tokens: sub_ast.tokens(),
|
||||
target_text: self.target_text,
|
||||
ancestors: Vec::new(),
|
||||
};
|
||||
sub_finder.visit_expr(sub_ast.expr());
|
||||
}
|
||||
}
|
||||
AnyNodeRef::Alias(alias) if self.should_include_declaration() => {
|
||||
// Handle import alias declarations
|
||||
if let Some(asname) = &alias.asname {
|
||||
@@ -300,13 +285,15 @@ impl LocalReferencesFinder<'_> {
|
||||
// the node is fine here. Offsets matter only for import statements
|
||||
// where the identifier might be a multi-part module name.
|
||||
let offset = covering_node.node().start();
|
||||
|
||||
if let Some(goto_target) =
|
||||
GotoTarget::from_covering_node(self.model, covering_node, offset, self.tokens)
|
||||
GotoTarget::from_covering_node(covering_node, offset, self.tokens)
|
||||
{
|
||||
// Get the definitions for this goto target
|
||||
let model = SemanticModel::new(self.db, self.file);
|
||||
if let Some(current_definitions_nav) = goto_target
|
||||
.get_definition_targets(self.model, ImportAliasResolution::PreserveAliases)
|
||||
.and_then(|definitions| definitions.declaration_targets(self.model.db()))
|
||||
.get_definition_targets(&model, ImportAliasResolution::PreserveAliases)
|
||||
.and_then(|definitions| definitions.declaration_targets(self.db))
|
||||
{
|
||||
let current_definitions: Vec<NavigationTarget> =
|
||||
current_definitions_nav.into_iter().collect();
|
||||
@@ -315,7 +302,7 @@ impl LocalReferencesFinder<'_> {
|
||||
// Determine if this is a read or write reference
|
||||
let kind = self.determine_reference_kind(covering_node);
|
||||
let target =
|
||||
ReferenceTarget::new(self.model.file(), covering_node.node().range(), kind);
|
||||
ReferenceTarget::new(self.file, covering_node.node().range(), kind);
|
||||
self.references.push(target);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ pub fn can_rename(db: &dyn Db, file: File, offset: TextSize) -> Option<ruff_text
|
||||
let model = SemanticModel::new(db, file);
|
||||
|
||||
// Get the definitions for the symbol at the offset
|
||||
let goto_target = find_goto_target(&model, &module, offset)?;
|
||||
let goto_target = find_goto_target(&module, offset)?;
|
||||
|
||||
// Don't allow renaming of import module components
|
||||
if matches!(
|
||||
@@ -59,10 +59,9 @@ pub fn rename(
|
||||
) -> Option<Vec<ReferenceTarget>> {
|
||||
let parsed = ruff_db::parsed::parsed_module(db, file);
|
||||
let module = parsed.load(db);
|
||||
let model = SemanticModel::new(db, file);
|
||||
|
||||
// Get the definitions for the symbol at the offset
|
||||
let goto_target = find_goto_target(&model, &module, offset)?;
|
||||
let goto_target = find_goto_target(&module, offset)?;
|
||||
|
||||
// Clients shouldn't call us with an empty new name, but just in case...
|
||||
if new_name.is_empty() {
|
||||
@@ -339,162 +338,6 @@ class DataProcessor:
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_string_annotation1() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCla<CURSOR>ss" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.rename("MyNewClass"), @r#"
|
||||
info[rename]: Rename symbol (found 2 locations)
|
||||
--> main.py:2:5
|
||||
|
|
||||
2 | a: "MyClass" = 1
|
||||
| ^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| -------
|
||||
5 | """some docs"""
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_string_annotation2() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyCl<CURSOR>ass" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.rename("MyNewClass"), @r#"
|
||||
info[rename]: Rename symbol (found 2 locations)
|
||||
--> main.py:2:12
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
| ^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| -------
|
||||
5 | """some docs"""
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_string_annotation3() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None |<CURSOR> MyClass" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.rename("MyNewClass"), @"Cannot rename");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_string_annotation4() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyClass<CURSOR>" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.rename("MyNewClass"), @r#"
|
||||
info[rename]: Rename symbol (found 2 locations)
|
||||
--> main.py:2:12
|
||||
|
|
||||
2 | a: "None | MyClass" = 1
|
||||
| ^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| -------
|
||||
5 | """some docs"""
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_string_annotation5() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "None | MyClass"<CURSOR> = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.rename("MyNewClass"), @"Cannot rename");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_string_annotation_dangling1() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCl<CURSOR>ass |" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.rename("MyNewClass"), @"Cannot rename");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_string_annotation_dangling2() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyCl<CURSOR>ass | No" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.rename("MyNewClass"), @r#"
|
||||
info[rename]: Rename symbol (found 2 locations)
|
||||
--> main.py:2:5
|
||||
|
|
||||
2 | a: "MyClass | No" = 1
|
||||
| ^^^^^^^
|
||||
3 |
|
||||
4 | class MyClass:
|
||||
| -------
|
||||
5 | """some docs"""
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_string_annotation_dangling3() {
|
||||
let test = cursor_test(
|
||||
r#"
|
||||
a: "MyClass | N<CURSOR>o" = 1
|
||||
|
||||
class MyClass:
|
||||
"""some docs"""
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_snapshot!(test.rename("MyNewClass"), @"Cannot rename");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cannot_rename_import_module_component() {
|
||||
// Test that we cannot rename parts of module names in import statements
|
||||
|
||||
@@ -187,9 +187,9 @@ impl Deref for SemanticTokens {
|
||||
/// Pass None to get tokens for the entire file.
|
||||
pub fn semantic_tokens(db: &dyn Db, file: File, range: Option<TextRange>) -> SemanticTokens {
|
||||
let parsed = parsed_module(db, file).load(db);
|
||||
let model = SemanticModel::new(db, file);
|
||||
let semantic_model = SemanticModel::new(db, file);
|
||||
|
||||
let mut visitor = SemanticTokenVisitor::new(&model, range);
|
||||
let mut visitor = SemanticTokenVisitor::new(&semantic_model, file, range);
|
||||
visitor.visit_body(parsed.suite());
|
||||
|
||||
SemanticTokens::new(visitor.tokens)
|
||||
@@ -197,7 +197,8 @@ pub fn semantic_tokens(db: &dyn Db, file: File, range: Option<TextRange>) -> Sem
|
||||
|
||||
/// AST visitor that collects semantic tokens.
|
||||
struct SemanticTokenVisitor<'db> {
|
||||
model: &'db SemanticModel<'db>,
|
||||
semantic_model: &'db SemanticModel<'db>,
|
||||
file: File,
|
||||
tokens: Vec<SemanticToken>,
|
||||
in_class_scope: bool,
|
||||
in_type_annotation: bool,
|
||||
@@ -206,9 +207,14 @@ struct SemanticTokenVisitor<'db> {
|
||||
}
|
||||
|
||||
impl<'db> SemanticTokenVisitor<'db> {
|
||||
fn new(model: &'db SemanticModel<'db>, range_filter: Option<TextRange>) -> Self {
|
||||
fn new(
|
||||
semantic_model: &'db SemanticModel<'db>,
|
||||
file: File,
|
||||
range_filter: Option<TextRange>,
|
||||
) -> Self {
|
||||
Self {
|
||||
model,
|
||||
semantic_model,
|
||||
file,
|
||||
tokens: Vec::new(),
|
||||
in_class_scope: false,
|
||||
in_target_creating_definition: false,
|
||||
@@ -259,7 +265,7 @@ impl<'db> SemanticTokenVisitor<'db> {
|
||||
|
||||
fn classify_name(&self, name: &ast::ExprName) -> (SemanticTokenType, SemanticTokenModifier) {
|
||||
// First try to classify the token based on its definition kind.
|
||||
let definition = definition_for_name(self.model, name);
|
||||
let definition = definition_for_name(self.semantic_model.db(), self.file, name);
|
||||
|
||||
if let Some(definition) = definition {
|
||||
let name_str = name.id.as_str();
|
||||
@@ -269,7 +275,7 @@ impl<'db> SemanticTokenVisitor<'db> {
|
||||
}
|
||||
|
||||
// Fall back to type-based classification.
|
||||
let ty = name.inferred_type(self.model);
|
||||
let ty = name.inferred_type(self.semantic_model);
|
||||
let name_str = name.id.as_str();
|
||||
self.classify_from_type_and_name_str(ty, name_str)
|
||||
}
|
||||
@@ -280,7 +286,7 @@ impl<'db> SemanticTokenVisitor<'db> {
|
||||
name_str: &str,
|
||||
) -> Option<(SemanticTokenType, SemanticTokenModifier)> {
|
||||
let mut modifiers = SemanticTokenModifier::empty();
|
||||
let db = self.model.db();
|
||||
let db = self.semantic_model.db();
|
||||
let model = SemanticModel::new(db, definition.file(db));
|
||||
|
||||
match definition.kind(db) {
|
||||
@@ -706,12 +712,12 @@ impl SourceOrderVisitor<'_> for SemanticTokenVisitor<'_> {
|
||||
for alias in &import.names {
|
||||
if let Some(asname) = &alias.asname {
|
||||
// For aliased imports (from X import Y as Z), classify Z based on what Y is
|
||||
let ty = alias.inferred_type(self.model);
|
||||
let ty = alias.inferred_type(self.semantic_model);
|
||||
let (token_type, modifiers) = self.classify_from_alias_type(ty, asname);
|
||||
self.add_token(asname, token_type, modifiers);
|
||||
} else {
|
||||
// For direct imports (from X import Y), use semantic classification
|
||||
let ty = alias.inferred_type(self.model);
|
||||
let ty = alias.inferred_type(self.semantic_model);
|
||||
let (token_type, modifiers) =
|
||||
self.classify_from_alias_type(ty, &alias.name);
|
||||
self.add_token(&alias.name, token_type, modifiers);
|
||||
@@ -831,7 +837,7 @@ impl SourceOrderVisitor<'_> for SemanticTokenVisitor<'_> {
|
||||
self.visit_expr(&attr.value);
|
||||
|
||||
// Then add token for the attribute name (e.g., 'path' in 'os.path')
|
||||
let ty = expr.inferred_type(self.model);
|
||||
let ty = expr.inferred_type(self.semantic_model);
|
||||
let (token_type, modifiers) =
|
||||
Self::classify_from_type_for_attribute(ty, &attr.attr);
|
||||
self.add_token(&attr.attr, token_type, modifiers);
|
||||
@@ -875,17 +881,6 @@ impl SourceOrderVisitor<'_> for SemanticTokenVisitor<'_> {
|
||||
|
||||
self.visit_expr(&named.value);
|
||||
}
|
||||
ast::Expr::StringLiteral(string_expr) => {
|
||||
// Highlight the sub-AST of a string annotation
|
||||
if let Some((sub_ast, sub_model)) = self.model.enter_string_annotation(string_expr)
|
||||
{
|
||||
let mut sub_visitor = SemanticTokenVisitor::new(&sub_model, None);
|
||||
sub_visitor.visit_expr(sub_ast.expr());
|
||||
self.tokens.extend(sub_visitor.tokens);
|
||||
} else {
|
||||
walk_expr(self, expr);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// For all other expression types, let the default visitor handle them
|
||||
walk_expr(self, expr);
|
||||
@@ -1066,17 +1061,6 @@ impl SourceOrderVisitor<'_> for SemanticTokenVisitor<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_comprehension(&mut self, comp: &ast::Comprehension) {
|
||||
self.in_target_creating_definition = true;
|
||||
self.visit_expr(&comp.target);
|
||||
self.in_target_creating_definition = false;
|
||||
|
||||
self.visit_expr(&comp.iter);
|
||||
for if_clause in &comp.ifs {
|
||||
self.visit_expr(if_clause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -1580,50 +1564,6 @@ from mymodule import CONSTANT, my_function, MyClass
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_str_annotation() {
|
||||
let test = SemanticTokenTest::new(
|
||||
r#"
|
||||
x: int = 1
|
||||
y: "int" = 1
|
||||
z = "int"
|
||||
w1: "int | str" = "hello"
|
||||
w2: "int | sr" = "hello"
|
||||
w3: "int | " = "hello"
|
||||
w4: "float"
|
||||
w5: "float
|
||||
"#,
|
||||
);
|
||||
|
||||
let tokens = test.highlight_file();
|
||||
|
||||
assert_snapshot!(test.to_snapshot(&tokens), @r#"
|
||||
"x" @ 1..2: Variable [definition]
|
||||
"int" @ 4..7: Class
|
||||
"1" @ 10..11: Number
|
||||
"y" @ 12..13: Variable [definition]
|
||||
"int" @ 16..19: Class
|
||||
"1" @ 23..24: Number
|
||||
"z" @ 25..26: Variable [definition]
|
||||
"\"int\"" @ 29..34: String
|
||||
"w1" @ 35..37: Variable [definition]
|
||||
"int" @ 40..43: Class
|
||||
"str" @ 46..49: Class
|
||||
"\"hello\"" @ 53..60: String
|
||||
"w2" @ 61..63: Variable [definition]
|
||||
"int" @ 66..69: Class
|
||||
"sr" @ 72..74: Variable
|
||||
"\"hello\"" @ 78..85: String
|
||||
"w3" @ 86..88: Variable [definition]
|
||||
"\"int | \"" @ 90..98: String
|
||||
"\"hello\"" @ 101..108: String
|
||||
"w4" @ 109..111: Variable [definition]
|
||||
"float" @ 114..119: Class
|
||||
"w5" @ 121..123: Variable [definition]
|
||||
"float" @ 126..131: Class
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_attribute_classification() {
|
||||
let test = SemanticTokenTest::new(
|
||||
@@ -2655,50 +2595,6 @@ with open("file.txt") as f:
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_comprehensions() {
|
||||
let test = SemanticTokenTest::new(
|
||||
r#"
|
||||
list_comp = [x for x in range(10) if x % 2 == 0]
|
||||
set_comp = {x for x in range(10)}
|
||||
dict_comp = {k: v for k, v in zip(["a", "b"], [1, 2])}
|
||||
generator = (x for x in range(10))
|
||||
"#,
|
||||
);
|
||||
|
||||
let tokens = test.highlight_file();
|
||||
assert_snapshot!(test.to_snapshot(&tokens), @r#"
|
||||
"list_comp" @ 1..10: Variable [definition]
|
||||
"x" @ 14..15: Variable
|
||||
"x" @ 20..21: Variable [definition]
|
||||
"range" @ 25..30: Class
|
||||
"10" @ 31..33: Number
|
||||
"x" @ 38..39: Variable
|
||||
"2" @ 42..43: Number
|
||||
"0" @ 47..48: Number
|
||||
"set_comp" @ 50..58: Variable [definition]
|
||||
"x" @ 62..63: Variable
|
||||
"x" @ 68..69: Variable [definition]
|
||||
"range" @ 73..78: Class
|
||||
"10" @ 79..81: Number
|
||||
"dict_comp" @ 84..93: Variable [definition]
|
||||
"k" @ 97..98: Variable
|
||||
"v" @ 100..101: Variable
|
||||
"k" @ 106..107: Variable [definition]
|
||||
"v" @ 109..110: Variable [definition]
|
||||
"zip" @ 114..117: Class
|
||||
"\"a\"" @ 119..122: String
|
||||
"\"b\"" @ 124..127: String
|
||||
"1" @ 131..132: Number
|
||||
"2" @ 134..135: Number
|
||||
"generator" @ 139..148: Variable [definition]
|
||||
"x" @ 152..153: Variable
|
||||
"x" @ 158..159: Variable [definition]
|
||||
"range" @ 163..168: Class
|
||||
"10" @ 169..171: Number
|
||||
"#);
|
||||
}
|
||||
|
||||
/// Regression test for <https://github.com/astral-sh/ty/issues/1406>
|
||||
#[test]
|
||||
fn test_invalid_kwargs() {
|
||||
|
||||
@@ -74,7 +74,7 @@ pub fn signature_help(db: &dyn Db, file: File, offset: TextSize) -> Option<Signa
|
||||
|
||||
// Get signature details from the semantic analyzer.
|
||||
let signature_details: Vec<CallSignatureDetails<'_>> =
|
||||
call_signature_details(&model, call_expr);
|
||||
call_signature_details(db, &model, call_expr);
|
||||
|
||||
if signature_details.is_empty() {
|
||||
return None;
|
||||
|
||||
@@ -11,12 +11,12 @@ use crate::cached_vendored_root;
|
||||
/// other language server providers (like hover, completion, and signature help) to find
|
||||
/// docstrings for functions that resolve to stubs.
|
||||
pub(crate) struct StubMapper<'db> {
|
||||
db: &'db dyn ty_python_semantic::Db,
|
||||
db: &'db dyn crate::Db,
|
||||
cached_vendored_root: Option<SystemPathBuf>,
|
||||
}
|
||||
|
||||
impl<'db> StubMapper<'db> {
|
||||
pub(crate) fn new(db: &'db dyn ty_python_semantic::Db) -> Self {
|
||||
pub(crate) fn new(db: &'db dyn crate::Db) -> Self {
|
||||
let cached_vendored_root = cached_vendored_root(db);
|
||||
Self {
|
||||
db,
|
||||
|
||||
@@ -23,12 +23,11 @@ use crate::completion::CompletionKind;
|
||||
pub struct QueryPattern {
|
||||
re: Option<Regex>,
|
||||
original: String,
|
||||
original_is_exact: bool,
|
||||
}
|
||||
|
||||
impl QueryPattern {
|
||||
/// Create a new query pattern from a literal search string given.
|
||||
pub fn fuzzy(literal_query_string: &str) -> QueryPattern {
|
||||
pub fn new(literal_query_string: &str) -> QueryPattern {
|
||||
let mut pattern = "(?i)".to_string();
|
||||
for ch in literal_query_string.chars() {
|
||||
pattern.push_str(®ex::escape(ch.encode_utf8(&mut [0; 4])));
|
||||
@@ -42,16 +41,6 @@ impl QueryPattern {
|
||||
QueryPattern {
|
||||
re: Regex::new(&pattern).ok(),
|
||||
original: literal_query_string.to_string(),
|
||||
original_is_exact: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new query
|
||||
pub fn exactly(symbol: &str) -> QueryPattern {
|
||||
QueryPattern {
|
||||
re: None,
|
||||
original: symbol.to_string(),
|
||||
original_is_exact: true,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +49,6 @@ impl QueryPattern {
|
||||
QueryPattern {
|
||||
re: None,
|
||||
original: String::new(),
|
||||
original_is_exact: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,8 +59,6 @@ impl QueryPattern {
|
||||
pub fn is_match_symbol_name(&self, symbol_name: &str) -> bool {
|
||||
if let Some(ref re) = self.re {
|
||||
re.is_match(symbol_name)
|
||||
} else if self.original_is_exact {
|
||||
symbol_name == self.original
|
||||
} else {
|
||||
// This is a degenerate case. The only way
|
||||
// we should get here is if the query string
|
||||
@@ -89,13 +75,13 @@ impl QueryPattern {
|
||||
/// incorrectly. That is, it's possible that this query will match all
|
||||
/// inputs but this still returns `false`.
|
||||
pub fn will_match_everything(&self) -> bool {
|
||||
self.re.is_none() && self.original.is_empty()
|
||||
self.re.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for QueryPattern {
|
||||
fn from(literal_query_string: &str) -> QueryPattern {
|
||||
QueryPattern::fuzzy(literal_query_string)
|
||||
QueryPattern::new(literal_query_string)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -579,7 +565,7 @@ impl SourceOrderVisitor<'_> for SymbolVisitor {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
fn matches(query: &str, symbol: &str) -> bool {
|
||||
super::QueryPattern::fuzzy(query).is_match_symbol_name(symbol)
|
||||
super::QueryPattern::new(query).is_match_symbol_name(symbol)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -12,7 +12,7 @@ pub fn workspace_symbols(db: &dyn Db, query: &str) -> Vec<WorkspaceSymbolInfo> {
|
||||
|
||||
let project = db.project();
|
||||
|
||||
let query = QueryPattern::fuzzy(query);
|
||||
let query = QueryPattern::new(query);
|
||||
let files = project.files(db);
|
||||
let results = std::sync::Mutex::new(Vec::new());
|
||||
{
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
use configuration_file::{ConfigurationFile, ConfigurationFileError};
|
||||
use ruff_db::system::walk_directory::WalkState;
|
||||
use ruff_db::system::{System, SystemPath, SystemPathBuf};
|
||||
use ruff_db::vendored::VendoredFileSystem;
|
||||
use ruff_python_ast::name::Name;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
use thiserror::Error;
|
||||
use ty_combine::Combine;
|
||||
@@ -21,7 +19,7 @@ pub mod pyproject;
|
||||
pub mod settings;
|
||||
pub mod value;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, get_size2::GetSize)]
|
||||
#[derive(Debug, PartialEq, Eq, get_size2::GetSize)]
|
||||
#[cfg_attr(test, derive(serde::Serialize))]
|
||||
pub struct ProjectMetadata {
|
||||
pub(super) name: Name,
|
||||
@@ -134,250 +132,124 @@ impl ProjectMetadata {
|
||||
path: &SystemPath,
|
||||
system: &dyn System,
|
||||
) -> Result<ProjectMetadata, ProjectMetadataError> {
|
||||
if !system.is_directory(path) {
|
||||
return Err(ProjectMetadataError::NotADirectory(path.to_path_buf()));
|
||||
}
|
||||
|
||||
let closest = Self::discover_closest(path, system)?;
|
||||
Ok(closest.into_metadata())
|
||||
}
|
||||
|
||||
fn discover_closest(
|
||||
path: &SystemPath,
|
||||
system: &dyn System,
|
||||
) -> Result<DiscoveredProject, ProjectMetadataError> {
|
||||
tracing::debug!("Searching for a project in '{path}'");
|
||||
|
||||
if !system.is_directory(path) {
|
||||
return Err(ProjectMetadataError::NotADirectory(path.to_path_buf()));
|
||||
}
|
||||
|
||||
let mut closest_project: Option<DiscoveredProject> = None;
|
||||
let mut closest_project: Option<ProjectMetadata> = None;
|
||||
|
||||
for project_root in path.ancestors() {
|
||||
let Some(discovered) = Self::discover_in(project_root, system)? else {
|
||||
continue;
|
||||
let pyproject_path = project_root.join("pyproject.toml");
|
||||
|
||||
let pyproject = if let Ok(pyproject_str) = system.read_to_string(&pyproject_path) {
|
||||
match PyProject::from_toml_str(
|
||||
&pyproject_str,
|
||||
ValueSource::File(Arc::new(pyproject_path.clone())),
|
||||
) {
|
||||
Ok(pyproject) => Some(pyproject),
|
||||
Err(error) => {
|
||||
return Err(ProjectMetadataError::InvalidPyProject {
|
||||
path: pyproject_path,
|
||||
source: Box::new(error),
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
match discovered {
|
||||
DiscoveredProject::PyProject {
|
||||
has_ty_section: true,
|
||||
..
|
||||
// A `ty.toml` takes precedence over a `pyproject.toml`.
|
||||
let ty_toml_path = project_root.join("ty.toml");
|
||||
if let Ok(ty_str) = system.read_to_string(&ty_toml_path) {
|
||||
let options = match Options::from_toml_str(
|
||||
&ty_str,
|
||||
ValueSource::File(Arc::new(ty_toml_path.clone())),
|
||||
) {
|
||||
Ok(options) => options,
|
||||
Err(error) => {
|
||||
return Err(ProjectMetadataError::InvalidTyToml {
|
||||
path: ty_toml_path,
|
||||
source: Box::new(error),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if pyproject
|
||||
.as_ref()
|
||||
.is_some_and(|project| project.ty().is_some())
|
||||
{
|
||||
// TODO: Consider using a diagnostic here
|
||||
tracing::warn!(
|
||||
"Ignoring the `tool.ty` section in `{pyproject_path}` because `{ty_toml_path}` takes precedence."
|
||||
);
|
||||
}
|
||||
| DiscoveredProject::Ty { .. } => {
|
||||
|
||||
tracing::debug!("Found project at '{}'", project_root);
|
||||
|
||||
let metadata = ProjectMetadata::from_options(
|
||||
options,
|
||||
project_root.to_path_buf(),
|
||||
pyproject
|
||||
.as_ref()
|
||||
.and_then(|pyproject| pyproject.project.as_ref()),
|
||||
)
|
||||
.map_err(|err| {
|
||||
ProjectMetadataError::InvalidRequiresPythonConstraint {
|
||||
source: err,
|
||||
path: pyproject_path,
|
||||
}
|
||||
})?;
|
||||
|
||||
return Ok(metadata);
|
||||
}
|
||||
|
||||
if let Some(pyproject) = pyproject {
|
||||
let has_ty_section = pyproject.ty().is_some();
|
||||
let metadata =
|
||||
ProjectMetadata::from_pyproject(pyproject, project_root.to_path_buf())
|
||||
.map_err(
|
||||
|err| ProjectMetadataError::InvalidRequiresPythonConstraint {
|
||||
source: err,
|
||||
path: pyproject_path,
|
||||
},
|
||||
)?;
|
||||
|
||||
if has_ty_section {
|
||||
tracing::debug!("Found project at '{}'", project_root);
|
||||
|
||||
return Ok(discovered);
|
||||
return Ok(metadata);
|
||||
}
|
||||
DiscoveredProject::PyProject { .. } => {
|
||||
// Not a project itself, keep looking for an enclosing project.
|
||||
if closest_project.is_none() {
|
||||
closest_project = Some(discovered);
|
||||
}
|
||||
|
||||
// Not a project itself, keep looking for an enclosing project.
|
||||
if closest_project.is_none() {
|
||||
closest_project = Some(metadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(closest_project) = closest_project {
|
||||
// No project found, but maybe a pyproject.toml was found.
|
||||
let metadata = if let Some(closest_project) = closest_project {
|
||||
tracing::debug!(
|
||||
"Project without `tool.ty` section: '{}'",
|
||||
closest_project.root()
|
||||
);
|
||||
|
||||
return Ok(closest_project);
|
||||
}
|
||||
closest_project
|
||||
} else {
|
||||
tracing::debug!(
|
||||
"The ancestor directories contain no `pyproject.toml`. Falling back to a virtual project."
|
||||
);
|
||||
|
||||
tracing::debug!(
|
||||
"The ancestor directories contain no `pyproject.toml`. Falling back to a virtual project."
|
||||
);
|
||||
|
||||
// Create a project with a default configuration
|
||||
Ok(DiscoveredProject::PyProject {
|
||||
metadata: Self::new(
|
||||
// Create a project with a default configuration
|
||||
Self::new(
|
||||
path.file_name().unwrap_or("root").into(),
|
||||
path.to_path_buf(),
|
||||
),
|
||||
has_ty_section: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// Discovers a project in `project_root`. Unlike [`Self::discover`], this function
|
||||
/// only searches for a configuration in `project_root`
|
||||
/// without traversing the ancestor directories.
|
||||
fn discover_in(
|
||||
project_root: &SystemPath,
|
||||
system: &dyn System,
|
||||
) -> Result<Option<DiscoveredProject>, ProjectMetadataError> {
|
||||
if !system.is_directory(project_root) {
|
||||
return Err(ProjectMetadataError::NotADirectory(
|
||||
project_root.to_path_buf(),
|
||||
));
|
||||
}
|
||||
|
||||
let pyproject_path = project_root.join("pyproject.toml");
|
||||
|
||||
let pyproject = if let Ok(pyproject_str) = system.read_to_string(&pyproject_path) {
|
||||
match PyProject::from_toml_str(
|
||||
&pyproject_str,
|
||||
ValueSource::File(Arc::new(pyproject_path.clone())),
|
||||
) {
|
||||
Ok(pyproject) => Some(pyproject),
|
||||
Err(error) => {
|
||||
return Err(ProjectMetadataError::InvalidPyProject {
|
||||
path: pyproject_path,
|
||||
source: Box::new(error),
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
)
|
||||
};
|
||||
|
||||
// A `ty.toml` takes precedence over a `pyproject.toml`.
|
||||
let ty_toml_path = project_root.join("ty.toml");
|
||||
if let Ok(ty_str) = system.read_to_string(&ty_toml_path) {
|
||||
let options = match Options::from_toml_str(
|
||||
&ty_str,
|
||||
ValueSource::File(Arc::new(ty_toml_path.clone())),
|
||||
) {
|
||||
Ok(options) => options,
|
||||
Err(error) => {
|
||||
return Err(ProjectMetadataError::InvalidTyToml {
|
||||
path: ty_toml_path,
|
||||
source: Box::new(error),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if pyproject
|
||||
.as_ref()
|
||||
.is_some_and(|project| project.ty().is_some())
|
||||
{
|
||||
// TODO: Consider using a diagnostic here
|
||||
tracing::warn!(
|
||||
"Ignoring the `tool.ty` section in `{pyproject_path}` because `{ty_toml_path}` takes precedence."
|
||||
);
|
||||
}
|
||||
|
||||
tracing::debug!("Found project at '{}'", project_root);
|
||||
|
||||
let metadata = ProjectMetadata::from_options(
|
||||
options,
|
||||
project_root.to_path_buf(),
|
||||
pyproject
|
||||
.as_ref()
|
||||
.and_then(|pyproject| pyproject.project.as_ref()),
|
||||
)
|
||||
.map_err(
|
||||
|err| ProjectMetadataError::InvalidRequiresPythonConstraint {
|
||||
source: err,
|
||||
path: pyproject_path,
|
||||
},
|
||||
)?;
|
||||
|
||||
return Ok(Some(DiscoveredProject::Ty { metadata }));
|
||||
}
|
||||
|
||||
if let Some(pyproject) = pyproject {
|
||||
let has_ty_section = pyproject.ty().is_some();
|
||||
let metadata = ProjectMetadata::from_pyproject(pyproject, project_root.to_path_buf())
|
||||
.map_err(|err| {
|
||||
ProjectMetadataError::InvalidRequiresPythonConstraint {
|
||||
source: err,
|
||||
path: pyproject_path,
|
||||
}
|
||||
})?;
|
||||
|
||||
return Ok(Some(DiscoveredProject::PyProject {
|
||||
has_ty_section,
|
||||
metadata,
|
||||
}));
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Discovers all project in `root`, recursively.
|
||||
pub fn discover_all(
|
||||
root: &SystemPath,
|
||||
system: &dyn System,
|
||||
) -> Result<BTreeMap<SystemPathBuf, ProjectMetadata>, ProjectMetadataError> {
|
||||
tracing::debug!("Searching for all projects in '{root}'");
|
||||
|
||||
// FIXME: We need to know if it is a pyproject toml or not :(
|
||||
let root_project = Self::discover_closest(root, system)?;
|
||||
|
||||
let projects: BTreeMap<_, _> = [(root.to_path_buf(), root_project)].into_iter().collect();
|
||||
|
||||
// Hmm, what's complicated about this is that
|
||||
// `check_project` now descends into sub directories so that
|
||||
// a single file now belongs to multiple projects.
|
||||
// We would need to exclude the inner projects from the outer project,
|
||||
// but that seems annoying.
|
||||
// The alternative is that we skip a directory as soon as we've found a project. But that
|
||||
// still doesn't solve the case where we have sub folders that each are a project
|
||||
// with an outermost default database
|
||||
let projects = std::sync::Mutex::new(projects);
|
||||
|
||||
system
|
||||
.walk_directory(root)
|
||||
.standard_filters(true)
|
||||
.ignore_hidden(true)
|
||||
.run(|| {
|
||||
Box::new(|entry| {
|
||||
let entry = match entry {
|
||||
Ok(entry) => entry,
|
||||
Err(error) => {
|
||||
tracing::debug!("Failed to walk directory {error}'");
|
||||
return WalkState::Skip;
|
||||
}
|
||||
};
|
||||
|
||||
if entry.depth() == 0 {
|
||||
return WalkState::Continue;
|
||||
}
|
||||
|
||||
if !entry.file_type().is_directory() {
|
||||
return WalkState::Skip;
|
||||
}
|
||||
|
||||
// TODO: Do propage the error somehow
|
||||
let discovered = match Self::discover_in(entry.path(), system) {
|
||||
Ok(Some(discovered)) => discovered,
|
||||
Ok(None) => return WalkState::Continue,
|
||||
Err(error) => {
|
||||
tracing::debug!(
|
||||
"Failed to discover project in {path}: {error}",
|
||||
path = entry.path()
|
||||
);
|
||||
|
||||
return WalkState::Skip;
|
||||
}
|
||||
};
|
||||
|
||||
let mut projects = projects.lock().unwrap();
|
||||
|
||||
// Skip the project if there's an outer project that uses either a `ty.toml` or
|
||||
// `pyproject.toml` with a `tool.ty` section.
|
||||
if let Some((parent_path, parent_project)) = projects.range(..entry.path().to_path_buf()).last() {
|
||||
if parent_project.takes_priority_over(&discovered) {
|
||||
tracing::debug!("Ignoring project at {path} because the parent configuration at {parent_path} takes priority", path = entry.path());
|
||||
return WalkState::Continue;
|
||||
}
|
||||
}
|
||||
|
||||
projects.insert(entry.path().to_path_buf(), discovered);
|
||||
|
||||
WalkState::Continue
|
||||
})
|
||||
});
|
||||
|
||||
Ok(projects
|
||||
.into_inner()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|(path, discovered)| (path, discovered.into_metadata()))
|
||||
.collect())
|
||||
Ok(metadata)
|
||||
}
|
||||
|
||||
pub fn root(&self) -> &SystemPath {
|
||||
@@ -472,53 +344,6 @@ pub enum ProjectMetadataError {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum DiscoveredProject {
|
||||
PyProject {
|
||||
has_ty_section: bool,
|
||||
metadata: ProjectMetadata,
|
||||
},
|
||||
Ty {
|
||||
metadata: ProjectMetadata,
|
||||
},
|
||||
}
|
||||
|
||||
impl DiscoveredProject {
|
||||
fn is_ty_or_has_ty_seciton(&self) -> bool {
|
||||
match self {
|
||||
DiscoveredProject::PyProject {
|
||||
has_ty_section,
|
||||
metadata: _,
|
||||
} => *has_ty_section,
|
||||
DiscoveredProject::Ty { .. } => true,
|
||||
}
|
||||
}
|
||||
|
||||
fn takes_priority_over(&self, other: &DiscoveredProject) -> bool {
|
||||
self.is_ty_or_has_ty_seciton() && !other.is_ty_or_has_ty_seciton()
|
||||
}
|
||||
|
||||
fn root(&self) -> &SystemPath {
|
||||
match self {
|
||||
DiscoveredProject::PyProject {
|
||||
has_ty_section: _,
|
||||
metadata,
|
||||
} => &metadata.root(),
|
||||
DiscoveredProject::Ty { metadata } => metadata.root(),
|
||||
}
|
||||
}
|
||||
|
||||
fn into_metadata(self) -> ProjectMetadata {
|
||||
match self {
|
||||
DiscoveredProject::PyProject {
|
||||
has_ty_section: _,
|
||||
metadata,
|
||||
} => metadata,
|
||||
DiscoveredProject::Ty { metadata } => metadata,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
//! Integration tests for project discovery
|
||||
@@ -545,7 +370,7 @@ mod tests {
|
||||
|
||||
assert_eq!(project.root(), &*root);
|
||||
|
||||
with_sanitized_paths(|| {
|
||||
with_escaped_paths(|| {
|
||||
assert_ron_snapshot!(&project, @r#"
|
||||
ProjectMetadata(
|
||||
name: Name("app"),
|
||||
@@ -583,7 +408,7 @@ mod tests {
|
||||
|
||||
assert_eq!(project.root(), &*root);
|
||||
|
||||
with_sanitized_paths(|| {
|
||||
with_escaped_paths(|| {
|
||||
assert_ron_snapshot!(&project, @r#"
|
||||
ProjectMetadata(
|
||||
name: Name("backend"),
|
||||
@@ -675,7 +500,7 @@ unclosed table, expected `]`
|
||||
|
||||
let sub_project = ProjectMetadata::discover(&root.join("packages/a"), &system)?;
|
||||
|
||||
with_sanitized_paths(|| {
|
||||
with_escaped_paths(|| {
|
||||
assert_ron_snapshot!(sub_project, @r#"
|
||||
ProjectMetadata(
|
||||
name: Name("nested-project"),
|
||||
@@ -725,7 +550,7 @@ unclosed table, expected `]`
|
||||
|
||||
let root = ProjectMetadata::discover(&root, &system)?;
|
||||
|
||||
with_sanitized_paths(|| {
|
||||
with_escaped_paths(|| {
|
||||
assert_ron_snapshot!(root, @r#"
|
||||
ProjectMetadata(
|
||||
name: Name("project-root"),
|
||||
@@ -769,7 +594,7 @@ unclosed table, expected `]`
|
||||
|
||||
let sub_project = ProjectMetadata::discover(&root.join("packages/a"), &system)?;
|
||||
|
||||
with_sanitized_paths(|| {
|
||||
with_escaped_paths(|| {
|
||||
assert_ron_snapshot!(sub_project, @r#"
|
||||
ProjectMetadata(
|
||||
name: Name("nested-project"),
|
||||
@@ -812,7 +637,7 @@ unclosed table, expected `]`
|
||||
|
||||
let root = ProjectMetadata::discover(&root.join("packages/a"), &system)?;
|
||||
|
||||
with_sanitized_paths(|| {
|
||||
with_escaped_paths(|| {
|
||||
assert_ron_snapshot!(root, @r#"
|
||||
ProjectMetadata(
|
||||
name: Name("project-root"),
|
||||
@@ -864,7 +689,7 @@ unclosed table, expected `]`
|
||||
|
||||
let root = ProjectMetadata::discover(&root, &system)?;
|
||||
|
||||
with_sanitized_paths(|| {
|
||||
with_escaped_paths(|| {
|
||||
assert_ron_snapshot!(root, @r#"
|
||||
ProjectMetadata(
|
||||
name: Name("super-app"),
|
||||
@@ -883,266 +708,6 @@ unclosed table, expected `]`
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discover_all_nested_projects_with_ty_sections() -> anyhow::Result<()> {
|
||||
let system = TestSystem::default();
|
||||
let root = SystemPathBuf::from("/app");
|
||||
|
||||
system
|
||||
.memory_file_system()
|
||||
.write_files_all([
|
||||
(
|
||||
root.join("pyproject.toml"),
|
||||
r#"
|
||||
[project]
|
||||
name = "project-root"
|
||||
|
||||
[tool.ty.src]
|
||||
root = "src"
|
||||
"#,
|
||||
),
|
||||
(
|
||||
root.join("packages/a/pyproject.toml"),
|
||||
r#"
|
||||
[project]
|
||||
name = "nested-project"
|
||||
|
||||
[tool.ty.src]
|
||||
root = "src"
|
||||
"#,
|
||||
),
|
||||
])
|
||||
.context("Failed to write files")?;
|
||||
|
||||
let projects = ProjectMetadata::discover_all(&root, &system)?;
|
||||
|
||||
with_sanitized_paths(|| {
|
||||
assert_ron_snapshot!(projects, @r###"
|
||||
{
|
||||
"/app": ProjectMetadata(
|
||||
name: Name("project-root"),
|
||||
root: "/app",
|
||||
options: Options(
|
||||
src: Some(SrcOptions(
|
||||
root: Some("src"),
|
||||
)),
|
||||
),
|
||||
),
|
||||
"/app/packages/a": ProjectMetadata(
|
||||
name: Name("nested-project"),
|
||||
root: "/app/packages/a",
|
||||
options: Options(
|
||||
src: Some(SrcOptions(
|
||||
root: Some("src"),
|
||||
)),
|
||||
),
|
||||
),
|
||||
}
|
||||
"###);
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discover_all_nested_project_without_ty_section() -> anyhow::Result<()> {
|
||||
let system = TestSystem::default();
|
||||
let root = SystemPathBuf::from("/app");
|
||||
|
||||
system
|
||||
.memory_file_system()
|
||||
.write_files_all([
|
||||
(
|
||||
root.join("pyproject.toml"),
|
||||
r#"
|
||||
[project]
|
||||
name = "project-root"
|
||||
|
||||
[tool.ty.src]
|
||||
root = "src"
|
||||
"#,
|
||||
),
|
||||
(
|
||||
root.join("packages/a/pyproject.toml"),
|
||||
r#"
|
||||
[project]
|
||||
name = "nested-project"
|
||||
"#,
|
||||
),
|
||||
])
|
||||
.context("Failed to write files")?;
|
||||
|
||||
let projects = ProjectMetadata::discover_all(&root, &system)?;
|
||||
|
||||
with_sanitized_paths(|| {
|
||||
assert_ron_snapshot!(projects, @r###"
|
||||
{
|
||||
"/app": ProjectMetadata(
|
||||
name: Name("project-root"),
|
||||
root: "/app",
|
||||
options: Options(
|
||||
src: Some(SrcOptions(
|
||||
root: Some("src"),
|
||||
)),
|
||||
),
|
||||
),
|
||||
}
|
||||
"###);
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discover_all_nested_projects_with_ty_toml() -> anyhow::Result<()> {
|
||||
let system = TestSystem::default();
|
||||
let root = SystemPathBuf::from("/app");
|
||||
|
||||
system
|
||||
.memory_file_system()
|
||||
.write_files_all([
|
||||
(
|
||||
root.join("pyproject.toml"),
|
||||
r#"
|
||||
[project]
|
||||
name = "project-root"
|
||||
|
||||
[tool.ty.src]
|
||||
root = "src"
|
||||
"#,
|
||||
),
|
||||
(
|
||||
root.join("packages/a/ty.toml"),
|
||||
r#"
|
||||
[src]
|
||||
root = "src"
|
||||
"#,
|
||||
),
|
||||
])
|
||||
.context("Failed to write files")?;
|
||||
|
||||
let projects = ProjectMetadata::discover_all(&root, &system)?;
|
||||
|
||||
with_sanitized_paths(|| {
|
||||
assert_ron_snapshot!(projects, @r###"
|
||||
{
|
||||
"/app": ProjectMetadata(
|
||||
name: Name("project-root"),
|
||||
root: "/app",
|
||||
options: Options(
|
||||
src: Some(SrcOptions(
|
||||
root: Some("src"),
|
||||
)),
|
||||
),
|
||||
),
|
||||
"/app/packages/a": ProjectMetadata(
|
||||
name: Name("a"),
|
||||
root: "/app/packages/a",
|
||||
options: Options(
|
||||
src: Some(SrcOptions(
|
||||
root: Some("src"),
|
||||
)),
|
||||
),
|
||||
),
|
||||
}
|
||||
"###);
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discover_all_nested_without_ty_sections() -> anyhow::Result<()> {
|
||||
let system = TestSystem::default();
|
||||
let root = SystemPathBuf::from("/app");
|
||||
|
||||
system
|
||||
.memory_file_system()
|
||||
.write_files_all([
|
||||
(
|
||||
root.join("pyproject.toml"),
|
||||
r#"
|
||||
[project]
|
||||
name = "project-root"
|
||||
|
||||
[tool.ty]
|
||||
"#,
|
||||
),
|
||||
(
|
||||
root.join("packages/a/pyproject.toml"),
|
||||
r#"
|
||||
[project]
|
||||
name = "sub-project"
|
||||
"#,
|
||||
),
|
||||
])
|
||||
.context("Failed to write files")?;
|
||||
|
||||
let projects = ProjectMetadata::discover_all(&root, &system)?;
|
||||
|
||||
with_sanitized_paths(|| {
|
||||
assert_ron_snapshot!(projects, @r###"
|
||||
{
|
||||
"/app": ProjectMetadata(
|
||||
name: Name("project-root"),
|
||||
root: "/app",
|
||||
options: Options(),
|
||||
),
|
||||
}
|
||||
"###);
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discover_all_nested_all_projects_without_ty_sections() -> anyhow::Result<()> {
|
||||
let system = TestSystem::default();
|
||||
let root = SystemPathBuf::from("/app");
|
||||
|
||||
system
|
||||
.memory_file_system()
|
||||
.write_files_all([
|
||||
(
|
||||
root.join("pyproject.toml"),
|
||||
r#"
|
||||
[project]
|
||||
name = "project-root"
|
||||
"#,
|
||||
),
|
||||
(
|
||||
root.join("packages/a/pyproject.toml"),
|
||||
r#"
|
||||
[project]
|
||||
name = "sub-project"
|
||||
"#,
|
||||
),
|
||||
])
|
||||
.context("Failed to write files")?;
|
||||
|
||||
let projects = ProjectMetadata::discover_all(&root, &system)?;
|
||||
|
||||
with_sanitized_paths(|| {
|
||||
assert_ron_snapshot!(projects, @r###"
|
||||
{
|
||||
"/app": ProjectMetadata(
|
||||
name: Name("project-root"),
|
||||
root: "/app",
|
||||
options: Options(),
|
||||
),
|
||||
"/app/packages/a": ProjectMetadata(
|
||||
name: Name("sub-project"),
|
||||
root: "/app/packages/a",
|
||||
options: Options(),
|
||||
),
|
||||
}
|
||||
"###);
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requires_python_major_minor() -> anyhow::Result<()> {
|
||||
let system = TestSystem::default();
|
||||
@@ -1426,7 +991,7 @@ unclosed table, expected `]`
|
||||
assert_eq!(error.to_string().replace('\\', "/"), message);
|
||||
}
|
||||
|
||||
fn with_sanitized_paths<R>(f: impl FnOnce() -> R) -> R {
|
||||
fn with_escaped_paths<R>(f: impl FnOnce() -> R) -> R {
|
||||
let mut settings = insta::Settings::clone_current();
|
||||
settings.add_dynamic_redaction(".root", |content, _path| {
|
||||
content.as_str().unwrap().replace('\\', "/")
|
||||
|
||||
@@ -13,7 +13,6 @@ license = { workspace = true }
|
||||
[dependencies]
|
||||
ruff_db = { workspace = true }
|
||||
ruff_annotate_snippets = { workspace = true }
|
||||
ruff_diagnostics = { workspace = true }
|
||||
ruff_index = { workspace = true, features = ["salsa"] }
|
||||
ruff_macros = { workspace = true }
|
||||
ruff_memory_usage = { workspace = true }
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
try:
|
||||
type name_4 = name_1
|
||||
finally:
|
||||
from .. import name_3
|
||||
|
||||
try:
|
||||
pass
|
||||
except* 0:
|
||||
pass
|
||||
else:
|
||||
def name_1() -> name_4:
|
||||
pass
|
||||
|
||||
@name_1
|
||||
def name_3():
|
||||
pass
|
||||
finally:
|
||||
try:
|
||||
pass
|
||||
except* 0:
|
||||
assert name_3
|
||||
finally:
|
||||
|
||||
@name_3
|
||||
class name_1:
|
||||
pass
|
||||
@@ -1,5 +0,0 @@
|
||||
class foo[_: foo](object): ...
|
||||
|
||||
[_] = (foo,) = foo
|
||||
|
||||
def foo(): ...
|
||||
@@ -1,20 +0,0 @@
|
||||
name_3: Foo = 0
|
||||
name_4 = 0
|
||||
|
||||
if _0:
|
||||
type name_3 = name_5
|
||||
type name_4 = name_3
|
||||
|
||||
_1: name_3
|
||||
|
||||
def name_1(_2: name_4):
|
||||
pass
|
||||
|
||||
match 0:
|
||||
case name_1._3:
|
||||
pass
|
||||
case 1:
|
||||
type name_5 = name_4
|
||||
case name_5:
|
||||
pass
|
||||
name_3 = name_5
|
||||
@@ -35,8 +35,6 @@ bad_nesting: Literal[LiteralString] # error: [invalid-type-form]
|
||||
|
||||
`LiteralString` cannot be parameterized.
|
||||
|
||||
<!-- snapshot-diagnostics -->
|
||||
|
||||
```py
|
||||
from typing_extensions import LiteralString
|
||||
|
||||
@@ -44,6 +42,7 @@ from typing_extensions import LiteralString
|
||||
a: LiteralString[str]
|
||||
|
||||
# error: [invalid-type-form]
|
||||
# error: [unresolved-reference] "Name `foo` used when not defined"
|
||||
b: LiteralString["foo"]
|
||||
```
|
||||
|
||||
|
||||
@@ -222,10 +222,10 @@ reveal_type(r) # revealed: dict[int | str, int | str]
|
||||
## Incorrect collection literal assignments are complained about
|
||||
|
||||
```py
|
||||
# error: [invalid-assignment] "Object of type `list[str | int]` is not assignable to `list[str]`"
|
||||
# error: [invalid-assignment] "Object of type `list[Unknown | int]` is not assignable to `list[str]`"
|
||||
a: list[str] = [1, 2, 3]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `set[int | str]` is not assignable to `set[int]`"
|
||||
# error: [invalid-assignment] "Object of type `set[Unknown | int | str]` is not assignable to `set[int]`"
|
||||
b: set[int] = {1, 2, "3"}
|
||||
```
|
||||
|
||||
@@ -422,7 +422,7 @@ reveal_type(d) # revealed: list[int | tuple[int, int]]
|
||||
e: list[int] = f(True)
|
||||
reveal_type(e) # revealed: list[int]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `list[int | str]` is not assignable to `list[int]`"
|
||||
# error: [invalid-assignment] "Object of type `list[str]` is not assignable to `list[int]`"
|
||||
g: list[int] = f("a")
|
||||
|
||||
# error: [invalid-assignment] "Object of type `list[str]` is not assignable to `tuple[int]`"
|
||||
@@ -459,12 +459,12 @@ reveal_type(b) # revealed: TD
|
||||
|
||||
# error: [missing-typed-dict-key] "Missing required key 'x' in TypedDict `TD` constructor"
|
||||
# error: [invalid-key] "Unknown key "y" for TypedDict `TD`"
|
||||
# error: [invalid-assignment] "Object of type `TD | dict[Unknown | str, Unknown | int]` is not assignable to `TD`"
|
||||
# error: [invalid-assignment] "Object of type `Unknown | dict[Unknown | str, Unknown | int]` is not assignable to `TD`"
|
||||
c: TD = f([{"y": 0}, {"x": 1}])
|
||||
|
||||
# error: [missing-typed-dict-key] "Missing required key 'x' in TypedDict `TD` constructor"
|
||||
# error: [invalid-key] "Unknown key "y" for TypedDict `TD`"
|
||||
# error: [invalid-assignment] "Object of type `TD | None | dict[Unknown | str, Unknown | int]` is not assignable to `TD | None`"
|
||||
# error: [invalid-assignment] "Object of type `Unknown | dict[Unknown | str, Unknown | int]` is not assignable to `TD | None`"
|
||||
c: TD | None = f([{"y": 0}, {"x": 1}])
|
||||
```
|
||||
|
||||
|
||||
@@ -79,9 +79,8 @@ async def main():
|
||||
task("B"),
|
||||
)
|
||||
|
||||
# TODO: these should be `int`
|
||||
reveal_type(a) # revealed: Unknown
|
||||
reveal_type(b) # revealed: Unknown
|
||||
reveal_type(a) # revealed: int
|
||||
reveal_type(b) # revealed: int
|
||||
```
|
||||
|
||||
## Under the hood
|
||||
|
||||
@@ -2383,34 +2383,6 @@ class B:
|
||||
|
||||
reveal_type(B().x) # revealed: Unknown | Literal[1]
|
||||
reveal_type(A().x) # revealed: Unknown | Literal[1]
|
||||
|
||||
class Base:
|
||||
def flip(self) -> "Sub":
|
||||
return Sub()
|
||||
|
||||
class Sub(Base):
|
||||
# error: [invalid-method-override]
|
||||
def flip(self) -> "Base":
|
||||
return Base()
|
||||
|
||||
class C2:
|
||||
def __init__(self, x: Sub):
|
||||
self.x = x
|
||||
|
||||
def replace_with(self, other: "C2"):
|
||||
self.x = other.x.flip()
|
||||
|
||||
reveal_type(C2(Sub()).x) # revealed: Unknown | Base
|
||||
|
||||
class C3:
|
||||
def __init__(self, x: Sub):
|
||||
self.x = [x]
|
||||
|
||||
def replace_with(self, other: "C3"):
|
||||
self.x = [self.x[0].flip()]
|
||||
|
||||
# TODO: should be `Unknown | list[Unknown | Sub] | list[Unknown | Base]`
|
||||
reveal_type(C3(Sub()).x) # revealed: Unknown | list[Unknown | Sub] | list[Divergent]
|
||||
```
|
||||
|
||||
And cycles between many attributes:
|
||||
@@ -2460,30 +2432,6 @@ class ManyCycles:
|
||||
reveal_type(self.x5) # revealed: Unknown | int
|
||||
reveal_type(self.x6) # revealed: Unknown | int
|
||||
reveal_type(self.x7) # revealed: Unknown | int
|
||||
|
||||
class ManyCycles2:
|
||||
def __init__(self: "ManyCycles2"):
|
||||
self.x1 = [0]
|
||||
self.x2 = [1]
|
||||
self.x3 = [1]
|
||||
|
||||
def f1(self: "ManyCycles2"):
|
||||
# TODO: should be Unknown | list[Unknown | int] | list[Divergent]
|
||||
reveal_type(self.x3) # revealed: Unknown | list[Unknown | int] | list[Divergent] | list[Divergent]
|
||||
|
||||
self.x1 = [self.x2] + [self.x3]
|
||||
self.x2 = [self.x1] + [self.x3]
|
||||
self.x3 = [self.x1] + [self.x2]
|
||||
|
||||
def f2(self: "ManyCycles2"):
|
||||
self.x1 = self.x2 + self.x3
|
||||
self.x2 = self.x1 + self.x3
|
||||
self.x3 = self.x1 + self.x2
|
||||
|
||||
def f3(self: "ManyCycles2"):
|
||||
self.x1 = self.x2 + self.x3
|
||||
self.x2 = self.x1 + self.x3
|
||||
self.x3 = self.x1 + self.x2
|
||||
```
|
||||
|
||||
This case additionally tests our union/intersection simplification logic:
|
||||
@@ -2663,18 +2611,12 @@ reveal_type(Answer.__members__) # revealed: MappingProxyType[str, Unknown]
|
||||
|
||||
## Divergent inferred implicit instance attribute types
|
||||
|
||||
If an implicit attribute is defined recursively and type inference diverges, the divergent part is
|
||||
filled in with the dynamic type `Divergent`. Types containing `Divergent` can be seen as "cheap"
|
||||
recursive types: they are not true recursive types based on recursive type theory, so no unfolding
|
||||
is performed when you use them.
|
||||
|
||||
```py
|
||||
class C:
|
||||
def f(self, other: "C"):
|
||||
self.x = (other.x, 1)
|
||||
|
||||
reveal_type(C().x) # revealed: Unknown | tuple[Divergent, Literal[1]]
|
||||
reveal_type(C().x[0]) # revealed: Unknown | Divergent
|
||||
```
|
||||
|
||||
This also works if the tuple is not constructed directly:
|
||||
@@ -2713,11 +2655,11 @@ And it also works for homogeneous tuples:
|
||||
def make_homogeneous_tuple(x: T) -> tuple[T, ...]:
|
||||
return (x, x)
|
||||
|
||||
class F:
|
||||
def f(self, other: "F"):
|
||||
class E:
|
||||
def f(self, other: "E"):
|
||||
self.x = make_homogeneous_tuple(other.x)
|
||||
|
||||
reveal_type(F().x) # revealed: Unknown | tuple[Divergent, ...]
|
||||
reveal_type(E().x) # revealed: Unknown | tuple[Divergent, ...]
|
||||
```
|
||||
|
||||
## Attributes of standard library modules that aren't yet defined
|
||||
|
||||
@@ -268,8 +268,8 @@ class A:
|
||||
|
||||
A(f(1))
|
||||
|
||||
# error: [invalid-argument-type] "Argument to function `__new__` is incorrect: Expected `list[int | str]`, found `list[int | None | list[Unknown]] & list[int | str | list[Unknown]] & list[list[Unknown]]`"
|
||||
# error: [invalid-argument-type] "Argument to bound method `__init__` is incorrect: Expected `list[int | None]`, found `list[int | None | list[Unknown]] & list[int | str | list[Unknown]] & list[list[Unknown]]`"
|
||||
# error: [invalid-argument-type] "Argument to function `__new__` is incorrect: Expected `list[int | str]`, found `list[list[Unknown]]`"
|
||||
# error: [invalid-argument-type] "Argument to bound method `__init__` is incorrect: Expected `list[int | None]`, found `list[list[Unknown]]`"
|
||||
A(f([]))
|
||||
```
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ type("Foo", Base, {})
|
||||
# error: [invalid-argument-type] "Argument to class `type` is incorrect: Expected `tuple[type, ...]`, found `tuple[Literal[1], Literal[2]]`"
|
||||
type("Foo", (1, 2), {})
|
||||
|
||||
# error: [invalid-argument-type] "Argument to class `type` is incorrect: Expected `dict[str, Any]`, found `dict[str | bytes, Any]`"
|
||||
# error: [invalid-argument-type] "Argument to class `type` is incorrect: Expected `dict[str, Any]`, found `dict[Unknown | bytes, Unknown | int]`"
|
||||
type("Foo", (Base,), {b"attr": 1})
|
||||
```
|
||||
|
||||
|
||||
@@ -277,6 +277,6 @@ def _(flag: bool):
|
||||
x = f({"x": 1})
|
||||
reveal_type(x) # revealed: int
|
||||
|
||||
# error: [invalid-argument-type] "Argument to function `f` is incorrect: Expected `T`, found `dict[str, int] & dict[Unknown | str, Unknown | int]`"
|
||||
# error: [invalid-argument-type] "Argument to function `f` is incorrect: Expected `T`, found `dict[Unknown | str, Unknown | int]`"
|
||||
f({"y": 1})
|
||||
```
|
||||
|
||||
@@ -32,39 +32,6 @@ reveal_type(p.x) # revealed: Unknown | int
|
||||
reveal_type(p.y) # revealed: Unknown | int
|
||||
```
|
||||
|
||||
## Self-referential bare type alias
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python-version = "3.12" # typing.TypeAliasType
|
||||
```
|
||||
|
||||
```py
|
||||
from typing import Union, TypeAliasType, Sequence, Mapping
|
||||
|
||||
A = list["A" | None]
|
||||
|
||||
def f(x: A):
|
||||
# TODO: should be `list[A | None]`?
|
||||
reveal_type(x) # revealed: list[Divergent]
|
||||
# TODO: should be `A | None`?
|
||||
reveal_type(x[0]) # revealed: Divergent
|
||||
|
||||
JSONPrimitive = Union[str, int, float, bool, None]
|
||||
JSONValue = TypeAliasType("JSONValue", 'Union[JSONPrimitive, Sequence["JSONValue"], Mapping[str, "JSONValue"]]')
|
||||
```
|
||||
|
||||
## Self-referential legacy type variables
|
||||
|
||||
```py
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
B = TypeVar("B", bound="Base")
|
||||
|
||||
class Base(Generic[B]):
|
||||
pass
|
||||
```
|
||||
|
||||
## Parameter default values
|
||||
|
||||
This is a regression test for <https://github.com/astral-sh/ty/issues/1402>. When a parameter has a
|
||||
|
||||
@@ -145,8 +145,8 @@ reveal_type(C[Literal[5]]()) # revealed: C[Literal[5]]
|
||||
The specialization must match the generic types:
|
||||
|
||||
```py
|
||||
# error: [invalid-type-arguments] "Too many type arguments to class `C`: expected 1, got 2"
|
||||
reveal_type(C[int, int]()) # revealed: C[Unknown]
|
||||
# error: [too-many-positional-arguments] "Too many positional arguments to class `C`: expected 1, got 2"
|
||||
reveal_type(C[int, int]()) # revealed: Unknown
|
||||
```
|
||||
|
||||
If the type variable has an upper bound, the specialized type must satisfy that bound:
|
||||
@@ -164,11 +164,13 @@ class IntSubclass(int): ...
|
||||
reveal_type(Bounded[int]()) # revealed: Bounded[int]
|
||||
reveal_type(Bounded[IntSubclass]()) # revealed: Bounded[IntSubclass]
|
||||
|
||||
# error: [invalid-type-arguments] "Type `str` is not assignable to upper bound `int` of type variable `BoundedT@Bounded`"
|
||||
reveal_type(Bounded[str]()) # revealed: Bounded[Unknown]
|
||||
# TODO: update this diagnostic to talk about type parameters and specializations
|
||||
# error: [invalid-argument-type] "Argument to class `Bounded` is incorrect: Expected `int`, found `str`"
|
||||
reveal_type(Bounded[str]()) # revealed: Unknown
|
||||
|
||||
# error: [invalid-type-arguments] "Type `int | str` is not assignable to upper bound `int` of type variable `BoundedT@Bounded`"
|
||||
reveal_type(Bounded[int | str]()) # revealed: Bounded[Unknown]
|
||||
# TODO: update this diagnostic to talk about type parameters and specializations
|
||||
# error: [invalid-argument-type] "Argument to class `Bounded` is incorrect: Expected `int`, found `int | str`"
|
||||
reveal_type(Bounded[int | str]()) # revealed: Unknown
|
||||
|
||||
reveal_type(BoundedByUnion[int]()) # revealed: BoundedByUnion[int]
|
||||
reveal_type(BoundedByUnion[IntSubclass]()) # revealed: BoundedByUnion[IntSubclass]
|
||||
@@ -195,8 +197,9 @@ reveal_type(Constrained[str]()) # revealed: Constrained[str]
|
||||
# TODO: revealed: Unknown
|
||||
reveal_type(Constrained[int | str]()) # revealed: Constrained[int | str]
|
||||
|
||||
# error: [invalid-type-arguments] "Type `object` does not satisfy constraints `int`, `str` of type variable `ConstrainedT@Constrained`"
|
||||
reveal_type(Constrained[object]()) # revealed: Constrained[Unknown]
|
||||
# TODO: update this diagnostic to talk about type parameters and specializations
|
||||
# error: [invalid-argument-type] "Argument to class `Constrained` is incorrect: Expected `int | str`, found `object`"
|
||||
reveal_type(Constrained[object]()) # revealed: Unknown
|
||||
```
|
||||
|
||||
If the type variable has a default, it can be omitted:
|
||||
@@ -270,7 +273,7 @@ class C(Generic[T]):
|
||||
|
||||
reveal_type(C(1)) # revealed: C[int]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
|
||||
# error: [invalid-assignment] "Object of type `C[str]` is not assignable to `C[int]`"
|
||||
wrong_innards: C[int] = C("five")
|
||||
```
|
||||
|
||||
@@ -286,7 +289,7 @@ class C(Generic[T]):
|
||||
|
||||
reveal_type(C(1)) # revealed: C[int]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
|
||||
# error: [invalid-assignment] "Object of type `C[str]` is not assignable to `C[int]`"
|
||||
wrong_innards: C[int] = C("five")
|
||||
```
|
||||
|
||||
@@ -305,7 +308,7 @@ class C(Generic[T]):
|
||||
|
||||
reveal_type(C(1)) # revealed: C[int]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
|
||||
# error: [invalid-assignment] "Object of type `C[str]` is not assignable to `C[int]`"
|
||||
wrong_innards: C[int] = C("five")
|
||||
```
|
||||
|
||||
@@ -324,7 +327,7 @@ class C(Generic[T]):
|
||||
|
||||
reveal_type(C(1)) # revealed: C[int]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
|
||||
# error: [invalid-assignment] "Object of type `C[str]` is not assignable to `C[int]`"
|
||||
wrong_innards: C[int] = C("five")
|
||||
|
||||
class D(Generic[T]):
|
||||
@@ -335,7 +338,7 @@ class D(Generic[T]):
|
||||
|
||||
reveal_type(D(1)) # revealed: D[int]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `D[int | str]` is not assignable to `D[int]`"
|
||||
# error: [invalid-assignment] "Object of type `D[str]` is not assignable to `D[int]`"
|
||||
wrong_innards: D[int] = D("five")
|
||||
```
|
||||
|
||||
@@ -451,7 +454,7 @@ reveal_type(C(1, 1)) # revealed: C[int]
|
||||
reveal_type(C(1, "string")) # revealed: C[int]
|
||||
reveal_type(C(1, True)) # revealed: C[int]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
|
||||
# error: [invalid-assignment] "Object of type `C[str]` is not assignable to `C[int]`"
|
||||
wrong_innards: C[int] = C("five", 1)
|
||||
```
|
||||
|
||||
@@ -729,14 +732,6 @@ class Base(Generic[T]): ...
|
||||
class Sub(Base["Sub"]): ...
|
||||
|
||||
reveal_type(Sub) # revealed: <class 'Sub'>
|
||||
|
||||
U = TypeVar("U")
|
||||
|
||||
class Base2(Generic[T, U]): ...
|
||||
|
||||
# TODO: no error
|
||||
# error: [unsupported-base] "Unsupported class base with type `<class 'Base2[Sub2, U@Sub2]'> | <class 'Base2[Sub2[Unknown], U@Sub2]'>`"
|
||||
class Sub2(Base2["Sub2", U]): ...
|
||||
```
|
||||
|
||||
#### Without string forward references
|
||||
@@ -761,8 +756,6 @@ from typing_extensions import Generic, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
# TODO: no error "Unsupported class base with type `<class 'list[Derived[T@Derived]]'> | <class 'list[@Todo]'>`"
|
||||
# error: [unsupported-base]
|
||||
class Derived(list[Derived[T]], Generic[T]): ...
|
||||
```
|
||||
|
||||
|
||||
@@ -444,22 +444,6 @@ class G(Generic[T]):
|
||||
reveal_type(G[list[G]]().x) # revealed: list[G[Unknown]]
|
||||
```
|
||||
|
||||
An invalid specialization in a recursive bound doesn't cause a panic:
|
||||
|
||||
```py
|
||||
from typing import TypeVar, Generic
|
||||
|
||||
# error: [invalid-type-arguments]
|
||||
T = TypeVar("T", bound="Node[int]")
|
||||
|
||||
class Node(Generic[T]):
|
||||
pass
|
||||
|
||||
# error: [invalid-type-arguments]
|
||||
def _(n: Node[str]):
|
||||
reveal_type(n) # revealed: Node[Unknown]
|
||||
```
|
||||
|
||||
### Defaults
|
||||
|
||||
```toml
|
||||
|
||||
@@ -61,8 +61,8 @@ def _(a: C[int], b: C[Literal[5]]):
|
||||
The specialization must match the generic types:
|
||||
|
||||
```py
|
||||
# error: [invalid-type-arguments] "Too many type arguments: expected 1, got 2"
|
||||
reveal_type(C[int, int]) # revealed: C[Unknown]
|
||||
# error: [too-many-positional-arguments] "Too many positional arguments: expected 1, got 2"
|
||||
reveal_type(C[int, int]) # revealed: Unknown
|
||||
```
|
||||
|
||||
And non-generic types cannot be specialized:
|
||||
@@ -88,11 +88,13 @@ class IntSubclass(int): ...
|
||||
reveal_type(Bounded[int]) # revealed: Bounded[int]
|
||||
reveal_type(Bounded[IntSubclass]) # revealed: Bounded[IntSubclass]
|
||||
|
||||
# error: [invalid-type-arguments] "Type `str` is not assignable to upper bound `int` of type variable `T@Bounded`"
|
||||
reveal_type(Bounded[str]) # revealed: Bounded[Unknown]
|
||||
# TODO: update this diagnostic to talk about type parameters and specializations
|
||||
# error: [invalid-argument-type] "Argument is incorrect: Expected `int`, found `str`"
|
||||
reveal_type(Bounded[str]) # revealed: Unknown
|
||||
|
||||
# error: [invalid-type-arguments] "Type `int | str` is not assignable to upper bound `int` of type variable `T@Bounded`"
|
||||
reveal_type(Bounded[int | str]) # revealed: Bounded[Unknown]
|
||||
# TODO: update this diagnostic to talk about type parameters and specializations
|
||||
# error: [invalid-argument-type] "Argument is incorrect: Expected `int`, found `int | str`"
|
||||
reveal_type(Bounded[int | str]) # revealed: Unknown
|
||||
|
||||
reveal_type(BoundedByUnion[int]) # revealed: BoundedByUnion[int]
|
||||
reveal_type(BoundedByUnion[IntSubclass]) # revealed: BoundedByUnion[IntSubclass]
|
||||
@@ -117,8 +119,9 @@ reveal_type(Constrained[str]) # revealed: Constrained[str]
|
||||
# TODO: revealed: Unknown
|
||||
reveal_type(Constrained[int | str]) # revealed: Constrained[int | str]
|
||||
|
||||
# error: [invalid-type-arguments] "Type `object` does not satisfy constraints `int`, `str` of type variable `T@Constrained`"
|
||||
reveal_type(Constrained[object]) # revealed: Constrained[Unknown]
|
||||
# TODO: update this diagnostic to talk about type parameters and specializations
|
||||
# error: [invalid-argument-type] "Argument is incorrect: Expected `int | str`, found `object`"
|
||||
reveal_type(Constrained[object]) # revealed: Unknown
|
||||
```
|
||||
|
||||
If the type variable has a default, it can be omitted:
|
||||
|
||||
@@ -135,8 +135,8 @@ reveal_type(C[Literal[5]]()) # revealed: C[Literal[5]]
|
||||
The specialization must match the generic types:
|
||||
|
||||
```py
|
||||
# error: [invalid-type-arguments] "Too many type arguments to class `C`: expected 1, got 2"
|
||||
reveal_type(C[int, int]()) # revealed: C[Unknown]
|
||||
# error: [too-many-positional-arguments] "Too many positional arguments to class `C`: expected 1, got 2"
|
||||
reveal_type(C[int, int]()) # revealed: Unknown
|
||||
```
|
||||
|
||||
If the type variable has an upper bound, the specialized type must satisfy that bound:
|
||||
@@ -149,11 +149,13 @@ class IntSubclass(int): ...
|
||||
reveal_type(Bounded[int]()) # revealed: Bounded[int]
|
||||
reveal_type(Bounded[IntSubclass]()) # revealed: Bounded[IntSubclass]
|
||||
|
||||
# error: [invalid-type-arguments] "Type `str` is not assignable to upper bound `int` of type variable `T@Bounded`"
|
||||
reveal_type(Bounded[str]()) # revealed: Bounded[Unknown]
|
||||
# TODO: update this diagnostic to talk about type parameters and specializations
|
||||
# error: [invalid-argument-type] "Argument to class `Bounded` is incorrect: Expected `int`, found `str`"
|
||||
reveal_type(Bounded[str]()) # revealed: Unknown
|
||||
|
||||
# error: [invalid-type-arguments] "Type `int | str` is not assignable to upper bound `int` of type variable `T@Bounded`"
|
||||
reveal_type(Bounded[int | str]()) # revealed: Bounded[Unknown]
|
||||
# TODO: update this diagnostic to talk about type parameters and specializations
|
||||
# error: [invalid-argument-type] "Argument to class `Bounded` is incorrect: Expected `int`, found `int | str`"
|
||||
reveal_type(Bounded[int | str]()) # revealed: Unknown
|
||||
|
||||
reveal_type(BoundedByUnion[int]()) # revealed: BoundedByUnion[int]
|
||||
reveal_type(BoundedByUnion[IntSubclass]()) # revealed: BoundedByUnion[IntSubclass]
|
||||
@@ -178,8 +180,9 @@ reveal_type(Constrained[str]()) # revealed: Constrained[str]
|
||||
# TODO: revealed: Unknown
|
||||
reveal_type(Constrained[int | str]()) # revealed: Constrained[int | str]
|
||||
|
||||
# error: [invalid-type-arguments] "Type `object` does not satisfy constraints `int`, `str` of type variable `T@Constrained`"
|
||||
reveal_type(Constrained[object]()) # revealed: Constrained[Unknown]
|
||||
# TODO: update this diagnostic to talk about type parameters and specializations
|
||||
# error: [invalid-argument-type] "Argument to class `Constrained` is incorrect: Expected `int | str`, found `object`"
|
||||
reveal_type(Constrained[object]()) # revealed: Unknown
|
||||
```
|
||||
|
||||
If the type variable has a default, it can be omitted:
|
||||
@@ -246,7 +249,7 @@ class C[T]:
|
||||
|
||||
reveal_type(C(1)) # revealed: C[int]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
|
||||
# error: [invalid-assignment] "Object of type `C[str]` is not assignable to `C[int]`"
|
||||
wrong_innards: C[int] = C("five")
|
||||
```
|
||||
|
||||
@@ -260,7 +263,7 @@ class C[T]:
|
||||
|
||||
reveal_type(C(1)) # revealed: C[int]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
|
||||
# error: [invalid-assignment] "Object of type `C[str]` is not assignable to `C[int]`"
|
||||
wrong_innards: C[int] = C("five")
|
||||
```
|
||||
|
||||
@@ -277,7 +280,7 @@ class C[T]:
|
||||
|
||||
reveal_type(C(1)) # revealed: C[int]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
|
||||
# error: [invalid-assignment] "Object of type `C[str]` is not assignable to `C[int]`"
|
||||
wrong_innards: C[int] = C("five")
|
||||
```
|
||||
|
||||
@@ -294,7 +297,7 @@ class C[T]:
|
||||
|
||||
reveal_type(C(1)) # revealed: C[int]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
|
||||
# error: [invalid-assignment] "Object of type `C[str]` is not assignable to `C[int]`"
|
||||
wrong_innards: C[int] = C("five")
|
||||
|
||||
class D[T]:
|
||||
@@ -307,7 +310,7 @@ class D[T]:
|
||||
|
||||
reveal_type(D(1)) # revealed: D[int]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `D[int | str]` is not assignable to `D[int]`"
|
||||
# error: [invalid-assignment] "Object of type `D[str]` is not assignable to `D[int]`"
|
||||
wrong_innards: D[int] = D("five")
|
||||
```
|
||||
|
||||
@@ -392,7 +395,7 @@ reveal_type(C(1, 1)) # revealed: C[int]
|
||||
reveal_type(C(1, "string")) # revealed: C[int]
|
||||
reveal_type(C(1, True)) # revealed: C[int]
|
||||
|
||||
# error: [invalid-assignment] "Object of type `C[int | str]` is not assignable to `C[int]`"
|
||||
# error: [invalid-assignment] "Object of type `C[str]` is not assignable to `C[int]`"
|
||||
wrong_innards: C[int] = C("five", 1)
|
||||
```
|
||||
|
||||
|
||||
@@ -834,18 +834,6 @@ class G[T: list[G]]:
|
||||
reveal_type(G[list[G]]().x) # revealed: list[G[Unknown]]
|
||||
```
|
||||
|
||||
An invalid specialization in a recursive bound doesn't cause a panic:
|
||||
|
||||
```py
|
||||
# error: [invalid-type-arguments]
|
||||
class Node[T: "Node[int]"]:
|
||||
pass
|
||||
|
||||
# error: [invalid-type-arguments]
|
||||
def _(n: Node[str]):
|
||||
reveal_type(n) # revealed: Node[Unknown]
|
||||
```
|
||||
|
||||
### Defaults
|
||||
|
||||
Defaults can be generic, but can only refer to earlier typevars:
|
||||
|
||||
@@ -191,13 +191,13 @@ def _(
|
||||
reveal_type(int_or_callable) # revealed: int | ((str, /) -> bytes)
|
||||
reveal_type(callable_or_int) # revealed: ((str, /) -> bytes) | int
|
||||
# TODO should be Unknown | int
|
||||
reveal_type(type_var_or_int) # revealed: typing.TypeVar | int
|
||||
reveal_type(type_var_or_int) # revealed: T@TypeVarOrInt | int
|
||||
# TODO should be int | Unknown
|
||||
reveal_type(int_or_type_var) # revealed: int | typing.TypeVar
|
||||
reveal_type(int_or_type_var) # revealed: int | T@IntOrTypeVar
|
||||
# TODO should be Unknown | None
|
||||
reveal_type(type_var_or_none) # revealed: typing.TypeVar | None
|
||||
reveal_type(type_var_or_none) # revealed: T@TypeVarOrNone | None
|
||||
# TODO should be None | Unknown
|
||||
reveal_type(none_or_type_var) # revealed: None | typing.TypeVar
|
||||
reveal_type(none_or_type_var) # revealed: None | T@NoneOrTypeVar
|
||||
```
|
||||
|
||||
If a type is unioned with itself in a value expression, the result is just that type. No
|
||||
@@ -366,7 +366,9 @@ def g(obj: Y):
|
||||
reveal_type(obj) # revealed: list[int | str]
|
||||
```
|
||||
|
||||
## Generic types
|
||||
## Generic implicit type aliases
|
||||
|
||||
### Functionality
|
||||
|
||||
Implicit type aliases can also be generic:
|
||||
|
||||
@@ -388,24 +390,25 @@ ListOrTuple = list[T] | tuple[T, ...]
|
||||
ListOrTupleLegacy = Union[list[T], tuple[T, ...]]
|
||||
MyCallable = Callable[P, T]
|
||||
AnnotatedType = Annotated[T, "tag"]
|
||||
TransparentAlias = T
|
||||
MyOptional = T | None
|
||||
|
||||
# TODO: Consider displaying this as `<class 'list[T]'>`, … instead? (and similar for some others below)
|
||||
reveal_type(MyList) # revealed: <class 'list[typing.TypeVar]'>
|
||||
reveal_type(MyDict) # revealed: <class 'dict[typing.TypeVar, typing.TypeVar]'>
|
||||
reveal_type(MyList) # revealed: <class 'list[T@MyList]'>
|
||||
reveal_type(MyDict) # revealed: <class 'dict[T@MyDict, U@MyDict]'>
|
||||
reveal_type(MyType) # revealed: GenericAlias
|
||||
reveal_type(IntAndType) # revealed: <class 'tuple[int, typing.TypeVar]'>
|
||||
reveal_type(Pair) # revealed: <class 'tuple[typing.TypeVar, typing.TypeVar]'>
|
||||
reveal_type(Sum) # revealed: <class 'tuple[typing.TypeVar, typing.TypeVar]'>
|
||||
reveal_type(IntAndType) # revealed: <class 'tuple[int, T@IntAndType]'>
|
||||
reveal_type(Pair) # revealed: <class 'tuple[T@Pair, T@Pair]'>
|
||||
reveal_type(Sum) # revealed: <class 'tuple[T@Sum, U@Sum]'>
|
||||
reveal_type(ListOrTuple) # revealed: types.UnionType
|
||||
reveal_type(ListOrTupleLegacy) # revealed: types.UnionType
|
||||
reveal_type(MyCallable) # revealed: GenericAlias
|
||||
reveal_type(AnnotatedType) # revealed: <typing.Annotated special form>
|
||||
reveal_type(TransparentAlias) # revealed: typing.TypeVar
|
||||
reveal_type(MyOptional) # revealed: types.UnionType
|
||||
|
||||
def _(
|
||||
list_of_ints: MyList[int],
|
||||
dict_str_to_int: MyDict[str, int],
|
||||
# TODO: no error here
|
||||
# error: [invalid-type-form] "`typing.TypeVar` is not a generic class"
|
||||
subclass_of_int: MyType[int],
|
||||
int_and_str: IntAndType[str],
|
||||
pair_of_ints: Pair[int],
|
||||
@@ -413,48 +416,40 @@ def _(
|
||||
list_or_tuple: ListOrTuple[int],
|
||||
list_or_tuple_legacy: ListOrTupleLegacy[int],
|
||||
# TODO: no error here
|
||||
# error: [too-many-positional-arguments] "Too many positional arguments: expected 1, got 2"
|
||||
# error: [invalid-type-form] "List literals are not allowed in this context in a type expression: Did you mean `tuple[str, bytes]`?"
|
||||
my_callable: MyCallable[[str, bytes], int],
|
||||
annotated_int: AnnotatedType[int],
|
||||
transparent_alias: TransparentAlias[int],
|
||||
optional_int: MyOptional[int],
|
||||
):
|
||||
# TODO: This should be `list[int]`
|
||||
reveal_type(list_of_ints) # revealed: @Todo(specialized generic alias in type expression)
|
||||
# TODO: This should be `dict[str, int]`
|
||||
reveal_type(dict_str_to_int) # revealed: @Todo(specialized generic alias in type expression)
|
||||
# TODO: This should be `type[int]`
|
||||
reveal_type(subclass_of_int) # revealed: Unknown
|
||||
# TODO: This should be `tuple[int, str]`
|
||||
reveal_type(int_and_str) # revealed: @Todo(specialized generic alias in type expression)
|
||||
# TODO: This should be `tuple[int, int]`
|
||||
reveal_type(pair_of_ints) # revealed: @Todo(specialized generic alias in type expression)
|
||||
# TODO: This should be `tuple[int, bytes]`
|
||||
reveal_type(int_and_bytes) # revealed: @Todo(specialized generic alias in type expression)
|
||||
# TODO: This should be `list[int] | tuple[int, ...]`
|
||||
reveal_type(list_or_tuple) # revealed: @Todo(Generic specialization of types.UnionType)
|
||||
# TODO: This should be `list[int] | tuple[int, ...]`
|
||||
reveal_type(list_or_tuple_legacy) # revealed: @Todo(Generic specialization of types.UnionType)
|
||||
reveal_type(list_of_ints) # revealed: list[int]
|
||||
reveal_type(dict_str_to_int) # revealed: dict[str, int]
|
||||
reveal_type(subclass_of_int) # revealed: type[int]
|
||||
reveal_type(int_and_str) # revealed: tuple[int, str]
|
||||
reveal_type(pair_of_ints) # revealed: tuple[int, int]
|
||||
reveal_type(int_and_bytes) # revealed: tuple[int, bytes]
|
||||
reveal_type(list_or_tuple) # revealed: list[int] | tuple[int, ...]
|
||||
reveal_type(list_or_tuple_legacy) # revealed: list[int] | tuple[int, ...]
|
||||
reveal_type(list_or_tuple_legacy) # revealed: list[int] | tuple[int, ...]
|
||||
# TODO: This should be `(str, bytes) -> int`
|
||||
reveal_type(my_callable) # revealed: @Todo(Generic specialization of typing.Callable)
|
||||
# TODO: This should be `int`
|
||||
reveal_type(annotated_int) # revealed: @Todo(Generic specialization of typing.Annotated)
|
||||
reveal_type(my_callable) # revealed: Unknown
|
||||
reveal_type(annotated_int) # revealed: int
|
||||
reveal_type(transparent_alias) # revealed: int
|
||||
reveal_type(optional_int) # revealed: int | None
|
||||
```
|
||||
|
||||
Generic implicit type aliases can be partially specialized:
|
||||
|
||||
```py
|
||||
U = TypeVar("U")
|
||||
|
||||
DictStrTo = MyDict[str, U]
|
||||
|
||||
reveal_type(DictStrTo) # revealed: GenericAlias
|
||||
reveal_type(DictStrTo) # revealed: <class 'dict[str, U@DictStrTo]'>
|
||||
|
||||
def _(
|
||||
# TODO: No error here
|
||||
# error: [invalid-type-form] "Invalid subscript of object of type `GenericAlias` in type expression"
|
||||
dict_str_to_int: DictStrTo[int],
|
||||
):
|
||||
# TODO: This should be `dict[str, int]`
|
||||
reveal_type(dict_str_to_int) # revealed: Unknown
|
||||
reveal_type(dict_str_to_int) # revealed: dict[str, int]
|
||||
```
|
||||
|
||||
Using specializations of generic implicit type aliases in other implicit type aliases works as
|
||||
@@ -464,26 +459,65 @@ expected:
|
||||
IntsOrNone = MyList[int] | None
|
||||
IntsOrStrs = Pair[int] | Pair[str]
|
||||
ListOfPairs = MyList[Pair[str]]
|
||||
ListOrTupleOfInts = ListOrTuple[int]
|
||||
AnnotatedInt = AnnotatedType[int]
|
||||
SubclassOfInt = MyType[int]
|
||||
# TODO: No error here
|
||||
# error: [too-many-positional-arguments] "Too many positional arguments: expected 1, got 2"
|
||||
# error: [invalid-type-form] "List literals are not allowed in this context in a type expression: Did you mean `list[int]`?"
|
||||
CallableIntToStr = MyCallable[[int], str]
|
||||
|
||||
reveal_type(IntsOrNone) # revealed: UnionType
|
||||
reveal_type(IntsOrStrs) # revealed: UnionType
|
||||
reveal_type(ListOfPairs) # revealed: GenericAlias
|
||||
reveal_type(IntsOrNone) # revealed: types.UnionType
|
||||
reveal_type(IntsOrStrs) # revealed: types.UnionType
|
||||
reveal_type(ListOfPairs) # revealed: <class 'list[tuple[str, str]]'>
|
||||
reveal_type(ListOrTupleOfInts) # revealed: types.UnionType
|
||||
reveal_type(AnnotatedInt) # revealed: <typing.Annotated special form>
|
||||
reveal_type(SubclassOfInt) # revealed: GenericAlias
|
||||
reveal_type(CallableIntToStr) # revealed: Unknown
|
||||
|
||||
def _(
|
||||
# TODO: This should not be an error
|
||||
# error: [invalid-type-form] "Variable of type `UnionType` is not allowed in a type expression"
|
||||
ints_or_none: IntsOrNone,
|
||||
# TODO: This should not be an error
|
||||
# error: [invalid-type-form] "Variable of type `UnionType` is not allowed in a type expression"
|
||||
ints_or_strs: IntsOrStrs,
|
||||
list_of_pairs: ListOfPairs,
|
||||
list_or_tuple_of_ints: ListOrTupleOfInts,
|
||||
annotated_int: AnnotatedInt,
|
||||
subclass_of_int: SubclassOfInt,
|
||||
callable_int_to_str: CallableIntToStr,
|
||||
):
|
||||
# TODO: This should be `list[int] | None`
|
||||
reveal_type(ints_or_none) # revealed: Unknown
|
||||
# TODO: This should be `tuple[int, int] | tuple[str, str]`
|
||||
reveal_type(ints_or_strs) # revealed: Unknown
|
||||
# TODO: This should be `list[tuple[str, str]]`
|
||||
reveal_type(list_of_pairs) # revealed: @Todo(Support for `typing.GenericAlias` instances in type expressions)
|
||||
reveal_type(ints_or_none) # revealed: list[int] | None
|
||||
reveal_type(ints_or_strs) # revealed: tuple[int, int] | tuple[str, str]
|
||||
reveal_type(list_of_pairs) # revealed: list[tuple[str, str]]
|
||||
reveal_type(list_or_tuple_of_ints) # revealed: list[int] | tuple[int, ...]
|
||||
reveal_type(annotated_int) # revealed: int
|
||||
reveal_type(subclass_of_int) # revealed: type[int]
|
||||
# TODO: This should be `(int, /) -> str`
|
||||
reveal_type(callable_int_to_str) # revealed: Unknown
|
||||
```
|
||||
|
||||
A generic implicit type alias can also be used in another generic implicit type alias:
|
||||
|
||||
```py
|
||||
from typing_extensions import Any
|
||||
|
||||
B = TypeVar("B", bound=int)
|
||||
|
||||
MyOtherList = MyList[T]
|
||||
MyOtherType = MyType[T]
|
||||
TypeOrList = MyType[B] | MyList[B]
|
||||
|
||||
reveal_type(MyOtherList) # revealed: <class 'list[T@MyOtherList]'>
|
||||
reveal_type(MyOtherType) # revealed: GenericAlias
|
||||
reveal_type(TypeOrList) # revealed: types.UnionType
|
||||
|
||||
def _(
|
||||
list_of_ints: MyOtherList[int],
|
||||
subclass_of_int: MyOtherType[int],
|
||||
type_or_list: TypeOrList[Any],
|
||||
):
|
||||
reveal_type(list_of_ints) # revealed: list[int]
|
||||
reveal_type(subclass_of_int) # revealed: type[int]
|
||||
# TODO: Should be `type[Any] | list[Any]`
|
||||
reveal_type(type_or_list) # revealed: @Todo(type[T] for typevar T) | list[Any]
|
||||
```
|
||||
|
||||
If a generic implicit type alias is used unspecialized in a type expression, we treat it as an
|
||||
@@ -496,11 +530,11 @@ def _(
|
||||
my_callable: MyCallable,
|
||||
):
|
||||
# TODO: Should be `list[Unknown]`
|
||||
reveal_type(my_list) # revealed: list[typing.TypeVar]
|
||||
reveal_type(my_list) # revealed: list[T@MyList]
|
||||
# TODO: Should be `dict[Unknown, Unknown]`
|
||||
reveal_type(my_dict) # revealed: dict[typing.TypeVar, typing.TypeVar]
|
||||
reveal_type(my_dict) # revealed: dict[T@MyDict, U@MyDict]
|
||||
# TODO: Should be `(...) -> Unknown`
|
||||
reveal_type(my_callable) # revealed: (...) -> typing.TypeVar
|
||||
reveal_type(my_callable) # revealed: (...) -> T@MyCallable
|
||||
```
|
||||
|
||||
(Generic) implicit type aliases can be used as base classes:
|
||||
@@ -522,37 +556,182 @@ reveal_mro(Derived1)
|
||||
|
||||
GenericBaseAlias = GenericBase[T]
|
||||
|
||||
# TODO: No error here
|
||||
# error: [non-subscriptable] "Cannot subscript object of type `<class 'GenericBase[typing.TypeVar]'>` with no `__class_getitem__` method"
|
||||
class Derived2(GenericBaseAlias[int]):
|
||||
pass
|
||||
```
|
||||
|
||||
### Imported aliases
|
||||
|
||||
Generic implicit type aliases can be imported from other modules and specialized:
|
||||
|
||||
`my_types.py`:
|
||||
|
||||
```py
|
||||
from typing_extensions import TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
MyList = list[T]
|
||||
```
|
||||
|
||||
`main.py`:
|
||||
|
||||
```py
|
||||
from my_types import MyList
|
||||
import my_types as mt
|
||||
|
||||
def _(
|
||||
list_of_ints1: MyList[int],
|
||||
list_of_ints2: mt.MyList[int],
|
||||
):
|
||||
reveal_type(list_of_ints1) # revealed: list[int]
|
||||
reveal_type(list_of_ints2) # revealed: list[int]
|
||||
```
|
||||
|
||||
### In stringified annotations
|
||||
|
||||
Generic implicit type aliases can be specialized in stringified annotations:
|
||||
|
||||
```py
|
||||
from typing_extensions import TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
MyList = list[T]
|
||||
|
||||
def _(
|
||||
list_of_ints: "MyList[int]",
|
||||
):
|
||||
reveal_type(list_of_ints) # revealed: list[int]
|
||||
```
|
||||
|
||||
### Error cases
|
||||
|
||||
A generic alias that is already fully specialized cannot be specialized again:
|
||||
|
||||
```py
|
||||
ListOfInts = list[int]
|
||||
|
||||
# TODO: this should be an error
|
||||
# error: [too-many-positional-arguments] "Too many positional arguments: expected 0, got 1"
|
||||
def _(doubly_specialized: ListOfInts[int]):
|
||||
# TODO: this should be `Unknown`
|
||||
reveal_type(doubly_specialized) # revealed: @Todo(specialized generic alias in type expression)
|
||||
reveal_type(doubly_specialized) # revealed: Unknown
|
||||
```
|
||||
|
||||
Specializing a generic implicit type alias with an incorrect number of type arguments also results
|
||||
in an error:
|
||||
|
||||
```py
|
||||
from typing_extensions import TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
U = TypeVar("U")
|
||||
|
||||
MyList = list[T]
|
||||
MyDict = dict[T, U]
|
||||
|
||||
def _(
|
||||
# TODO: this should be an error
|
||||
# error: [too-many-positional-arguments] "Too many positional arguments: expected 1, got 2"
|
||||
list_too_many_args: MyList[int, str],
|
||||
# TODO: this should be an error
|
||||
# error: [missing-argument] "No argument provided for required parameter `U`"
|
||||
dict_too_few_args: MyDict[int],
|
||||
):
|
||||
# TODO: this should be `Unknown`
|
||||
reveal_type(list_too_many_args) # revealed: @Todo(specialized generic alias in type expression)
|
||||
# TODO: this should be `Unknown`
|
||||
reveal_type(dict_too_few_args) # revealed: @Todo(specialized generic alias in type expression)
|
||||
reveal_type(list_too_many_args) # revealed: Unknown
|
||||
reveal_type(dict_too_few_args) # revealed: Unknown
|
||||
```
|
||||
|
||||
Trying to specialize a non-name node results in an error:
|
||||
|
||||
```py
|
||||
from ty_extensions import TypeOf
|
||||
|
||||
IntOrStr = int | str
|
||||
|
||||
def this_does_not_work() -> TypeOf[IntOrStr]:
|
||||
raise NotImplementedError()
|
||||
|
||||
def _(
|
||||
# TODO: Better error message? `invalid-type-form`
|
||||
# error: [too-many-positional-arguments] "Too many positional arguments: expected 0, got 1"
|
||||
specialized: this_does_not_work()[int],
|
||||
):
|
||||
reveal_type(specialized) # revealed: Unknown
|
||||
```
|
||||
|
||||
Similarly, if you try to specialize a union type without a binding context, we emit an error:
|
||||
|
||||
```py
|
||||
# error: [invalid-type-form] "`types.UnionType` is not subscriptable"
|
||||
x: (list[T] | set[T])[int]
|
||||
|
||||
def _():
|
||||
reveal_type(x) # revealed: Unknown
|
||||
```
|
||||
|
||||
### Multiple definitions
|
||||
|
||||
#### Shadowed definitions
|
||||
|
||||
When a generic type alias shadows a definition from an outer scope, the inner definition is used:
|
||||
|
||||
```py
|
||||
from typing_extensions import TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
MyAlias = list[T]
|
||||
|
||||
def outer():
|
||||
MyAlias = set[T]
|
||||
|
||||
def _(x: MyAlias[int]):
|
||||
reveal_type(x) # revealed: set[int]
|
||||
```
|
||||
|
||||
#### Statically known conditions
|
||||
|
||||
```py
|
||||
from typing_extensions import TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
if True:
|
||||
MyAlias1 = list[T]
|
||||
else:
|
||||
MyAlias1 = set[T]
|
||||
|
||||
if False:
|
||||
MyAlias2 = list[T]
|
||||
else:
|
||||
MyAlias2 = set[T]
|
||||
|
||||
def _(
|
||||
x1: MyAlias1[int],
|
||||
x2: MyAlias2[int],
|
||||
):
|
||||
reveal_type(x1) # revealed: list[int]
|
||||
reveal_type(x2) # revealed: set[int]
|
||||
```
|
||||
|
||||
#### Statically unknown conditions
|
||||
|
||||
If several definitions are visible, we emit an error:
|
||||
|
||||
```py
|
||||
from typing_extensions import TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
def flag() -> bool:
|
||||
return True
|
||||
|
||||
if flag():
|
||||
MyAlias = list[T]
|
||||
else:
|
||||
MyAlias = set[T]
|
||||
|
||||
# error: [invalid-type-form] "Invalid subscript of object of type `<class 'list[T@MyAlias]'> | <class 'set[T@MyAlias]'>` in type expression"
|
||||
def _(x: MyAlias[int]):
|
||||
reveal_type(x) # revealed: Unknown
|
||||
```
|
||||
|
||||
## `Literal`s
|
||||
@@ -642,8 +821,7 @@ Deprecated = Annotated[T, "deprecated attribute"]
|
||||
class C:
|
||||
old: Deprecated[int]
|
||||
|
||||
# TODO: Should be `int`
|
||||
reveal_type(C().old) # revealed: @Todo(Generic specialization of typing.Annotated)
|
||||
reveal_type(C().old) # revealed: int
|
||||
```
|
||||
|
||||
If the metadata argument is missing, we emit an error (because this code fails at runtime), but
|
||||
@@ -1298,3 +1476,14 @@ def _(
|
||||
reveal_type(recursive_dict3) # revealed: dict[Divergent, int]
|
||||
reveal_type(recursive_dict4) # revealed: dict[Divergent, int]
|
||||
```
|
||||
|
||||
### Self-referential generic implicit type aliases
|
||||
|
||||
<!-- expect-panic: execute: too many cycle iterations -->
|
||||
|
||||
```py
|
||||
from typing import TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
NestedDict = dict[str, "NestedDict[T] | T"]
|
||||
```
|
||||
|
||||
@@ -38,7 +38,7 @@ See: <https://github.com/astral-sh/ty/issues/113>
|
||||
from pkg.sub import A
|
||||
|
||||
# TODO: This should be `<class 'A'>`
|
||||
reveal_type(A) # revealed: Divergent
|
||||
reveal_type(A) # revealed: Never
|
||||
```
|
||||
|
||||
`pkg/outer.py`:
|
||||
|
||||
@@ -1,314 +0,0 @@
|
||||
# `typing.override`
|
||||
|
||||
## Basics
|
||||
|
||||
Decorating a method with `typing.override` decorator is an explicit indication to a type checker
|
||||
that the method is intended to override a method on a superclass. If the decorated method does not
|
||||
in fact override anything, a type checker should report a diagnostic on that method.
|
||||
|
||||
<!-- snapshot-diagnostics -->
|
||||
|
||||
```pyi
|
||||
from typing_extensions import override, Callable, TypeVar
|
||||
|
||||
def lossy_decorator(fn: Callable) -> Callable: ...
|
||||
|
||||
class A:
|
||||
@override
|
||||
def __repr__(self): ... # fine: overrides `object.__repr__`
|
||||
|
||||
class Parent:
|
||||
def foo(self): ...
|
||||
@property
|
||||
def my_property1(self) -> int: ...
|
||||
@property
|
||||
def my_property2(self) -> int: ...
|
||||
baz = None
|
||||
@classmethod
|
||||
def class_method1(cls) -> int: ...
|
||||
@staticmethod
|
||||
def static_method1() -> int: ...
|
||||
@classmethod
|
||||
def class_method2(cls) -> int: ...
|
||||
@staticmethod
|
||||
def static_method2() -> int: ...
|
||||
@lossy_decorator
|
||||
def decorated_1(self): ...
|
||||
@lossy_decorator
|
||||
def decorated_2(self): ...
|
||||
@lossy_decorator
|
||||
def decorated_3(self): ...
|
||||
|
||||
class Child(Parent):
|
||||
@override
|
||||
def foo(self): ... # fine: overrides `Parent.foo`
|
||||
@property
|
||||
@override
|
||||
def my_property1(self) -> int: ... # fine: overrides `Parent.my_property1`
|
||||
@override
|
||||
@property
|
||||
def my_property2(self) -> int: ... # fine: overrides `Parent.my_property2`
|
||||
@override
|
||||
def baz(self): ... # fine: overrides `Parent.baz`
|
||||
@classmethod
|
||||
@override
|
||||
def class_method1(cls) -> int: ... # fine: overrides `Parent.class_method1`
|
||||
@staticmethod
|
||||
@override
|
||||
def static_method1() -> int: ... # fine: overrides `Parent.static_method1`
|
||||
@override
|
||||
@classmethod
|
||||
def class_method2(cls) -> int: ... # fine: overrides `Parent.class_method2`
|
||||
@override
|
||||
@staticmethod
|
||||
def static_method2() -> int: ... # fine: overrides `Parent.static_method2`
|
||||
@override
|
||||
def decorated_1(self): ... # fine: overrides `Parent.decorated_1`
|
||||
@override
|
||||
@lossy_decorator
|
||||
def decorated_2(self): ... # fine: overrides `Parent.decorated_2`
|
||||
@lossy_decorator
|
||||
@override
|
||||
def decorated_3(self): ... # fine: overrides `Parent.decorated_3`
|
||||
|
||||
class OtherChild(Parent): ...
|
||||
|
||||
class Grandchild(OtherChild):
|
||||
@override
|
||||
def foo(self): ... # fine: overrides `Parent.foo`
|
||||
@override
|
||||
@property
|
||||
def bar(self) -> int: ... # fine: overrides `Parent.bar`
|
||||
@override
|
||||
def baz(self): ... # fine: overrides `Parent.baz`
|
||||
@classmethod
|
||||
@override
|
||||
def class_method1(cls) -> int: ... # fine: overrides `Parent.class_method1`
|
||||
@staticmethod
|
||||
@override
|
||||
def static_method1() -> int: ... # fine: overrides `Parent.static_method1`
|
||||
@override
|
||||
@classmethod
|
||||
def class_method2(cls) -> int: ... # fine: overrides `Parent.class_method2`
|
||||
@override
|
||||
@staticmethod
|
||||
def static_method2() -> int: ... # fine: overrides `Parent.static_method2`
|
||||
@override
|
||||
def decorated_1(self): ... # fine: overrides `Parent.decorated_1`
|
||||
@override
|
||||
@lossy_decorator
|
||||
def decorated_2(self): ... # fine: overrides `Parent.decorated_2`
|
||||
@lossy_decorator
|
||||
@override
|
||||
def decorated_3(self): ... # fine: overrides `Parent.decorated_3`
|
||||
|
||||
class Invalid:
|
||||
@override
|
||||
def ___reprrr__(self): ... # error: [invalid-explicit-override]
|
||||
@override
|
||||
@classmethod
|
||||
def foo(self): ... # error: [invalid-explicit-override]
|
||||
@classmethod
|
||||
@override
|
||||
def bar(self): ... # error: [invalid-explicit-override]
|
||||
@staticmethod
|
||||
@override
|
||||
def baz(): ... # error: [invalid-explicit-override]
|
||||
@override
|
||||
@staticmethod
|
||||
def eggs(): ... # error: [invalid-explicit-override]
|
||||
@property
|
||||
@override
|
||||
def bad_property1(self) -> int: ... # TODO: should emit `invalid-explicit-override` here
|
||||
@override
|
||||
@property
|
||||
def bad_property2(self) -> int: ... # TODO: should emit `invalid-explicit-override` here
|
||||
@lossy_decorator
|
||||
@override
|
||||
def lossy(self): ... # TODO: should emit `invalid-explicit-override` here
|
||||
@override
|
||||
@lossy_decorator
|
||||
def lossy2(self): ... # TODO: should emit `invalid-explicit-override` here
|
||||
|
||||
# TODO: all overrides in this class should cause us to emit *Liskov* violations,
|
||||
# but not `@override` violations
|
||||
class LiskovViolatingButNotOverrideViolating(Parent):
|
||||
@override
|
||||
@property
|
||||
def foo(self) -> int: ...
|
||||
@override
|
||||
def my_property1(self) -> int: ...
|
||||
@staticmethod
|
||||
@override
|
||||
def class_method1() -> int: ...
|
||||
@classmethod
|
||||
@override
|
||||
def static_method1(cls) -> int: ...
|
||||
|
||||
# Diagnostic edge case: `override` is very far away from the method definition in the source code:
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
def identity(x: T) -> T: ...
|
||||
|
||||
class Foo:
|
||||
@override
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
@identity
|
||||
def bar(self): ... # error: [invalid-explicit-override]
|
||||
```
|
||||
|
||||
## Overloads
|
||||
|
||||
The typing spec states that for an overloaded method, `@override` should only be applied to the
|
||||
implementation function. However, we nonetheless respect the decorator in this situation, even
|
||||
though we also emit `invalid-overload` on these methods.
|
||||
|
||||
```py
|
||||
from typing_extensions import override, overload
|
||||
|
||||
class Spam:
|
||||
@overload
|
||||
def foo(self, x: str) -> str: ...
|
||||
@overload
|
||||
def foo(self, x: int) -> int: ...
|
||||
@override
|
||||
def foo(self, x: str | int) -> str | int: # error: [invalid-explicit-override]
|
||||
return x
|
||||
|
||||
@overload
|
||||
@override
|
||||
def bar(self, x: str) -> str: ...
|
||||
@overload
|
||||
@override
|
||||
def bar(self, x: int) -> int: ...
|
||||
@override
|
||||
# error: [invalid-overload] "`@override` decorator should be applied only to the overload implementation"
|
||||
# error: [invalid-overload] "`@override` decorator should be applied only to the overload implementation"
|
||||
# error: [invalid-explicit-override]
|
||||
def bar(self, x: str | int) -> str | int:
|
||||
return x
|
||||
|
||||
@overload
|
||||
@override
|
||||
def baz(self, x: str) -> str: ...
|
||||
@overload
|
||||
def baz(self, x: int) -> int: ...
|
||||
# error: [invalid-overload] "`@override` decorator should be applied only to the overload implementation"
|
||||
# error: [invalid-explicit-override]
|
||||
def baz(self, x: str | int) -> str | int:
|
||||
return x
|
||||
```
|
||||
|
||||
In a stub file, `@override` should always be applied to the first overload. Even if it isn't, we
|
||||
always emit `invalid-explicit-override` diagnostics on the first overload.
|
||||
|
||||
`module.pyi`:
|
||||
|
||||
```pyi
|
||||
from typing_extensions import override, overload
|
||||
|
||||
class Spam:
|
||||
@overload
|
||||
def foo(self, x: str) -> str: ... # error: [invalid-explicit-override]
|
||||
@overload
|
||||
@override
|
||||
# error: [invalid-overload] "`@override` decorator should be applied only to the first overload"
|
||||
def foo(self, x: int) -> int: ...
|
||||
|
||||
@overload
|
||||
@override
|
||||
def bar(self, x: str) -> str: ... # error: [invalid-explicit-override]
|
||||
@overload
|
||||
@override
|
||||
# error: [invalid-overload] "`@override` decorator should be applied only to the first overload"
|
||||
def bar(self, x: int) -> int: ...
|
||||
|
||||
@overload
|
||||
@override
|
||||
def baz(self, x: str) -> str: ... # error: [invalid-explicit-override]
|
||||
@overload
|
||||
def baz(self, x: int) -> int: ...
|
||||
```
|
||||
|
||||
## Classes inheriting from `Any`
|
||||
|
||||
```py
|
||||
from typing_extensions import Any, override
|
||||
from does_not_exist import SomethingUnknown # error: [unresolved-import]
|
||||
|
||||
class Parent1(Any): ...
|
||||
class Parent2(SomethingUnknown): ...
|
||||
|
||||
class Child1(Parent1):
|
||||
@override
|
||||
def bar(self): ... # fine
|
||||
|
||||
class Child2(Parent2):
|
||||
@override
|
||||
def bar(self): ... # fine
|
||||
```
|
||||
|
||||
## Override of a synthesized method
|
||||
|
||||
```pyi
|
||||
from typing_extensions import NamedTuple, TypedDict, override, Any, Self
|
||||
from dataclasses import dataclass
|
||||
|
||||
@dataclass(order=True)
|
||||
class ParentDataclass:
|
||||
x: int
|
||||
|
||||
class Child(ParentDataclass):
|
||||
@override
|
||||
def __lt__(self, other: ParentDataclass) -> bool: ... # fine
|
||||
|
||||
class MyNamedTuple(NamedTuple):
|
||||
x: int
|
||||
|
||||
@override
|
||||
# TODO: this raises an exception at runtime (which we should emit a diagnostic for).
|
||||
# It shouldn't be an `invalid-explicit-override` diagnostic, however.
|
||||
def _asdict(self, /) -> dict[str, Any]: ...
|
||||
|
||||
class MyNamedTupleParent(NamedTuple):
|
||||
x: int
|
||||
|
||||
class MyNamedTupleChild(MyNamedTupleParent):
|
||||
@override
|
||||
def _asdict(self, /) -> dict[str, Any]: ... # fine
|
||||
|
||||
class MyTypedDict(TypedDict):
|
||||
x: int
|
||||
|
||||
@override
|
||||
# TODO: it's invalid to define a method on a `TypedDict` class,
|
||||
# so we should emit a diagnostic here.
|
||||
# It shouldn't be an `invalid-explicit-override` diagnostic, however.
|
||||
def copy(self) -> Self: ...
|
||||
|
||||
class Grandparent(Any): ...
|
||||
|
||||
class Parent(Grandparent, NamedTuple): # error: [invalid-named-tuple]
|
||||
x: int
|
||||
|
||||
class Child(Parent):
|
||||
@override
|
||||
def foo(self): ... # fine because `Any` is in the MRO
|
||||
```
|
||||
@@ -96,6 +96,24 @@ def _(x: MyAlias):
|
||||
reveal_type(x) # revealed: int | ((str, /) -> int)
|
||||
```
|
||||
|
||||
## Generic aliases
|
||||
|
||||
```py
|
||||
from typing import TypeAlias, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
MyList: TypeAlias = list[T]
|
||||
ListOrSet: TypeAlias = list[T] | set[T]
|
||||
|
||||
reveal_type(MyList) # revealed: <class 'list[T]'>
|
||||
reveal_type(ListOrSet) # revealed: types.UnionType
|
||||
|
||||
def _(list_of_int: MyList[int], list_or_set_of_str: ListOrSet[str]):
|
||||
reveal_type(list_of_int) # revealed: list[int]
|
||||
reveal_type(list_or_set_of_str) # revealed: list[str] | set[str]
|
||||
```
|
||||
|
||||
## Subscripted generic alias in union
|
||||
|
||||
```py
|
||||
@@ -107,8 +125,7 @@ Alias1: TypeAlias = list[T] | set[T]
|
||||
MyAlias: TypeAlias = int | Alias1[str]
|
||||
|
||||
def _(x: MyAlias):
|
||||
# TODO: int | list[str] | set[str]
|
||||
reveal_type(x) # revealed: int | @Todo(Specialization of union type alias)
|
||||
reveal_type(x) # revealed: int | list[str] | set[str]
|
||||
```
|
||||
|
||||
## Imported
|
||||
@@ -144,46 +161,17 @@ def _(x: IntOrStr):
|
||||
## Cyclic
|
||||
|
||||
```py
|
||||
from typing import TypeAlias, TypeVar, Union
|
||||
from types import UnionType
|
||||
from typing import TypeAlias
|
||||
|
||||
RecursiveTuple: TypeAlias = tuple[int | "RecursiveTuple", str]
|
||||
|
||||
def _(rec: RecursiveTuple):
|
||||
# TODO should be `tuple[int | RecursiveTuple, str]`
|
||||
reveal_type(rec) # revealed: tuple[Divergent, str]
|
||||
|
||||
RecursiveHomogeneousTuple: TypeAlias = tuple[int | "RecursiveHomogeneousTuple", ...]
|
||||
|
||||
def _(rec: RecursiveHomogeneousTuple):
|
||||
# TODO should be `tuple[int | RecursiveHomogeneousTuple, ...]`
|
||||
reveal_type(rec) # revealed: tuple[Divergent, ...]
|
||||
|
||||
ClassInfo: TypeAlias = type | UnionType | tuple["ClassInfo", ...]
|
||||
reveal_type(ClassInfo) # revealed: types.UnionType
|
||||
|
||||
def my_isinstance(obj: object, classinfo: ClassInfo) -> bool:
|
||||
# TODO should be `type | UnionType | tuple[ClassInfo, ...]`
|
||||
reveal_type(classinfo) # revealed: type | UnionType | tuple[Divergent, ...]
|
||||
return isinstance(obj, classinfo)
|
||||
|
||||
K = TypeVar("K")
|
||||
V = TypeVar("V")
|
||||
NestedDict: TypeAlias = dict[K, Union[V, "NestedDict[K, V]"]]
|
||||
|
||||
def _(nested: NestedDict[str, int]):
|
||||
# TODO should be `dict[str, int | NestedDict[str, int]]`
|
||||
reveal_type(nested) # revealed: @Todo(specialized generic alias in type expression)
|
||||
|
||||
my_isinstance(1, int)
|
||||
my_isinstance(1, int | str)
|
||||
my_isinstance(1, (int, str))
|
||||
my_isinstance(1, (int, (str, float)))
|
||||
my_isinstance(1, (int, (str | float)))
|
||||
# error: [invalid-argument-type]
|
||||
my_isinstance(1, 1)
|
||||
# TODO should be an invalid-argument-type error
|
||||
my_isinstance(1, (int, (str, 1)))
|
||||
```
|
||||
|
||||
## Conditionally imported on Python < 3.10
|
||||
|
||||
@@ -106,29 +106,6 @@ def _(flag: bool):
|
||||
```py
|
||||
type ListOrSet[T] = list[T] | set[T]
|
||||
reveal_type(ListOrSet.__type_params__) # revealed: tuple[TypeVar | ParamSpec | TypeVarTuple, ...]
|
||||
type Tuple1[T] = tuple[T]
|
||||
|
||||
def _(cond: bool):
|
||||
Generic = ListOrSet if cond else Tuple1
|
||||
|
||||
def _(x: Generic[int]):
|
||||
reveal_type(x) # revealed: list[int] | set[int] | tuple[int]
|
||||
|
||||
try:
|
||||
class Foo[T]:
|
||||
x: T
|
||||
def foo(self) -> T:
|
||||
return self.x
|
||||
|
||||
...
|
||||
except Exception:
|
||||
class Foo[T]:
|
||||
x: T
|
||||
def foo(self) -> T:
|
||||
return self.x
|
||||
|
||||
def f(x: Foo[int]):
|
||||
reveal_type(x.foo()) # revealed: int
|
||||
```
|
||||
|
||||
## In unions and intersections
|
||||
@@ -267,47 +244,6 @@ def f(x: IntOr, y: OrInt):
|
||||
reveal_type(x) # revealed: Never
|
||||
if not isinstance(y, int):
|
||||
reveal_type(y) # revealed: Never
|
||||
|
||||
# error: [cyclic-type-alias-definition] "Cyclic definition of `Itself`"
|
||||
type Itself = Itself
|
||||
|
||||
def foo(
|
||||
# this is a very strange thing to do, but this is a regression test to ensure it doesn't panic
|
||||
Itself: Itself,
|
||||
):
|
||||
x: Itself
|
||||
reveal_type(Itself) # revealed: Divergent
|
||||
|
||||
# A type alias defined with invalid recursion behaves as a dynamic type.
|
||||
foo(42)
|
||||
foo("hello")
|
||||
|
||||
# error: [cyclic-type-alias-definition] "Cyclic definition of `A`"
|
||||
type A = B
|
||||
# error: [cyclic-type-alias-definition] "Cyclic definition of `B`"
|
||||
type B = A
|
||||
|
||||
def bar(B: B):
|
||||
x: B
|
||||
reveal_type(B) # revealed: Divergent
|
||||
|
||||
# error: [cyclic-type-alias-definition] "Cyclic definition of `G`"
|
||||
type G[T] = G[T]
|
||||
# error: [cyclic-type-alias-definition] "Cyclic definition of `H`"
|
||||
type H[T] = I[T]
|
||||
# error: [cyclic-type-alias-definition] "Cyclic definition of `I`"
|
||||
type I[T] = H[T]
|
||||
|
||||
# It's not possible to create an element of this type, but it's not an error for now
|
||||
type DirectRecursiveList[T] = list[DirectRecursiveList[T]]
|
||||
|
||||
# TODO: this should probably be a cyclic-type-alias-definition error
|
||||
type Foo[T] = list[T] | Bar[T]
|
||||
type Bar[T] = int | Foo[T]
|
||||
|
||||
def _(x: Bar[int]):
|
||||
# TODO: should be `int | list[int]`
|
||||
reveal_type(x) # revealed: int | list[int] | Any
|
||||
```
|
||||
|
||||
### With legacy generic
|
||||
@@ -391,7 +327,7 @@ class C(P[T]):
|
||||
pass
|
||||
|
||||
reveal_type(C[int]()) # revealed: C[int]
|
||||
reveal_type(C()) # revealed: C[C[Divergent]]
|
||||
reveal_type(C()) # revealed: C[Divergent]
|
||||
```
|
||||
|
||||
### Union inside generic
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
|
||||
Regression test for <https://github.com/astral-sh/ty/issues/1377>.
|
||||
|
||||
The code is an excerpt from <https://github.com/Gobot1234/steam.py>.
|
||||
The code is an excerpt from <https://github.com/Gobot1234/steam.py> that is minimal enough to
|
||||
trigger the iteration count mismatch bug in Salsa.
|
||||
|
||||
<!-- expect-panic: execute: too many cycle iterations -->
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
|
||||
@@ -34,27 +34,29 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/annotations/invalid.md
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[invalid-type-form]: Module `datetime` is not valid in a type expression
|
||||
error[invalid-type-form]: Variable of type `<module 'datetime'>` is not allowed in a type expression
|
||||
--> src/foo.py:3:10
|
||||
|
|
||||
1 | import datetime
|
||||
2 |
|
||||
3 | def f(x: datetime): ... # error: [invalid-type-form]
|
||||
| ^^^^^^^^ Did you mean to use the module's member `datetime.datetime`?
|
||||
| ^^^^^^^^
|
||||
|
|
||||
info: Did you mean to use the module's member `datetime.datetime` instead?
|
||||
info: rule `invalid-type-form` is enabled by default
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
error[invalid-type-form]: Module `PIL.Image` is not valid in a type expression
|
||||
error[invalid-type-form]: Variable of type `<module 'PIL.Image'>` is not allowed in a type expression
|
||||
--> src/bar.py:3:10
|
||||
|
|
||||
1 | from PIL import Image
|
||||
2 |
|
||||
3 | def g(x: Image): ... # error: [invalid-type-form]
|
||||
| ^^^^^ Did you mean to use the module's member `Image.Image`?
|
||||
| ^^^^^
|
||||
|
|
||||
info: Did you mean to use the module's member `Image.Image` instead?
|
||||
info: rule `invalid-type-form` is enabled by default
|
||||
|
||||
```
|
||||
|
||||
@@ -23,13 +23,13 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_assi
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[invalid-assignment]: Object of type `Literal["a"]` is not assignable to `int`
|
||||
error[invalid-assignment]: Object of type `tuple[Literal["a"], Literal["b"]]` is not assignable to `int`
|
||||
--> src/mdtest_snippet.py:4:1
|
||||
|
|
||||
2 | y: str
|
||||
3 |
|
||||
4 | x, y = ("a", "b") # error: [invalid-assignment]
|
||||
| - ^^^^^^^^^^ Incompatible value of type `Literal["a"]`
|
||||
| - ^^^^^^^^^^ Incompatible value of type `tuple[Literal["a"], Literal["b"]]`
|
||||
| |
|
||||
| Declared type `int`
|
||||
5 |
|
||||
@@ -40,13 +40,13 @@ info: rule `invalid-assignment` is enabled by default
|
||||
```
|
||||
|
||||
```
|
||||
error[invalid-assignment]: Object of type `Literal[0]` is not assignable to `str`
|
||||
error[invalid-assignment]: Object of type `tuple[Literal[0], Literal[0]]` is not assignable to `str`
|
||||
--> src/mdtest_snippet.py:6:4
|
||||
|
|
||||
4 | x, y = ("a", "b") # error: [invalid-assignment]
|
||||
5 |
|
||||
6 | x, y = (0, 0) # error: [invalid-assignment]
|
||||
| - ^^^^^^ Incompatible value of type `Literal[0]`
|
||||
| - ^^^^^^ Incompatible value of type `tuple[Literal[0], Literal[0]]`
|
||||
| |
|
||||
| Declared type `str`
|
||||
|
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
---
|
||||
mdtest name: literal_string.md - `LiteralString` - Usages - Parameterized
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/annotations/literal_string.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## mdtest_snippet.py
|
||||
|
||||
```
|
||||
1 | from typing_extensions import LiteralString
|
||||
2 |
|
||||
3 | # error: [invalid-type-form]
|
||||
4 | a: LiteralString[str]
|
||||
5 |
|
||||
6 | # error: [invalid-type-form]
|
||||
7 | b: LiteralString["foo"]
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[invalid-type-form]: `LiteralString` expects no type parameter
|
||||
--> src/mdtest_snippet.py:4:4
|
||||
|
|
||||
3 | # error: [invalid-type-form]
|
||||
4 | a: LiteralString[str]
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
5 |
|
||||
6 | # error: [invalid-type-form]
|
||||
|
|
||||
info: rule `invalid-type-form` is enabled by default
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
error[invalid-type-form]: `LiteralString` expects no type parameter
|
||||
--> src/mdtest_snippet.py:7:4
|
||||
|
|
||||
6 | # error: [invalid-type-form]
|
||||
7 | b: LiteralString["foo"]
|
||||
| -------------^^^^^^^
|
||||
| |
|
||||
| Did you mean `Literal`?
|
||||
|
|
||||
info: rule `invalid-type-form` is enabled by default
|
||||
|
||||
```
|
||||
@@ -312,15 +312,6 @@ info[unused-ignore-comment]: Unused blanket `type: ignore` directive
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
73 | ): ...
|
||||
|
|
||||
help: Remove the unused suppression comment
|
||||
69 | class D(
|
||||
70 | A,
|
||||
71 | # error: [unused-ignore-comment]
|
||||
- A, # type: ignore[duplicate-base]
|
||||
72 + A,
|
||||
73 | ): ...
|
||||
74 |
|
||||
75 | # error: [duplicate-base]
|
||||
|
||||
```
|
||||
|
||||
@@ -365,13 +356,5 @@ info[unused-ignore-comment]: Unused blanket `type: ignore` directive
|
||||
82 |
|
||||
83 | # fmt: on
|
||||
|
|
||||
help: Remove the unused suppression comment
|
||||
78 | A
|
||||
79 | ):
|
||||
80 | # error: [unused-ignore-comment]
|
||||
- x: int # type: ignore[duplicate-base]
|
||||
81 + x: int
|
||||
82 |
|
||||
83 | # fmt: on
|
||||
|
||||
```
|
||||
|
||||
@@ -1,293 +0,0 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
---
|
||||
mdtest name: override.md - `typing.override` - Basics
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/override.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## mdtest_snippet.pyi
|
||||
|
||||
```
|
||||
1 | from typing_extensions import override, Callable, TypeVar
|
||||
2 |
|
||||
3 | def lossy_decorator(fn: Callable) -> Callable: ...
|
||||
4 |
|
||||
5 | class A:
|
||||
6 | @override
|
||||
7 | def __repr__(self): ... # fine: overrides `object.__repr__`
|
||||
8 |
|
||||
9 | class Parent:
|
||||
10 | def foo(self): ...
|
||||
11 | @property
|
||||
12 | def my_property1(self) -> int: ...
|
||||
13 | @property
|
||||
14 | def my_property2(self) -> int: ...
|
||||
15 | baz = None
|
||||
16 | @classmethod
|
||||
17 | def class_method1(cls) -> int: ...
|
||||
18 | @staticmethod
|
||||
19 | def static_method1() -> int: ...
|
||||
20 | @classmethod
|
||||
21 | def class_method2(cls) -> int: ...
|
||||
22 | @staticmethod
|
||||
23 | def static_method2() -> int: ...
|
||||
24 | @lossy_decorator
|
||||
25 | def decorated_1(self): ...
|
||||
26 | @lossy_decorator
|
||||
27 | def decorated_2(self): ...
|
||||
28 | @lossy_decorator
|
||||
29 | def decorated_3(self): ...
|
||||
30 |
|
||||
31 | class Child(Parent):
|
||||
32 | @override
|
||||
33 | def foo(self): ... # fine: overrides `Parent.foo`
|
||||
34 | @property
|
||||
35 | @override
|
||||
36 | def my_property1(self) -> int: ... # fine: overrides `Parent.my_property1`
|
||||
37 | @override
|
||||
38 | @property
|
||||
39 | def my_property2(self) -> int: ... # fine: overrides `Parent.my_property2`
|
||||
40 | @override
|
||||
41 | def baz(self): ... # fine: overrides `Parent.baz`
|
||||
42 | @classmethod
|
||||
43 | @override
|
||||
44 | def class_method1(cls) -> int: ... # fine: overrides `Parent.class_method1`
|
||||
45 | @staticmethod
|
||||
46 | @override
|
||||
47 | def static_method1() -> int: ... # fine: overrides `Parent.static_method1`
|
||||
48 | @override
|
||||
49 | @classmethod
|
||||
50 | def class_method2(cls) -> int: ... # fine: overrides `Parent.class_method2`
|
||||
51 | @override
|
||||
52 | @staticmethod
|
||||
53 | def static_method2() -> int: ... # fine: overrides `Parent.static_method2`
|
||||
54 | @override
|
||||
55 | def decorated_1(self): ... # fine: overrides `Parent.decorated_1`
|
||||
56 | @override
|
||||
57 | @lossy_decorator
|
||||
58 | def decorated_2(self): ... # fine: overrides `Parent.decorated_2`
|
||||
59 | @lossy_decorator
|
||||
60 | @override
|
||||
61 | def decorated_3(self): ... # fine: overrides `Parent.decorated_3`
|
||||
62 |
|
||||
63 | class OtherChild(Parent): ...
|
||||
64 |
|
||||
65 | class Grandchild(OtherChild):
|
||||
66 | @override
|
||||
67 | def foo(self): ... # fine: overrides `Parent.foo`
|
||||
68 | @override
|
||||
69 | @property
|
||||
70 | def bar(self) -> int: ... # fine: overrides `Parent.bar`
|
||||
71 | @override
|
||||
72 | def baz(self): ... # fine: overrides `Parent.baz`
|
||||
73 | @classmethod
|
||||
74 | @override
|
||||
75 | def class_method1(cls) -> int: ... # fine: overrides `Parent.class_method1`
|
||||
76 | @staticmethod
|
||||
77 | @override
|
||||
78 | def static_method1() -> int: ... # fine: overrides `Parent.static_method1`
|
||||
79 | @override
|
||||
80 | @classmethod
|
||||
81 | def class_method2(cls) -> int: ... # fine: overrides `Parent.class_method2`
|
||||
82 | @override
|
||||
83 | @staticmethod
|
||||
84 | def static_method2() -> int: ... # fine: overrides `Parent.static_method2`
|
||||
85 | @override
|
||||
86 | def decorated_1(self): ... # fine: overrides `Parent.decorated_1`
|
||||
87 | @override
|
||||
88 | @lossy_decorator
|
||||
89 | def decorated_2(self): ... # fine: overrides `Parent.decorated_2`
|
||||
90 | @lossy_decorator
|
||||
91 | @override
|
||||
92 | def decorated_3(self): ... # fine: overrides `Parent.decorated_3`
|
||||
93 |
|
||||
94 | class Invalid:
|
||||
95 | @override
|
||||
96 | def ___reprrr__(self): ... # error: [invalid-explicit-override]
|
||||
97 | @override
|
||||
98 | @classmethod
|
||||
99 | def foo(self): ... # error: [invalid-explicit-override]
|
||||
100 | @classmethod
|
||||
101 | @override
|
||||
102 | def bar(self): ... # error: [invalid-explicit-override]
|
||||
103 | @staticmethod
|
||||
104 | @override
|
||||
105 | def baz(): ... # error: [invalid-explicit-override]
|
||||
106 | @override
|
||||
107 | @staticmethod
|
||||
108 | def eggs(): ... # error: [invalid-explicit-override]
|
||||
109 | @property
|
||||
110 | @override
|
||||
111 | def bad_property1(self) -> int: ... # TODO: should emit `invalid-explicit-override` here
|
||||
112 | @override
|
||||
113 | @property
|
||||
114 | def bad_property2(self) -> int: ... # TODO: should emit `invalid-explicit-override` here
|
||||
115 | @lossy_decorator
|
||||
116 | @override
|
||||
117 | def lossy(self): ... # TODO: should emit `invalid-explicit-override` here
|
||||
118 | @override
|
||||
119 | @lossy_decorator
|
||||
120 | def lossy2(self): ... # TODO: should emit `invalid-explicit-override` here
|
||||
121 |
|
||||
122 | # TODO: all overrides in this class should cause us to emit *Liskov* violations,
|
||||
123 | # but not `@override` violations
|
||||
124 | class LiskovViolatingButNotOverrideViolating(Parent):
|
||||
125 | @override
|
||||
126 | @property
|
||||
127 | def foo(self) -> int: ...
|
||||
128 | @override
|
||||
129 | def my_property1(self) -> int: ...
|
||||
130 | @staticmethod
|
||||
131 | @override
|
||||
132 | def class_method1() -> int: ...
|
||||
133 | @classmethod
|
||||
134 | @override
|
||||
135 | def static_method1(cls) -> int: ...
|
||||
136 |
|
||||
137 | # Diagnostic edge case: `override` is very far away from the method definition in the source code:
|
||||
138 |
|
||||
139 | T = TypeVar("T")
|
||||
140 |
|
||||
141 | def identity(x: T) -> T: ...
|
||||
142 |
|
||||
143 | class Foo:
|
||||
144 | @override
|
||||
145 | @identity
|
||||
146 | @identity
|
||||
147 | @identity
|
||||
148 | @identity
|
||||
149 | @identity
|
||||
150 | @identity
|
||||
151 | @identity
|
||||
152 | @identity
|
||||
153 | @identity
|
||||
154 | @identity
|
||||
155 | @identity
|
||||
156 | @identity
|
||||
157 | @identity
|
||||
158 | @identity
|
||||
159 | @identity
|
||||
160 | @identity
|
||||
161 | @identity
|
||||
162 | @identity
|
||||
163 | def bar(self): ... # error: [invalid-explicit-override]
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[invalid-explicit-override]: Method `___reprrr__` is decorated with `@override` but does not override anything
|
||||
--> src/mdtest_snippet.pyi:95:5
|
||||
|
|
||||
94 | class Invalid:
|
||||
95 | @override
|
||||
| ---------
|
||||
96 | def ___reprrr__(self): ... # error: [invalid-explicit-override]
|
||||
| ^^^^^^^^^^^
|
||||
97 | @override
|
||||
98 | @classmethod
|
||||
|
|
||||
info: No `___reprrr__` definitions were found on any superclasses of `Invalid`
|
||||
info: rule `invalid-explicit-override` is enabled by default
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
error[invalid-explicit-override]: Method `foo` is decorated with `@override` but does not override anything
|
||||
--> src/mdtest_snippet.pyi:97:5
|
||||
|
|
||||
95 | @override
|
||||
96 | def ___reprrr__(self): ... # error: [invalid-explicit-override]
|
||||
97 | @override
|
||||
| ---------
|
||||
98 | @classmethod
|
||||
99 | def foo(self): ... # error: [invalid-explicit-override]
|
||||
| ^^^
|
||||
100 | @classmethod
|
||||
101 | @override
|
||||
|
|
||||
info: No `foo` definitions were found on any superclasses of `Invalid`
|
||||
info: rule `invalid-explicit-override` is enabled by default
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
error[invalid-explicit-override]: Method `bar` is decorated with `@override` but does not override anything
|
||||
--> src/mdtest_snippet.pyi:101:5
|
||||
|
|
||||
99 | def foo(self): ... # error: [invalid-explicit-override]
|
||||
100 | @classmethod
|
||||
101 | @override
|
||||
| ---------
|
||||
102 | def bar(self): ... # error: [invalid-explicit-override]
|
||||
| ^^^
|
||||
103 | @staticmethod
|
||||
104 | @override
|
||||
|
|
||||
info: No `bar` definitions were found on any superclasses of `Invalid`
|
||||
info: rule `invalid-explicit-override` is enabled by default
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
error[invalid-explicit-override]: Method `baz` is decorated with `@override` but does not override anything
|
||||
--> src/mdtest_snippet.pyi:104:5
|
||||
|
|
||||
102 | def bar(self): ... # error: [invalid-explicit-override]
|
||||
103 | @staticmethod
|
||||
104 | @override
|
||||
| ---------
|
||||
105 | def baz(): ... # error: [invalid-explicit-override]
|
||||
| ^^^
|
||||
106 | @override
|
||||
107 | @staticmethod
|
||||
|
|
||||
info: No `baz` definitions were found on any superclasses of `Invalid`
|
||||
info: rule `invalid-explicit-override` is enabled by default
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
error[invalid-explicit-override]: Method `eggs` is decorated with `@override` but does not override anything
|
||||
--> src/mdtest_snippet.pyi:106:5
|
||||
|
|
||||
104 | @override
|
||||
105 | def baz(): ... # error: [invalid-explicit-override]
|
||||
106 | @override
|
||||
| ---------
|
||||
107 | @staticmethod
|
||||
108 | def eggs(): ... # error: [invalid-explicit-override]
|
||||
| ^^^^
|
||||
109 | @property
|
||||
110 | @override
|
||||
|
|
||||
info: No `eggs` definitions were found on any superclasses of `Invalid`
|
||||
info: rule `invalid-explicit-override` is enabled by default
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
error[invalid-explicit-override]: Method `bar` is decorated with `@override` but does not override anything
|
||||
--> src/mdtest_snippet.pyi:163:9
|
||||
|
|
||||
161 | @identity
|
||||
162 | @identity
|
||||
163 | def bar(self): ... # error: [invalid-explicit-override]
|
||||
| ^^^
|
||||
|
|
||||
::: src/mdtest_snippet.pyi:144:5
|
||||
|
|
||||
143 | class Foo:
|
||||
144 | @override
|
||||
| ---------
|
||||
145 | @identity
|
||||
146 | @identity
|
||||
|
|
||||
info: No `bar` definitions were found on any superclasses of `Foo`
|
||||
info: rule `invalid-explicit-override` is enabled by default
|
||||
|
||||
```
|
||||
@@ -1,109 +0,0 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
---
|
||||
mdtest name: ty_ignore.md - Suppressing errors with `ty: ignore` - Multiple unused comments
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/suppressions/ty_ignore.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## mdtest_snippet.py
|
||||
|
||||
```
|
||||
1 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive"
|
||||
2 | a = 10 / 2 # ty: ignore[division-by-zero, unresolved-reference]
|
||||
3 |
|
||||
4 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment'"
|
||||
5 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'unresolved-reference'"
|
||||
6 | a = 10 / 0 # ty: ignore[invalid-assignment, division-by-zero, unresolved-reference]
|
||||
7 |
|
||||
8 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment', 'unresolved-reference'"
|
||||
9 | a = 10 / 0 # ty: ignore[invalid-assignment, unresolved-reference, division-by-zero]
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
info[unused-ignore-comment]: Unused `ty: ignore` directive
|
||||
--> src/mdtest_snippet.py:2:13
|
||||
|
|
||||
1 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive"
|
||||
2 | a = 10 / 2 # ty: ignore[division-by-zero, unresolved-reference]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
3 |
|
||||
4 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment'"
|
||||
|
|
||||
help: Remove the unused suppression comment
|
||||
1 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive"
|
||||
- a = 10 / 2 # ty: ignore[division-by-zero, unresolved-reference]
|
||||
2 + a = 10 / 2
|
||||
3 |
|
||||
4 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment'"
|
||||
5 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'unresolved-reference'"
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
info[unused-ignore-comment]: Unused `ty: ignore` directive: 'invalid-assignment'
|
||||
--> src/mdtest_snippet.py:6:26
|
||||
|
|
||||
4 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment'"
|
||||
5 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'unresolved-reference'"
|
||||
6 | a = 10 / 0 # ty: ignore[invalid-assignment, division-by-zero, unresolved-reference]
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
7 |
|
||||
8 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment', 'unresolved-reference'"
|
||||
|
|
||||
help: Remove the unused suppression code
|
||||
3 |
|
||||
4 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment'"
|
||||
5 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'unresolved-reference'"
|
||||
- a = 10 / 0 # ty: ignore[invalid-assignment, division-by-zero, unresolved-reference]
|
||||
6 + a = 10 / 0 # ty: ignore[division-by-zero, unresolved-reference]
|
||||
7 |
|
||||
8 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment', 'unresolved-reference'"
|
||||
9 | a = 10 / 0 # ty: ignore[invalid-assignment, unresolved-reference, division-by-zero]
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
info[unused-ignore-comment]: Unused `ty: ignore` directive: 'unresolved-reference'
|
||||
--> src/mdtest_snippet.py:6:64
|
||||
|
|
||||
4 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment'"
|
||||
5 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'unresolved-reference'"
|
||||
6 | a = 10 / 0 # ty: ignore[invalid-assignment, division-by-zero, unresolved-reference]
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
7 |
|
||||
8 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment', 'unresolved-reference'"
|
||||
|
|
||||
help: Remove the unused suppression code
|
||||
3 |
|
||||
4 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment'"
|
||||
5 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'unresolved-reference'"
|
||||
- a = 10 / 0 # ty: ignore[invalid-assignment, division-by-zero, unresolved-reference]
|
||||
6 + a = 10 / 0 # ty: ignore[invalid-assignment, division-by-zero]
|
||||
7 |
|
||||
8 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment', 'unresolved-reference'"
|
||||
9 | a = 10 / 0 # ty: ignore[invalid-assignment, unresolved-reference, division-by-zero]
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
info[unused-ignore-comment]: Unused `ty: ignore` directive: 'invalid-assignment', 'unresolved-reference'
|
||||
--> src/mdtest_snippet.py:9:26
|
||||
|
|
||||
8 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment', 'unresolved-reference'"
|
||||
9 | a = 10 / 0 # ty: ignore[invalid-assignment, unresolved-reference, division-by-zero]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: Remove the unused suppression codes
|
||||
6 | a = 10 / 0 # ty: ignore[invalid-assignment, division-by-zero, unresolved-reference]
|
||||
7 |
|
||||
8 | # error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment', 'unresolved-reference'"
|
||||
- a = 10 / 0 # ty: ignore[invalid-assignment, unresolved-reference, division-by-zero]
|
||||
9 + a = 10 / 0 # ty: ignore[division-by-zero]
|
||||
|
||||
```
|
||||
@@ -1,72 +0,0 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
---
|
||||
mdtest name: type_ignore.md - Suppressing errors with `type: ignore` - Nested comments
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/suppressions/type_ignore.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## mdtest_snippet.py
|
||||
|
||||
```
|
||||
1 | # fmt: off
|
||||
2 | a = test /
|
||||
3 | + 2 # fmt: skip # type: ignore
|
||||
4 |
|
||||
5 | a = test /
|
||||
6 | + 2 # type: ignore # fmt: skip
|
||||
7 |
|
||||
8 | a = (3
|
||||
9 | # error: [unused-ignore-comment]
|
||||
10 | + 2) # ty:ignore[division-by-zero] # fmt: skip
|
||||
11 |
|
||||
12 | a = (3
|
||||
13 | # error: [unused-ignore-comment]
|
||||
14 | + 2) # fmt: skip # ty:ignore[division-by-zero]
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
info[unused-ignore-comment]: Unused `ty: ignore` directive
|
||||
--> src/mdtest_snippet.py:10:9
|
||||
|
|
||||
8 | a = (3
|
||||
9 | # error: [unused-ignore-comment]
|
||||
10 | + 2) # ty:ignore[division-by-zero] # fmt: skip
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
11 |
|
||||
12 | a = (3
|
||||
|
|
||||
help: Remove the unused suppression comment
|
||||
7 |
|
||||
8 | a = (3
|
||||
9 | # error: [unused-ignore-comment]
|
||||
- + 2) # ty:ignore[division-by-zero] # fmt: skip
|
||||
10 + + 2) # fmt: skip
|
||||
11 |
|
||||
12 | a = (3
|
||||
13 | # error: [unused-ignore-comment]
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
info[unused-ignore-comment]: Unused `ty: ignore` directive
|
||||
--> src/mdtest_snippet.py:14:21
|
||||
|
|
||||
12 | a = (3
|
||||
13 | # error: [unused-ignore-comment]
|
||||
14 | + 2) # fmt: skip # ty:ignore[division-by-zero]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: Remove the unused suppression comment
|
||||
11 |
|
||||
12 | a = (3
|
||||
13 | # error: [unused-ignore-comment]
|
||||
- + 2) # fmt: skip # ty:ignore[division-by-zero]
|
||||
14 + + 2) # fmt: skip
|
||||
|
||||
```
|
||||
@@ -1,34 +0,0 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
---
|
||||
mdtest name: typed_dict.md - `TypedDict` - Error cases - `typing.TypedDict` is not allowed in type expressions
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/typed_dict.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## mdtest_snippet.py
|
||||
|
||||
```
|
||||
1 | from typing import TypedDict
|
||||
2 |
|
||||
3 | # error: [invalid-type-form] "The special form `typing.TypedDict` is not allowed in type expressions"
|
||||
4 | x: TypedDict = {"name": "Alice"}
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[invalid-type-form]: The special form `typing.TypedDict` is not allowed in type expressions
|
||||
--> src/mdtest_snippet.py:4:4
|
||||
|
|
||||
3 | # error: [invalid-type-form] "The special form `typing.TypedDict` is not allowed in type expressions"
|
||||
4 | x: TypedDict = {"name": "Alice"}
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
help: You might have meant to use a concrete TypedDict or `collections.abc.Mapping[str, object]`
|
||||
info: rule `invalid-type-form` is enabled by default
|
||||
|
||||
```
|
||||
@@ -1,45 +0,0 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
---
|
||||
mdtest name: unreachable.md - Unreachable code - `Never`-inferred variables in type expressions
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/unreachable.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## module.py
|
||||
|
||||
```
|
||||
1 | import sys
|
||||
2 |
|
||||
3 | if sys.version_info >= (3, 14):
|
||||
4 | raise RuntimeError("this library doesn't support 3.14 yet!!!")
|
||||
5 |
|
||||
6 | class AwesomeAPI: ...
|
||||
```
|
||||
|
||||
## main.py
|
||||
|
||||
```
|
||||
1 | import module
|
||||
2 |
|
||||
3 | def f(x: module.AwesomeAPI): ... # error: [invalid-type-form]
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[invalid-type-form]: Variable of type `Never` is not allowed in a type expression
|
||||
--> src/main.py:3:10
|
||||
|
|
||||
1 | import module
|
||||
2 |
|
||||
3 | def f(x: module.AwesomeAPI): ... # error: [invalid-type-form]
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: The variable may have been inferred as `Never` because its definition was inferred as being unreachable
|
||||
info: rule `invalid-type-form` is enabled by default
|
||||
|
||||
```
|
||||
@@ -117,6 +117,6 @@ from typing import no_type_check
|
||||
|
||||
@no_type_check
|
||||
def test():
|
||||
# error: [unused-ignore-comment] "Unused `ty: ignore` directive"
|
||||
# error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'unresolved-reference'"
|
||||
return x + 5 # ty: ignore[unresolved-reference]
|
||||
```
|
||||
|
||||
@@ -18,7 +18,7 @@ a = 4 + test # ty: ignore[unresolved-reference]
|
||||
|
||||
```py
|
||||
test = 10
|
||||
# error: [unused-ignore-comment] "Unused `ty: ignore` directive"
|
||||
# error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'possibly-unresolved-reference'"
|
||||
a = test + 3 # ty: ignore[possibly-unresolved-reference]
|
||||
```
|
||||
|
||||
@@ -26,7 +26,7 @@ a = test + 3 # ty: ignore[possibly-unresolved-reference]
|
||||
|
||||
```py
|
||||
# error: [unresolved-reference]
|
||||
# error: [unused-ignore-comment] "Unused `ty: ignore` directive"
|
||||
# error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'possibly-unresolved-reference'"
|
||||
a = test + 3 # ty: ignore[possibly-unresolved-reference]
|
||||
```
|
||||
|
||||
@@ -50,20 +50,17 @@ a = 10 / 0 # ty: ignore[division-by-zero, unused-ignore-comment]
|
||||
|
||||
## Multiple unused comments
|
||||
|
||||
ty groups unused codes that are next to each other.
|
||||
|
||||
<!-- snapshot-diagnostics -->
|
||||
Today, ty emits a diagnostic for every unused code. We might want to group the codes by comment at
|
||||
some point in the future.
|
||||
|
||||
```py
|
||||
# error: [unused-ignore-comment] "Unused `ty: ignore` directive"
|
||||
# error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'division-by-zero'"
|
||||
# error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'unresolved-reference'"
|
||||
a = 10 / 2 # ty: ignore[division-by-zero, unresolved-reference]
|
||||
|
||||
# error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment'"
|
||||
# error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'unresolved-reference'"
|
||||
a = 10 / 0 # ty: ignore[invalid-assignment, division-by-zero, unresolved-reference]
|
||||
|
||||
# error: [unused-ignore-comment] "Unused `ty: ignore` directive: 'invalid-assignment', 'unresolved-reference'"
|
||||
a = 10 / 0 # ty: ignore[invalid-assignment, unresolved-reference, division-by-zero]
|
||||
```
|
||||
|
||||
## Multiple suppressions
|
||||
|
||||
@@ -96,8 +96,6 @@ a = test # type: ignore[name-defined]
|
||||
|
||||
## Nested comments
|
||||
|
||||
<!-- snapshot-diagnostics -->
|
||||
|
||||
```py
|
||||
# fmt: off
|
||||
a = test \
|
||||
@@ -105,14 +103,6 @@ a = test \
|
||||
|
||||
a = test \
|
||||
+ 2 # type: ignore # fmt: skip
|
||||
|
||||
a = (3
|
||||
# error: [unused-ignore-comment]
|
||||
+ 2) # ty:ignore[division-by-zero] # fmt: skip
|
||||
|
||||
a = (3
|
||||
# error: [unused-ignore-comment]
|
||||
+ 2) # fmt: skip # ty:ignore[division-by-zero]
|
||||
```
|
||||
|
||||
## Misspelled `type: ignore`
|
||||
|
||||
@@ -1397,7 +1397,7 @@ msg = Message(id=1, content="Hello")
|
||||
# No errors for yet-unsupported features (`closed`):
|
||||
OtherMessage = TypedDict("OtherMessage", {"id": int, "content": str}, closed=True)
|
||||
|
||||
reveal_type(Message.__required_keys__) # revealed: @Todo(Support for functional `TypedDict`)
|
||||
reveal_type(Message.__required_keys__) # revealed: @Todo(Support for `TypedDict`)
|
||||
|
||||
# TODO: this should be an error
|
||||
msg.content
|
||||
@@ -1407,12 +1407,10 @@ msg.content
|
||||
|
||||
### `typing.TypedDict` is not allowed in type expressions
|
||||
|
||||
<!-- snapshot-diagnostics -->
|
||||
|
||||
```py
|
||||
from typing import TypedDict
|
||||
|
||||
# error: [invalid-type-form] "The special form `typing.TypedDict` is not allowed in type expressions"
|
||||
# error: [invalid-type-form] "The special form `typing.TypedDict` is not allowed in type expressions."
|
||||
x: TypedDict = {"name": "Alice"}
|
||||
```
|
||||
|
||||
|
||||
@@ -581,35 +581,3 @@ if False:
|
||||
|
||||
1 / number
|
||||
```
|
||||
|
||||
## `Never`-inferred variables in type expressions
|
||||
|
||||
We offer a helpful subdiagnostic if a variable in a type expression is inferred as having type
|
||||
`Never`, since this almost certainly resulted in the definition of the type being inferred by ty as
|
||||
being unreachable:
|
||||
|
||||
<!-- snapshot-diagnostics -->
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python-version = "3.14"
|
||||
```
|
||||
|
||||
`module.py`:
|
||||
|
||||
```py
|
||||
import sys
|
||||
|
||||
if sys.version_info >= (3, 14):
|
||||
raise RuntimeError("this library doesn't support 3.14 yet!!!")
|
||||
|
||||
class AwesomeAPI: ...
|
||||
```
|
||||
|
||||
`main.py`:
|
||||
|
||||
```py
|
||||
import module
|
||||
|
||||
def f(x: module.AwesomeAPI): ... # error: [invalid-type-form]
|
||||
```
|
||||
|
||||
@@ -1,11 +1,5 @@
|
||||
use crate::{
|
||||
Db, Program, PythonVersionWithSource, lint::lint_documentation_url, types::TypeCheckDiagnostics,
|
||||
};
|
||||
use ruff_db::{
|
||||
diagnostic::{Annotation, Diagnostic, DiagnosticId, SubDiagnostic, SubDiagnosticSeverity},
|
||||
files::File,
|
||||
};
|
||||
use std::cell::RefCell;
|
||||
use crate::{Db, Program, PythonVersionWithSource};
|
||||
use ruff_db::diagnostic::{Annotation, Diagnostic, SubDiagnostic, SubDiagnosticSeverity};
|
||||
use std::fmt::Write;
|
||||
|
||||
/// Suggest a name from `existing_names` that is similar to `wrong_name`.
|
||||
@@ -150,129 +144,3 @@ where
|
||||
|
||||
buffer
|
||||
}
|
||||
|
||||
/// An abstraction for mutating a diagnostic.
|
||||
///
|
||||
/// Callers likely should use `LintDiagnosticGuard` via
|
||||
/// `InferContext::report_lint` instead. This guard is only intended for use
|
||||
/// with non-lint diagnostics or non-type checking diagnostics. It is fundamentally lower level and easier to
|
||||
/// get things wrong by using it.
|
||||
///
|
||||
/// Unlike `LintDiagnosticGuard`, this API does not guarantee that the
|
||||
/// constructed `Diagnostic` not only has a primary annotation, but its
|
||||
/// associated file is equivalent to the file being type checked. As a result,
|
||||
/// if either is violated, then the `Drop` impl on `DiagnosticGuard` will
|
||||
/// panic.
|
||||
pub(super) struct DiagnosticGuard<'sink> {
|
||||
/// The file of the primary span (to which file does this diagnostic belong).
|
||||
file: File,
|
||||
|
||||
/// The target where to emit the diagnostic to.
|
||||
///
|
||||
/// We use a [`RefCell`] here over a `&mut TypeCheckDiagnostics` to ensure the fact that
|
||||
/// `InferContext` (and other contexts with diagnostics) use a [`RefCell`] internally
|
||||
/// remains abstracted away. Specifically, we want to ensure that calling `report_lint` on
|
||||
/// `InferContext` twice doesn't result in a panic:
|
||||
///
|
||||
/// ```ignore
|
||||
/// let diag1 = context.report_lint(...);
|
||||
///
|
||||
/// // would panic if using a `&mut TypeCheckDiagnostics`
|
||||
/// // because of a second mutable borrow.
|
||||
/// let diag2 = context.report_lint(...);
|
||||
/// ```
|
||||
sink: &'sink RefCell<TypeCheckDiagnostics>,
|
||||
|
||||
/// The diagnostic that we want to report.
|
||||
///
|
||||
/// This is always `Some` until the `Drop` impl.
|
||||
diag: Option<Diagnostic>,
|
||||
}
|
||||
|
||||
impl<'sink> DiagnosticGuard<'sink> {
|
||||
pub(crate) fn new(
|
||||
file: File,
|
||||
sink: &'sink std::cell::RefCell<TypeCheckDiagnostics>,
|
||||
diag: Diagnostic,
|
||||
) -> Self {
|
||||
Self {
|
||||
file,
|
||||
sink,
|
||||
diag: Some(diag),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for DiagnosticGuard<'_> {
|
||||
type Target = Diagnostic;
|
||||
|
||||
fn deref(&self) -> &Diagnostic {
|
||||
// OK because `self.diag` is only `None` within `Drop`.
|
||||
self.diag.as_ref().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a mutable borrow of the diagnostic in this guard.
|
||||
///
|
||||
/// Callers may mutate the diagnostic to add new sub-diagnostics
|
||||
/// or annotations.
|
||||
///
|
||||
/// The diagnostic is added to the typing context, if appropriate,
|
||||
/// when this guard is dropped.
|
||||
impl std::ops::DerefMut for DiagnosticGuard<'_> {
|
||||
fn deref_mut(&mut self) -> &mut Diagnostic {
|
||||
// OK because `self.diag` is only `None` within `Drop`.
|
||||
self.diag.as_mut().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
/// Finishes use of this guard.
|
||||
///
|
||||
/// This will add the diagnostic to the typing context if appropriate.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This panics when the underlying diagnostic lacks a primary
|
||||
/// annotation, or if it has one and its file doesn't match the file
|
||||
/// being type checked.
|
||||
impl Drop for DiagnosticGuard<'_> {
|
||||
fn drop(&mut self) {
|
||||
if std::thread::panicking() {
|
||||
// Don't submit diagnostics when panicking because they might be incomplete.
|
||||
return;
|
||||
}
|
||||
|
||||
// OK because the only way `self.diag` is `None`
|
||||
// is via this impl, which can only run at most
|
||||
// once.
|
||||
let mut diag = self.diag.take().unwrap();
|
||||
|
||||
let Some(ann) = diag.primary_annotation() else {
|
||||
panic!(
|
||||
"All diagnostics reported by `InferContext` must have a \
|
||||
primary annotation, but diagnostic {id} does not",
|
||||
id = diag.id(),
|
||||
);
|
||||
};
|
||||
|
||||
let expected_file = self.file;
|
||||
let got_file = ann.get_span().expect_ty_file();
|
||||
assert_eq!(
|
||||
expected_file,
|
||||
got_file,
|
||||
"All diagnostics reported by `InferContext` must have a \
|
||||
primary annotation whose file matches the file of the \
|
||||
current typing context, but diagnostic {id} has file \
|
||||
{got_file:?} and we expected {expected_file:?}",
|
||||
id = diag.id(),
|
||||
);
|
||||
|
||||
if let DiagnosticId::Lint(lint_name) = diag.id()
|
||||
&& diag.documentation_url().is_none()
|
||||
{
|
||||
diag.set_documentation_url(Some(lint_documentation_url(lint_name)));
|
||||
}
|
||||
|
||||
self.sink.borrow_mut().push(diag);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ impl TypeOrigin {
|
||||
/// bound_or_declared: Place::Defined(Literal[1], TypeOrigin::Inferred, Definedness::PossiblyUndefined),
|
||||
/// non_existent: Place::Undefined,
|
||||
/// ```
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, salsa::Update, get_size2::GetSize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, salsa::Update, get_size2::GetSize)]
|
||||
pub(crate) enum Place<'db> {
|
||||
Defined(Type<'db>, TypeOrigin, Definedness),
|
||||
Undefined,
|
||||
@@ -532,7 +532,7 @@ impl<'db> PlaceFromDeclarationsResult<'db> {
|
||||
/// that this comes with a [`CLASS_VAR`] type qualifier.
|
||||
///
|
||||
/// [`CLASS_VAR`]: crate::types::TypeQualifiers::CLASS_VAR
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, salsa::Update, get_size2::GetSize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, salsa::Update, get_size2::GetSize)]
|
||||
pub(crate) struct PlaceAndQualifiers<'db> {
|
||||
pub(crate) place: Place<'db>,
|
||||
pub(crate) qualifiers: TypeQualifiers,
|
||||
@@ -689,51 +689,6 @@ impl<'db> PlaceAndQualifiers<'db> {
|
||||
.or_else(|lookup_error| lookup_error.or_fall_back_to(db, fallback_fn()))
|
||||
.into()
|
||||
}
|
||||
|
||||
pub(crate) fn cycle_normalized(
|
||||
self,
|
||||
db: &'db dyn Db,
|
||||
previous_place: Self,
|
||||
cycle: &salsa::Cycle,
|
||||
) -> Self {
|
||||
let place = match (previous_place.place, self.place) {
|
||||
// In fixed-point iteration of type inference, the member type must be monotonically widened and not "oscillate".
|
||||
// Here, monotonicity is guaranteed by pre-unioning the type of the previous iteration into the current result.
|
||||
(Place::Defined(prev_ty, _, _), Place::Defined(ty, origin, definedness)) => {
|
||||
Place::Defined(ty.cycle_normalized(db, prev_ty, cycle), origin, definedness)
|
||||
}
|
||||
// If a `Place` in the current cycle is `Defined` but `Undefined` in the previous cycle,
|
||||
// that means that its definedness depends on the truthiness of the previous cycle value.
|
||||
// In this case, the definedness of the current cycle `Place` is set to `PossiblyUndefined`.
|
||||
// Actually, this branch is unreachable. We evaluate the truthiness of non-definitely-bound places as Ambiguous (see #19579),
|
||||
// so convergence is guaranteed without resorting to this handling.
|
||||
// However, the handling described above may reduce the exactness of reachability analysis,
|
||||
// so it may be better to remove it. In that case, this branch is necessary.
|
||||
(Place::Undefined, Place::Defined(ty, origin, _definedness)) => Place::Defined(
|
||||
ty.recursive_type_normalized(db, cycle),
|
||||
origin,
|
||||
Definedness::PossiblyUndefined,
|
||||
),
|
||||
// If a `Place` that was `Defined(Divergent)` in the previous cycle is actually found to be unreachable in the current cycle,
|
||||
// it is set to `Undefined` (because the cycle initial value does not include meaningful reachability information).
|
||||
(Place::Defined(ty, origin, _definedness), Place::Undefined) => {
|
||||
if cycle.head_ids().any(|id| ty == Type::divergent(id)) {
|
||||
Place::Undefined
|
||||
} else {
|
||||
Place::Defined(
|
||||
ty.recursive_type_normalized(db, cycle),
|
||||
origin,
|
||||
Definedness::PossiblyUndefined,
|
||||
)
|
||||
}
|
||||
}
|
||||
(Place::Undefined, Place::Undefined) => Place::Undefined,
|
||||
};
|
||||
PlaceAndQualifiers {
|
||||
place,
|
||||
qualifiers: self.qualifiers,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'db> From<Place<'db>> for PlaceAndQualifiers<'db> {
|
||||
@@ -744,30 +699,16 @@ impl<'db> From<Place<'db>> for PlaceAndQualifiers<'db> {
|
||||
|
||||
fn place_cycle_initial<'db>(
|
||||
_db: &'db dyn Db,
|
||||
id: salsa::Id,
|
||||
_id: salsa::Id,
|
||||
_scope: ScopeId<'db>,
|
||||
_place_id: ScopedPlaceId,
|
||||
_requires_explicit_reexport: RequiresExplicitReExport,
|
||||
_considered_definitions: ConsideredDefinitions,
|
||||
) -> PlaceAndQualifiers<'db> {
|
||||
Place::bound(Type::divergent(id)).into()
|
||||
Place::bound(Type::Never).into()
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn place_cycle_recover<'db>(
|
||||
db: &'db dyn Db,
|
||||
cycle: &salsa::Cycle,
|
||||
previous_place: &PlaceAndQualifiers<'db>,
|
||||
place: PlaceAndQualifiers<'db>,
|
||||
_scope: ScopeId<'db>,
|
||||
_place_id: ScopedPlaceId,
|
||||
_requires_explicit_reexport: RequiresExplicitReExport,
|
||||
_considered_definitions: ConsideredDefinitions,
|
||||
) -> PlaceAndQualifiers<'db> {
|
||||
place.cycle_normalized(db, *previous_place, cycle)
|
||||
}
|
||||
|
||||
#[salsa::tracked(cycle_fn=place_cycle_recover, cycle_initial=place_cycle_initial, heap_size=ruff_memory_usage::heap_size)]
|
||||
#[salsa::tracked(cycle_initial=place_cycle_initial, heap_size=ruff_memory_usage::heap_size)]
|
||||
pub(crate) fn place_by_id<'db>(
|
||||
db: &'db dyn Db,
|
||||
scope: ScopeId<'db>,
|
||||
|
||||
@@ -2255,8 +2255,6 @@ impl<'ast> Visitor<'ast> for SemanticIndexBuilder<'_, 'ast> {
|
||||
names,
|
||||
}) => {
|
||||
for name in names {
|
||||
self.scopes_by_expression
|
||||
.record_expression(name, self.current_scope());
|
||||
let symbol_id = self.add_symbol(name.id.clone());
|
||||
let symbol = self.current_place_table().symbol(symbol_id);
|
||||
// Check whether the variable has already been accessed in this scope.
|
||||
@@ -2292,8 +2290,6 @@ impl<'ast> Visitor<'ast> for SemanticIndexBuilder<'_, 'ast> {
|
||||
names,
|
||||
}) => {
|
||||
for name in names {
|
||||
self.scopes_by_expression
|
||||
.record_expression(name, self.current_scope());
|
||||
let symbol_id = self.add_symbol(name.id.clone());
|
||||
let symbol = self.current_place_table().symbol(symbol_id);
|
||||
// Check whether the variable has already been accessed in this scope.
|
||||
|
||||
@@ -90,6 +90,12 @@ impl<'db> Definition<'db> {
|
||||
.to_string(),
|
||||
)
|
||||
}
|
||||
DefinitionKind::Assignment(assignment) => {
|
||||
let target_node = assignment.target.node(&module);
|
||||
target_node
|
||||
.as_name_expr()
|
||||
.map(|name_expr| name_expr.id.as_str().to_string())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user