mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-07 21:25:08 +00:00
[ty] Type inference for hasattr(…)
This commit is contained in:
parent
48c425c15b
commit
ae6dd04910
1 changed files with 20 additions and 0 deletions
|
@ -655,6 +655,26 @@ impl<'db> Bindings<'db> {
|
|||
}
|
||||
}
|
||||
|
||||
Some(KnownFunction::HasAttr) => {
|
||||
if let [Some(obj), Some(Type::StringLiteral(attr_name))] =
|
||||
overload.parameter_types()
|
||||
{
|
||||
match obj.member(db, attr_name.value(db)).symbol {
|
||||
Symbol::Type(_, Boundness::Bound) => {
|
||||
overload.set_return_type(Type::BooleanLiteral(true));
|
||||
}
|
||||
Symbol::Type(_, Boundness::PossiblyUnbound) => {
|
||||
// Fall back to bool (from typeshed)
|
||||
}
|
||||
Symbol::Unbound => {
|
||||
// Returning `Literal[False]` here seems potentially
|
||||
// dangerous. The attribute could have been added
|
||||
// dynamically, so fall back to `bool` here to be safe.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(KnownFunction::IsProtocol) => {
|
||||
if let [Some(ty)] = overload.parameter_types() {
|
||||
overload.set_return_type(Type::BooleanLiteral(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue