mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:39:12 +00:00
[red-knot] use declared types in inference/checking (#13335)
Use declared types in inference and checking. This means several things: * Imports prefer declarations over inference, when declarations are available. * When we encounter a binding, we check that the bound value's inferred type is assignable to the live declarations of the bound symbol, if any. * When we encounter a declaration, we check that the declared type is assignable from the inferred type of the symbol from previous bindings, if any. * When we encounter a binding+declaration, we check that the inferred type of the bound value is assignable to the declared type.
This commit is contained in:
parent
d86e5ad031
commit
dcfebaa4a8
13 changed files with 876 additions and 233 deletions
52
crates/ruff_db/src/display.rs
Normal file
52
crates/ruff_db/src/display.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
use std::fmt::{self, Display, Formatter};
|
||||
|
||||
pub trait FormatterJoinExtension<'b> {
|
||||
fn join<'a>(&'a mut self, separator: &'static str) -> Join<'a, 'b>;
|
||||
}
|
||||
|
||||
impl<'b> FormatterJoinExtension<'b> for Formatter<'b> {
|
||||
fn join<'a>(&'a mut self, separator: &'static str) -> Join<'a, 'b> {
|
||||
Join {
|
||||
fmt: self,
|
||||
separator,
|
||||
result: fmt::Result::Ok(()),
|
||||
seen_first: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Join<'a, 'b> {
|
||||
fmt: &'a mut Formatter<'b>,
|
||||
separator: &'static str,
|
||||
result: fmt::Result,
|
||||
seen_first: bool,
|
||||
}
|
||||
|
||||
impl<'a, 'b> Join<'a, 'b> {
|
||||
pub fn entry(&mut self, item: &dyn Display) -> &mut Self {
|
||||
if self.seen_first {
|
||||
self.result = self
|
||||
.result
|
||||
.and_then(|()| self.fmt.write_str(self.separator));
|
||||
} else {
|
||||
self.seen_first = true;
|
||||
}
|
||||
self.result = self.result.and_then(|()| item.fmt(self.fmt));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn entries<I, F>(&mut self, items: I) -> &mut Self
|
||||
where
|
||||
I: IntoIterator<Item = F>,
|
||||
F: Display,
|
||||
{
|
||||
for item in items {
|
||||
self.entry(&item);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn finish(&mut self) -> fmt::Result {
|
||||
self.result
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ use crate::files::Files;
|
|||
use crate::system::System;
|
||||
use crate::vendored::VendoredFileSystem;
|
||||
|
||||
pub mod display;
|
||||
pub mod file_revision;
|
||||
pub mod files;
|
||||
pub mod parsed;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue