Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Evaluate default parameter value in enclosing scope #12852

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,7 @@ def bar(x: T) -> T: # OK
return x

bar(t)


def cannot_access_in_default[T](t: T = T): # F821
pass
11 changes: 8 additions & 3 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,14 @@ impl<'a> Visitor<'a> for Checker<'a> {
self.semantic(),
);

// The default values of the parameters needs to be evaluated in the enclosing
// scope.
for parameter in &**parameters {
if let Some(expr) = parameter.default() {
self.visit_expr(expr);
}
}

self.semantic.push_scope(ScopeKind::Type);

if let Some(type_params) = type_params {
Expand All @@ -715,9 +723,6 @@ impl<'a> Visitor<'a> for Checker<'a> {
}
}
}
if let Some(expr) = parameter.default() {
self.visit_expr(expr);
}
}
if let Some(expr) = returns {
match annotation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,10 @@ F821_17.py:103:17: F821 Undefined name `t`
| ^ F821
104 | return x
|

F821_17.py:116:40: F821 Undefined name `T`
|
116 | def cannot_access_in_default[T](t: T = T): # F821
| ^ F821
117 | pass
|
Loading