mirror of
				https://github.com/astral-sh/ruff.git
				synced 2025-10-31 12:05:57 +00:00 
			
		
		
		
	[ty] Prefer declared type for invariant collection literals (#20927)
	
		
			
	
		
	
	
		
	
		
			Some checks are pending
		
		
	
	
		
			
				
	
				CI / Determine changes (push) Waiting to run
				
			
		
			
				
	
				CI / cargo fmt (push) Waiting to run
				
			
		
			
				
	
				CI / cargo clippy (push) Blocked by required conditions
				
			
		
			
				
	
				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 (macos) (push) Blocked by required conditions
				
			
		
			
				
	
				CI / cargo test (wasm) (push) Blocked by required conditions
				
			
		
			
				
	
				CI / cargo build (msrv) (push) Blocked by required conditions
				
			
		
			
				
	
				CI / cargo fuzz build (push) Blocked by required conditions
				
			
		
			
				
	
				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 / ty completion evaluation (push) Blocked by required conditions
				
			
		
			
				
	
				CI / python package (push) Waiting to run
				
			
		
			
				
	
				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
				
			
		
			
				
	
				CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
				
			
		
			
				
	
				CI / benchmarks instrumented (ty) (push) Blocked by required conditions
				
			
		
			
				
	
				CI / benchmarks walltime (medium|multithreaded) (push) Blocked by required conditions
				
			
		
			
				
	
				CI / benchmarks walltime (small|large) (push) Blocked by required conditions
				
			
		
			
				
	
				[ty Playground] Release / publish (push) Waiting to run
				
			
		
		
	
	
				
					
				
			
		
			Some checks are pending
		
		
	
	CI / Determine changes (push) Waiting to run
				
			CI / cargo fmt (push) Waiting to run
				
			CI / cargo clippy (push) Blocked by required conditions
				
			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 (macos) (push) Blocked by required conditions
				
			CI / cargo test (wasm) (push) Blocked by required conditions
				
			CI / cargo build (msrv) (push) Blocked by required conditions
				
			CI / cargo fuzz build (push) Blocked by required conditions
				
			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 / ty completion evaluation (push) Blocked by required conditions
				
			CI / python package (push) Waiting to run
				
			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
				
			CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
				
			CI / benchmarks instrumented (ty) (push) Blocked by required conditions
				
			CI / benchmarks walltime (medium|multithreaded) (push) Blocked by required conditions
				
			CI / benchmarks walltime (small|large) (push) Blocked by required conditions
				
			[ty Playground] Release / publish (push) Waiting to run
				
			## Summary Prefer the declared type for collection literals, e.g., ```py x: list[Any] = [1, "2", (3,)] reveal_type(x) # list[Any] ``` This solves a large part of https://github.com/astral-sh/ty/issues/136 for invariant generics, where respecting the declared type is a lot more important. It also means that annotated dict literals with `dict[_, Any]` is a way out of https://github.com/astral-sh/ty/issues/1248.
This commit is contained in:
		
							parent
							
								
									b0e10a9777
								
							
						
					
					
						commit
						96b156303b
					
				
					 3 changed files with 21 additions and 6 deletions
				
			
		|  | @ -114,7 +114,7 @@ h: list[list[int]] = [[], [42]] | ||||||
| reveal_type(h)  # revealed: list[list[int]] | reveal_type(h)  # revealed: list[list[int]] | ||||||
| 
 | 
 | ||||||
| i: list[typing.Any] = [1, 2, "3", ([4],)] | i: list[typing.Any] = [1, 2, "3", ([4],)] | ||||||
| reveal_type(i)  # revealed: list[Any | int | str | tuple[list[Unknown | int]]] | reveal_type(i)  # revealed: list[Any] | ||||||
| 
 | 
 | ||||||
| j: list[tuple[str | int, ...]] = [(1, 2), ("foo", "bar"), ()] | j: list[tuple[str | int, ...]] = [(1, 2), ("foo", "bar"), ()] | ||||||
| reveal_type(j)  # revealed: list[tuple[str | int, ...]] | reveal_type(j)  # revealed: list[tuple[str | int, ...]] | ||||||
|  | @ -123,7 +123,7 @@ k: list[tuple[list[int], ...]] = [([],), ([1, 2], [3, 4]), ([5], [6], [7])] | ||||||
| reveal_type(k)  # revealed: list[tuple[list[int], ...]] | reveal_type(k)  # revealed: list[tuple[list[int], ...]] | ||||||
| 
 | 
 | ||||||
| l: tuple[list[int], *tuple[list[typing.Any], ...], list[str]] = ([1, 2, 3], [4, 5, 6], [7, 8, 9], ["10", "11", "12"]) | l: tuple[list[int], *tuple[list[typing.Any], ...], list[str]] = ([1, 2, 3], [4, 5, 6], [7, 8, 9], ["10", "11", "12"]) | ||||||
| reveal_type(l)  # revealed: tuple[list[int], list[Any | int], list[Any | int], list[str]] | reveal_type(l)  # revealed: tuple[list[int], list[Any], list[Any], list[str]] | ||||||
| 
 | 
 | ||||||
| type IntList = list[int] | type IntList = list[int] | ||||||
| 
 | 
 | ||||||
|  | @ -187,7 +187,7 @@ h: list[list[int]] | None = [[], [42]] | ||||||
| reveal_type(h)  # revealed: list[list[int]] | reveal_type(h)  # revealed: list[list[int]] | ||||||
| 
 | 
 | ||||||
| i: list[typing.Any] | None = [1, 2, "3", ([4],)] | i: list[typing.Any] | None = [1, 2, "3", ([4],)] | ||||||
| reveal_type(i)  # revealed: list[Any | int | str | tuple[list[Unknown | int]]] | reveal_type(i)  # revealed: list[Any] | ||||||
| 
 | 
 | ||||||
| j: list[tuple[str | int, ...]] | None = [(1, 2), ("foo", "bar"), ()] | j: list[tuple[str | int, ...]] | None = [(1, 2), ("foo", "bar"), ()] | ||||||
| reveal_type(j)  # revealed: list[tuple[str | int, ...]] | reveal_type(j)  # revealed: list[tuple[str | int, ...]] | ||||||
|  | @ -196,7 +196,7 @@ k: list[tuple[list[int], ...]] | None = [([],), ([1, 2], [3, 4]), ([5], [6], [7] | ||||||
| reveal_type(k)  # revealed: list[tuple[list[int], ...]] | reveal_type(k)  # revealed: list[tuple[list[int], ...]] | ||||||
| 
 | 
 | ||||||
| l: tuple[list[int], *tuple[list[typing.Any], ...], list[str]] | None = ([1, 2, 3], [4, 5, 6], [7, 8, 9], ["10", "11", "12"]) | l: tuple[list[int], *tuple[list[typing.Any], ...], list[str]] | None = ([1, 2, 3], [4, 5, 6], [7, 8, 9], ["10", "11", "12"]) | ||||||
| reveal_type(l)  # revealed: tuple[list[int], list[Any | int], list[Any | int], list[str]] | reveal_type(l)  # revealed: tuple[list[int], list[Any], list[Any], list[str]] | ||||||
| 
 | 
 | ||||||
| type IntList = list[int] | type IntList = list[int] | ||||||
| 
 | 
 | ||||||
|  | @ -282,7 +282,7 @@ reveal_type(k)  # revealed: list[Literal[1, 2, 3]] | ||||||
| type Y[T] = list[T] | type Y[T] = list[T] | ||||||
| 
 | 
 | ||||||
| l: Y[Y[Literal[1]]] = [[1]] | l: Y[Y[Literal[1]]] = [[1]] | ||||||
| reveal_type(l)  # revealed: list[list[Literal[1]]] | reveal_type(l)  # revealed: list[Y[Literal[1]]] | ||||||
| 
 | 
 | ||||||
| m: list[tuple[Literal[1], Literal[2], Literal[3]]] = [(1, 2, 3)] | m: list[tuple[Literal[1], Literal[2], Literal[3]]] = [(1, 2, 3)] | ||||||
| reveal_type(m)  # revealed: list[tuple[Literal[1], Literal[2], Literal[3]]] | reveal_type(m)  # revealed: list[tuple[Literal[1], Literal[2], Literal[3]]] | ||||||
|  |  | ||||||
|  | @ -591,7 +591,15 @@ impl Display for DisplayRepresentation<'_> { | ||||||
|                 .0 |                 .0 | ||||||
|                 .display_with(self.db, self.settings.clone()) |                 .display_with(self.db, self.settings.clone()) | ||||||
|                 .fmt(f), |                 .fmt(f), | ||||||
|             Type::TypeAlias(alias) => f.write_str(alias.name(self.db)), |             Type::TypeAlias(alias) => { | ||||||
|  |                 f.write_str(alias.name(self.db))?; | ||||||
|  |                 match alias.specialization(self.db) { | ||||||
|  |                     None => Ok(()), | ||||||
|  |                     Some(specialization) => specialization | ||||||
|  |                         .display_short(self.db, TupleSpecialization::No, self.settings.clone()) | ||||||
|  |                         .fmt(f), | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -6179,6 +6179,13 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> { | ||||||
| 
 | 
 | ||||||
|                 let inferred_elt_ty = self.get_or_infer_expression(elt, elt_tcx); |                 let inferred_elt_ty = self.get_or_infer_expression(elt, elt_tcx); | ||||||
| 
 | 
 | ||||||
|  |                 // Simplify the inference based on the declared type of the element.
 | ||||||
|  |                 if let Some(elt_tcx) = elt_tcx.annotation { | ||||||
|  |                     if inferred_elt_ty.is_assignable_to(self.db(), elt_tcx) { | ||||||
|  |                         continue; | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|                 // Convert any element literals to their promoted type form to avoid excessively large
 |                 // Convert any element literals to their promoted type form to avoid excessively large
 | ||||||
|                 // unions for large nested list literals, which the constraint solver struggles with.
 |                 // unions for large nested list literals, which the constraint solver struggles with.
 | ||||||
|                 let inferred_elt_ty = inferred_elt_ty.promote_literals(self.db(), elt_tcx); |                 let inferred_elt_ty = inferred_elt_ty.promote_literals(self.db(), elt_tcx); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Ibraheem Ahmed
						Ibraheem Ahmed