Skip to content
Closed
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
19 changes: 15 additions & 4 deletions pyrefly/lib/alt/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,17 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
}
}

/// Extract the ClassType to use for attribute lookup on a quantified (TypeVar)
/// type from the attribute base of its bound.
fn quantified_bound_class(&self, base: AttributeBase1) -> Option<ClassType> {
match base {
AttributeBase1::ClassInstance(cls) => Some(cls),
// Handle `type[Any]`, which happens for TypeVars w/ `bound=type`
AttributeBase1::TypeAny(_) => Some(self.stdlib.builtins_type().clone()),
_ => None,
}
}

fn as_attribute_base(&self, ty: Type) -> Option<AttributeBase> {
let mut acc = Vec::new();
self.as_attribute_base1(ty, &mut acc);
Expand Down Expand Up @@ -1979,7 +1990,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
let mut use_fallback = false;
if let Some(base) = self.as_attribute_base(ty.clone()) {
for base1 in base.0 {
if let AttributeBase1::ClassInstance(cls) = base1 {
if let Some(cls) = self.quantified_bound_class(base1) {
acc.push(AttributeBase1::ClassObject(ClassBase::Quantified(
(*quantified).clone(),
cls,
Expand All @@ -2001,7 +2012,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
for ty in constraints {
if let Some(base) = self.as_attribute_base(ty.clone()) {
for base1 in base.0 {
if let AttributeBase1::ClassInstance(cls) = base1 {
if let Some(cls) = self.quantified_bound_class(base1) {
acc.push(AttributeBase1::ClassObject(ClassBase::Quantified(
(*quantified).clone(),
cls,
Expand Down Expand Up @@ -2185,7 +2196,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
let mut use_fallback = false;
if let Some(base) = self.as_attribute_base(ty.clone()) {
for base1 in base.0 {
if let AttributeBase1::ClassInstance(cls) = base1 {
if let Some(cls) = self.quantified_bound_class(base1) {
acc.push(AttributeBase1::Quantified((*quantified).clone(), cls));
} else {
use_fallback = true;
Expand All @@ -2204,7 +2215,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
for ty in constraints {
if let Some(base) = self.as_attribute_base(ty.clone()) {
for base1 in base.0 {
if let AttributeBase1::ClassInstance(cls) = base1 {
if let Some(cls) = self.quantified_bound_class(base1) {
acc.push(AttributeBase1::Quantified(
(*quantified).clone(),
cls,
Expand Down
11 changes: 11 additions & 0 deletions pyrefly/lib/test/generic_restrictions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@ def func(c: T) -> C:
"#,
);

testcase!(
test_bounded_typevar_type_attribute_access,
r#"
from typing import TypeVar, assert_type
T = TypeVar('T', bound=type)
def get_class_name(cls: T) -> str:
assert_type(cls.__name__, str)
return cls.__name__
"#,
);

testcase!(
test_instantiate_default_typevar,
r#"
Expand Down