Repository navigation
Fix constrained TypeVar narrowing for issubclass - #11951
Guishi Wang (guishiw) wants to merge 2 commits into
Conversation
🔴 Pyright CLI QA —
|
|
Thanks! The simple 1. Subclass constraints. from typing import TypeVar
class Base: ...
class Derived(Base):
def only_derived(self) -> int: return 42
T = TypeVar("T", Base, Derived)
def make(cls: type[T]) -> T:
if issubclass(cls, Base):
return Base() # accepted now, but T may be Derived
return cls()
make(Derived).only_derived() # AttributeError at runtimeA similar issue happens when the second argument has a declared type like 2. Shadowed T = TypeVar("T", str, int)
def issubclass(cls: type, other: type) -> bool:
return True
def make(cls: type[T]) -> T:
if issubclass(cls, str):
return "wrong" # accepted now
return cls()Could the narrowing (a) keep a constraint on the positive branch whenever it's a subclass of the filter and drop it on the negative branch only when it's provably a subclass, and (b) confirm the callee resolves to the builtin, as the existing Non-blocking: the tuple form 🤖 AI-assisted review posted on behalf of Bill Schnurr (@bschnurr). |
|
Local review: Alongside the two cases in my earlier comment, I reproduced the QA case where the filter is a |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
The new issubclass narrowing is unsafe for a bare T constrained to metaclasses:
from typing import TypeVar, assert_type
class MetaA(type):
def marker(cls) -> str: return 'MetaA'
class MetaB(type): ...
class ClassA(metaclass=MetaA): ...
class ClassB(metaclass=MetaB): ...
T = TypeVar('T', MetaA, MetaB)
def choose(cls: T) -> T:
if issubclass(cls, MetaA):
return ClassA
return ClassB
result = choose(ClassA)
assert_type(result, MetaA)
result.marker()The base revision reports reportReturnType at both returns. 3fe6716 reports no errors, including at the argument. On CPython 3.14.3, result.marker() raises AttributeError: ClassA is an instance of MetaA, not a subclass of it, so choose returns ClassB. The isinstance(cls, MetaA) control works on both revisions.
Could we restrict this particular constrained-return narrowing to appropriate type[T] cases rather than a bare T? I haven't tested that change.
Even with type[T], numeric promotion and common subclasses need consideration beyond Bill's feedback:
- With
Tconstrained to(float, str),p(t: type[T]) -> Treturns1.5in theissubclass(t, float)branch and's'otherwise.p(int)is inferred asfloatbut returns's'. - With
Tconstrained to(A, B)andAB(A, B),preturnsB()in theissubclass(t, B)branch andA()otherwise.p(AB)is inferred asAbut returnsB().
Base reports reportReturnType for both returns in both examples, while head accepts them. The analogous isinstance weakness for these constraint shapes predates this PR, but accepting the issubclass versions is new. The current PR test only has correct returns. Could we add negative controls?
fix: #11946
Root cause
Control-flow narrowing for constrained TypeVars recognized only
isinstanceconditions. Anissubclasscondition receivestype[T], so the return-type checker could not associate the narrowed class with the constrained instance TypeVarT.Fix strategy
Recognize
issubclassin constrained-TypeVar flow analysis and remove one instantiable layer fromtype[T]before matching it toT. Add positive and negative branch coverage to the existing constrained TypeVar sample.The relevant test suite passes with 164 tests.