Skip to content

Commit 4470bdf

Browse files
committed
more comments
1 parent 2eea054 commit 4470bdf

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

crates/ty_python_semantic/resources/mdtest/pep695_type_aliases.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ def f(x: IntOr, y: OrInt):
248248
# error: [cyclic-type-alias-definition] "Cyclic definition of `Itself`"
249249
type Itself = Itself
250250

251-
def foo(Itself: Itself):
251+
def foo(
252+
# this is a very strange thing to do, but this is a regression test to ensure it doesn't panic
253+
Itself: Itself,
254+
):
252255
x: Itself
253256
reveal_type(Itself) # revealed: Divergent
254257

@@ -271,6 +274,9 @@ type G[T] = G[T]
271274
type H[T] = I[T]
272275
# error: [cyclic-type-alias-definition] "Cyclic definition of `I`"
273276
type I[T] = H[T]
277+
278+
# It's not possible to create an element of this type, but it's not an error for now
279+
type DirectRecursiveList[T] = list[DirectRecursiveList[T]]
274280
```
275281

276282
### With legacy generic

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,18 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
19031903
self.infer_annotation_expression(&type_alias.value, DeferredExpressionState::None);
19041904

19051905
// A type alias where a value type points to itself, i.e. the expanded type is `Divergent` is meaningless
1906+
// (but a type alias that expands to something like `list[Divergent]` may be a valid recursive type alias)
19061907
// and would lead to infinite recursion. Therefore, such type aliases should not be exposed.
1908+
// ```python
1909+
// type Itself = Itself # error: "Cyclic definition of `Itself`"
1910+
// type A = B # error: "Cyclic definition of `A`"
1911+
// type B = A # error: "Cyclic definition of `B`"
1912+
// type G[T] = G[T] # error: "Cyclic definition of `G`"
1913+
// type RecursiveList[T] = list[T | RecursiveList[T]] # OK
1914+
// type RecursiveList2[T] = list[RecursiveList2[T]] # It's not possible to create an element of this, but it's not an error for now
1915+
// type IntOrStr = int | StrOrInt # It's redundant, but OK for now
1916+
// type StrOrInt = str | IntOrStr # It's redundant, but OK for now
1917+
// ```
19071918
let expanded = value_ty.inner_type().expand_eagerly(self.db());
19081919
if expanded.is_divergent() {
19091920
if let Some(builder) = self

0 commit comments

Comments
 (0)