Skip to content

Commit 573be3e

Browse files
elazargilevkivskyi
authored andcommitted
Allow calling Type[T] where T has generic bound (#5309)
Fix #3631 : looks like it was an explicit decision, made before we had self types. The code modified in this PR was introduced at #1569. There are no tests for the current behavior, and it seems like we actually want to allow it.
1 parent 737470e commit 573be3e

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

mypy/checkexpr.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -716,16 +716,10 @@ def analyze_type_type_callee(self, item: Type, context: Context) -> Type:
716716
callee = self.analyze_type_type_callee(item.upper_bound,
717717
context) # type: Optional[Type]
718718
if isinstance(callee, CallableType):
719-
if callee.is_generic():
720-
callee = None
721-
else:
722-
callee = callee.copy_modified(ret_type=item)
719+
callee = callee.copy_modified(ret_type=item)
723720
elif isinstance(callee, Overloaded):
724-
if callee.items()[0].is_generic():
725-
callee = None
726-
else:
727-
callee = Overloaded([c.copy_modified(ret_type=item)
728-
for c in callee.items()])
721+
callee = Overloaded([c.copy_modified(ret_type=item)
722+
for c in callee.items()])
729723
if callee:
730724
return callee
731725

test-data/unit/check-generics.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,16 @@ def f(c: Type[T]) -> T: ...
17781778
x: Any
17791779
reveal_type(f(x)) # E: Revealed type is 'Any'
17801780

1781+
[case testCallTypeTWithGenericBound]
1782+
from typing import Generic, TypeVar, Type
1783+
T = TypeVar('T')
1784+
S = TypeVar('S', bound='A')
1785+
1786+
class A(Generic[T]): pass
1787+
1788+
def f(cls: Type[S]) -> None:
1789+
cls()
1790+
17811791
[case testQualifiedTypeVariableName]
17821792
import b
17831793
def f(x: b.T) -> b.T: return x

0 commit comments

Comments
 (0)