Skip to content

Commit

Permalink
Revert comment
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jun 14, 2023
1 parent e1a1ec0 commit 0dde195
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 44 deletions.
4 changes: 2 additions & 2 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4319,11 +4319,11 @@ impl<'a> Checker<'a> {
.is_annotation()
{
self.semantic_model
.add_delayed_annotation(binding_id, shadowed_id);
.add_delayed_annotation(shadowed_id, binding_id);
return binding_id;
}

let shadowed = &self.semantic_model.bindings[binding_id];
let shadowed = &self.semantic_model.bindings[shadowed_id];
match &shadowed.kind {
BindingKind::Builtin => {
// Avoid overriding builtins.
Expand Down
14 changes: 12 additions & 2 deletions crates/ruff_python_semantic/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,18 @@ pub struct SemanticModel<'a> {
/// ```
///
/// In this case, the binding created by `x = 1` is annotated by the binding created by
/// `x: int`. We don't consider the latter binding to shadow the former, because it doesn't
/// change the value of the binding, but we do want to track the annotation.
/// `x: int`. We don't consider the latter binding to _shadow_ the former, because it doesn't
/// change the value of the binding, and so we don't store in on the scope. But we _do_ want to
/// track the annotation in some form, since it's a reference to `x`.
///
/// Note that, given:
/// ```python
/// x: int
/// ```
///
/// In this case, we _do_ store the binding created by `x: int` directly on the scope, and not
/// as a delayed annotation. Annotations are thus treated as bindings only when they are the
/// first binding in a scope; any annotations that follow are treated as "delayed" annotations.
delayed_annotations: HashMap<BindingId, Vec<BindingId>, BuildNoHashHasher<BindingId>>,

/// Body iteration; used to peek at siblings.
Expand Down
40 changes: 0 additions & 40 deletions crates/ruff_python_semantic/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,6 @@ use ruff_index::{newtype_index, Idx, IndexSlice, IndexVec};
use crate::binding::{BindingId, StarImportation};
use crate::globals::GlobalsId;

/// Cases to consider:
/// ```python
/// x: int
/// x = 1
///
/// print(x) # OK
/// ```
///
/// ```python
/// x = 1
/// x: int
///
/// print(x) # OK
/// ```
///
/// ```python
/// x: int
///
/// print(x) # NameError, but the annotation is used
/// ```
///
/// ```python
/// x: int # Unused annotation
/// ```
///
/// In each case, we want to track the annotations.
///
/// What if we:
/// - Remove `Annotation` from `Bindings`.
///
/// Then, we'd still need a way to get the reference for rewrites.
/// We'd also be unable to raise "Unused annotation" warnings.
///
/// What if we:
/// - Remove `Annotation` from `Bindings`.
/// - Add a new `Annotations` map to `Scope`.
///
/// I think the lazy thing to do is: keep our current model, but add a vector of annotations
/// for each binding. This is a bit wasteful, but it's also simple.

#[derive(Debug)]
pub struct Scope<'a> {
/// The kind of scope.
Expand Down

0 comments on commit 0dde195

Please sign in to comment.