[inspections] PY-85120 Do not consider expressions like a: list[int] | None = None a type alias

Target expression should have either explicit `TypeAlias` annotation or valid type expression in assignment without type annotation (to be considered implicit type alias)

(cherry picked from commit 1059a8df2d221bd068eee706139be20c256df549)

GitOrigin-RevId: 8c952138fbc4124f02f717ce8a1784c1f0dc86b2
This commit is contained in:
Daniil Kalinin
2025-11-03 10:33:56 +00:00
committed by intellij-monorepo-bot
parent 1b197eb884
commit 489b9b90aa
4 changed files with 15 additions and 2 deletions
@@ -60,7 +60,7 @@ public class PyFileElementType extends IStubFileElementType<PyFileStub> {
@Override
public int getStubVersion() {
// Don't forget to update versions of indexes that use the updated stub-based elements
return 103;
return 104;
}
@Override
@@ -80,7 +80,7 @@ public final class PyTypingAliasStubType extends CustomTargetExpressionStubType<
return null;
}
if (isExplicitTypeAlias(target) || looksLikeTypeHint(value)) {
if (isExplicitTypeAlias(target) || (looksLikeTypeHint(value) && target.getAnnotation() == null)) {
return value;
}
return null;
+3
View File
@@ -22,6 +22,9 @@ bin3_ok = Union[str, bool] | None
bin4_notOk = str & int
list_notOk = [int, str]
bin5_notOk: int | str = "foo"
list_none_notOk: list[int] | None = None
callable_ok = Callable[[int, str], bool]
explicit_alias1_ok: TypeAlias = 'Foo is bar.'
@@ -3189,6 +3189,16 @@ public class PyTypeHintsInspectionTest extends PyInspectionTestCase {
""");
}
// PY-85120
public void testTargetExpressionWithAnnotationNotConsideredTypeAlias() {
doTestByText("""
a: list[int] | None = None # Not a type alias
if a:
_ = a[1] # No error expected
""");
}
@NotNull
@Override
protected Class<? extends PyInspection> getInspectionClass() {