mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:25:17 +00:00
[ty] Do not emit errors if enums or NamedTuples constructed using functional syntax are used in type expressions (#17873)
Some checks are pending
CI / python package (push) Waiting to run
CI / benchmarks (push) Blocked by required conditions
CI / cargo clippy (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
CI / python package (push) Waiting to run
CI / benchmarks (push) Blocked by required conditions
CI / cargo clippy (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
## Summary This fixes some false positives that showed up in the primer diff for https://github.com/astral-sh/ruff/pull/17832 ## Test Plan new mdtests added that fail with false-positive diagnostics on `main`
This commit is contained in:
parent
fd76d70a31
commit
89424cce5f
2 changed files with 30 additions and 0 deletions
|
@ -0,0 +1,19 @@
|
||||||
|
# Unsupported special types
|
||||||
|
|
||||||
|
We do not understand the functional syntax for creating `NamedTuple`s, `TypedDict`s or `Enum`s yet.
|
||||||
|
But we also do not emit false positives when these are used in type expressions.
|
||||||
|
|
||||||
|
```py
|
||||||
|
import collections
|
||||||
|
import enum
|
||||||
|
import typing
|
||||||
|
|
||||||
|
# TODO: should not error (requires understanding metaclass `__call__`)
|
||||||
|
MyEnum = enum.Enum("MyEnum", ["foo", "bar", "baz"]) # error: [too-many-positional-arguments]
|
||||||
|
|
||||||
|
MyTypedDict = typing.TypedDict("MyTypedDict", {"foo": int})
|
||||||
|
MyNamedTuple1 = typing.NamedTuple("MyNamedTuple1", [("foo", int)])
|
||||||
|
MyNamedTuple2 = collections.namedtuple("MyNamedTuple2", ["foo"])
|
||||||
|
|
||||||
|
def f(a: MyEnum, b: MyTypedDict, c: MyNamedTuple1, d: MyNamedTuple2): ...
|
||||||
|
```
|
|
@ -4833,6 +4833,17 @@ impl<'db> Type<'db> {
|
||||||
Some(KnownClass::UnionType) => Ok(todo_type!(
|
Some(KnownClass::UnionType) => Ok(todo_type!(
|
||||||
"Support for `types.UnionType` instances in type expressions"
|
"Support for `types.UnionType` instances in type expressions"
|
||||||
)),
|
)),
|
||||||
|
Some(KnownClass::NamedTuple) => Ok(todo_type!(
|
||||||
|
"Support for functional `typing.NamedTuple` syntax"
|
||||||
|
)),
|
||||||
|
_ if instance
|
||||||
|
.class()
|
||||||
|
.iter_mro(db)
|
||||||
|
.filter_map(ClassBase::into_class)
|
||||||
|
.any(|class| class.is_known(db, KnownClass::Enum)) =>
|
||||||
|
{
|
||||||
|
Ok(todo_type!("Support for functional `enum` syntax"))
|
||||||
|
}
|
||||||
_ => Err(InvalidTypeExpressionError {
|
_ => Err(InvalidTypeExpressionError {
|
||||||
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::InvalidType(
|
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::InvalidType(
|
||||||
*self
|
*self
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue