Conversation
|
| pub(crate) fn definition(&self, db: &'db dyn Db) -> Option<Definition<'db>> { | ||
| match self { | ||
| TypeAliasType::PEP695(type_alias) => Some(type_alias.definition(db)), | ||
| TypeAliasType::Bare(_) => None, |
There was a problem hiding this comment.
This is used for go-to-type-definition. It seems more involved to provide the original definition here, as we would need to keep track of it through various Type operations and the calling machinery.
a66b2fd to
c89a7f2
Compare
c89a7f2 to
2c0a65d
Compare
| const KNOWN_FAILURES: &[(&str, bool, bool)] = &[ | ||
| // Fails with too-many-cycle-iterations due to a self-referential | ||
| // type alias, see https://github.com/astral-sh/ty/issues/256 | ||
| ("crates/ruff_linter/resources/test/fixtures/pyflakes/F401_34.py", true, true), |
There was a problem hiding this comment.
This is unfortunate, but that file could just as easily have been written using PEP 695 type aliases …
| Some(KnownClass::TypeAliasType) => { | ||
| if let [Some(name), Some(value), ..] = overload.parameter_types() { | ||
| // TODO: emit a diagnostic if the name is not a string literal | ||
| if let Some(name) = name.into_string_literal() { | ||
| overload.set_return_type(Type::KnownInstance( | ||
| KnownInstanceType::TypeAliasType(TypeAliasType::Bare( | ||
| BareTypeAliasType::new(db, name.value(db), value), | ||
| )), | ||
| )); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
If you move this construction logic into infer.rs, like we do for legacy typevars, you'll be able to get the Definition of the assignment statement, at least for the case where a call to the TypeAliasType constructor is being immediately assigned to a variable. That will let you return a non-None result for definition up above for bare type aliases too.
ruff/crates/ty_python_semantic/src/types/infer.rs
Lines 5197 to 5199 in dd04ca7
(Moving the construction logic also made it easier to emit diagnostics for legacy typevar construction, which might help you discharge the TODO comment)
There was a problem hiding this comment.
Thank you — I didn't realize we had call-related code in infer.rs, too. That was really helpful. TODO comment is also resolved.
| /// from typing import TypeAliasType | ||
| /// | ||
| /// IntOrStr = TypeAliasType("IntOrStr", int | str) # okay | ||
| /// NewAlias = TypeAliasType(get_name(), int) # error: TypeAliasType name must be a string literal |
There was a problem hiding this comment.
I wasn't sure if we should have the same strict requirements here as for legacy TypeVar constructions. I couldn't find anything in the spec, but pyright also emits diagnostics, if a TypeAliasType is not immediately assigned to a variable, for example.
d9729e6 to
4852009
Compare
| ### Links | ||
| * [Related issues](https://github.com/astral-sh/ty/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20call-non-callable) | ||
| * [View source](https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L86) | ||
| * [View source](https://github.com/astral-sh/ruff/blob/main/crates%2Fty_python_semantic%2Fsrc%2Ftypes%2Fdiagnostic.rs#L87) |
There was a problem hiding this comment.
Did you have to update these manually?
Whew, you did not! Looks like this was auto-generated from diagnostic.rs
[Not for this PR] We might consider marking this as a generated file in .gitattributes to hide them in PR diffs
| /// from typing import TypeAliasType | ||
| /// | ||
| /// IntOrStr = TypeAliasType("IntOrStr", int | str) # okay | ||
| /// NewAlias = TypeAliasType(get_name(), int) # error: TypeAliasType name must be a string literal |
## Summary See comment here: #18156 (comment)
Summary
Support direct uses of
typing.TypeAliasType, as in:closes astral-sh/ty#392
Ecosystem
The new false positive here:
+ error[invalid-type-form] altair/utils/core.py:49:53: The first argument to `Callable` must be either a list of types, ParamSpec, Concatenate, or `...`comes from the fact that we infer the second argument as a type expression now. We silence false positives for PEP695
ParamSpecs, but not forP = ParamSpec("P")insideCallable[P, ...].Test Plan
New Markdown tests