| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -177,8 +177,9 @@ def infer_constraints(template: Type, actual: Type, direction: int) -> list[Cons | |||
| 177 | 177 | for (t, a) in reversed(TypeState.inferring) | |
| 178 | 178 | ): | |
| 179 | 179 | return [] | |
| 180 | - if has_recursive_types(template): | ||
| 180 | + if has_recursive_types(template) or isinstance(get_proper_type(template), Instance): | ||
| 181 | 181 | # This case requires special care because it may cause infinite recursion. | |
| 182 | + # Note that we include Instances because the may be recursive as str(Sequence[str]). | ||
| 182 | 183 | if not has_type_vars(template): | |
| 183 | 184 | # Return early on an empty branch. | |
| 184 | 185 | return [] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3240,6 +3240,12 @@ def __init__(self) -> None: | |||
| 3240 | 3240 | def visit_type_var(self, t: TypeVarType) -> bool: | |
| 3241 | 3241 | return True | |
| 3242 | 3242 | ||
| 3243 | + def visit_type_var_tuple(self, t: TypeVarTupleType) -> bool: | ||
| 3244 | + return True | ||
| 3245 | + | ||
| 3246 | + def visit_param_spec(self, t: ParamSpecType) -> bool: | ||
| 3247 | + return True | ||
| 3248 | + | ||
| 3243 | 3249 | ||
| 3244 | 3250 | def has_type_vars(typ: Type) -> bool: | |
| 3245 | 3251 | """Check if a type contains any type variables (recursively).""" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -826,3 +826,14 @@ z = z | |||
| 826 | 826 | x = y # E: Incompatible types in assignment (expression has type "L", variable has type "K") | |
| 827 | 827 | z = x # OK | |
| 828 | 828 | [builtins fixtures/tuple.pyi] | |
| 829 | + | ||
| 830 | + [case testRecursiveInstanceInferenceNoCrash] | ||
| 831 | + from typing import Sequence, TypeVar, Union | ||
| 832 | + | ||
| 833 | + class C(Sequence[C]): ... | ||
| 834 | + | ||
| 835 | + T = TypeVar("T") | ||
| 836 | + def foo(x: T) -> C: ... | ||
| 837 | + | ||
| 838 | + Nested = Union[C, Sequence[Nested]] | ||
| 839 | + x: Nested = foo(42) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments