-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does crates/ruff/resources/test/fixtures/pyupgrade/UP014.py
contain incorrect NamedTuple
calls?
#5794
Comments
crates/ruff/resources/test/fixtures/pyupgrade/UP014.py
contains incorrect NamedTuple
calls?crates/ruff/resources/test/fixtures/pyupgrade/UP014.py
contain incorrect NamedTuple
calls?
or is this intended? |
If I understand this function signature correctly, no, right? def NamedTuple(typename, fields=_sentinel, /, **kwargs): edit: it's recently changed |
Is this what you saw? from typing import NamedTuple
MyType = NamedTuple(
"MyType",
[("a", int), ("b", str), ("c", list[bool])],
defaults=["foo", [True]],
)
|
I saw that, and then found this issue. What I meant by MyType = NamedTuple(
"MyType",
[("a", int), ("b", str), ("c", list[bool])],
defaults=[1, "foo", [True]],
) is an invalid class MyType(NamedTuple):
a: int = 1
b: str = "foo"
c: list[bool] = [True] Ruff currently does. |
I think it's intended, just to see how we would handle that case, even if it's not valid at runtime. |
Makes sense. Is F821 on |
Is passing |
(I assumed there was some case in which passing a list of defaults was supported, just that you couldn't mix it with specifying the fields that way.) |
from typing import NamedTuple
# valid
MyType = NamedTuple(
"MyType",
defaults=str,
)
# valid
MyType = NamedTuple(
"MyType",
defaults=list[str],
)
# invalid, doesn't throw but doesn't make sense because `[str]` is not a valid type annotation
MyType = NamedTuple(
"MyType",
defaults=[str],
)
# invalid, throws `TypeError: Either list of fields or keywords can be provided to NamedTuple, not both`
MyType = NamedTuple(
"MyType",
[("a", int), ("b", str), ("c", list[bool])],
defaults=[1, "foo", [True]],
) You can pass a list to |
In each case, |
Yeah, we should remove all the special-casing around |
E.g., this and related code seems unnecessary: /// Match the `defaults` keyword in a `NamedTuple(...)` call.
fn match_defaults(keywords: &[Keyword]) -> Result<&[Expr]> {
let defaults = keywords.iter().find(|keyword| {
if let Some(arg) = &keyword.arg {
arg == "defaults"
} else {
false
}
});
match defaults {
Some(defaults) => match &defaults.value {
Expr::List(ast::ExprList { elts, .. }) => Ok(elts),
Expr::Tuple(ast::ExprTuple { elts, .. }) => Ok(elts),
_ => bail!("Expected defaults to be `Expr::List` | `Expr::Tuple`"),
},
None => Ok(&[]),
}
} |
I agree.
ruff/crates/ruff/src/checkers/ast/mod.rs Lines 3690 to 3695 in d692ed0
Ruff thinks |
Happy to file a PR. |
…ywords (astral-sh#5799) ## Summary Fixes astral-sh#5794 ## Test Plan Existing tests
ruff/crates/ruff/resources/test/fixtures/pyupgrade/UP014.py
Lines 7 to 12 in f012ed2
Is this a valid
NamedTuple
call?The text was updated successfully, but these errors were encountered: