Changed the behavior when invoking constructor for type[T] where T is a TypeVar with no explicit upper bound (and therefore has an implicit upper bound of object). According to the newly-clarified typing spec, this should enforce the constructor signature of object. (#7697)

This commit is contained in:
Eric Traut 2024-04-13 21:05:39 -07:00 committed by GitHub
parent 7f46092aef
commit 9d87fe1079
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 14 additions and 13 deletions

View File

@ -637,7 +637,6 @@ function validateFallbackConstructorCall(
// It's OK if the argument list consists only of `*args` and `**kwargs`.
if (argList.length > 0 && argList.some((arg) => arg.argumentCategory === ArgumentCategory.Simple)) {
if (!type.includeSubclasses) {
evaluator.addDiagnostic(
DiagnosticRule.reportCallIssue,
LocMessage.constructorNoArgs().format({ type: type.aliasName || type.details.name }),
@ -645,7 +644,6 @@ function validateFallbackConstructorCall(
);
reportedErrors = true;
}
}
if (!inferenceContext && type.typeArguments) {
// If there was no expected type but the type was already specialized,

View File

@ -11,5 +11,7 @@ class Foo(Generic[T]):
val = self.method1()
reveal_type(val(), expected_text="T@Foo")
def method1(self) -> type[T]:
...
# This should generate an error.
val(1)
def method1(self) -> type[T]: ...

View File

@ -13,7 +13,7 @@ T_A = TypeVar("T_A", bound=ClassA)
def func1(cls: type[T_A]) -> T_A:
# This should generate an error
# This should generate an error.
y = cls()
x = cls(1, "")
@ -25,6 +25,7 @@ _T = TypeVar("_T")
def func2(cls: type[_T]) -> _T:
# This should generate an error.
y = cls(1, "")
x = cls()

View File

@ -1572,7 +1572,7 @@ test('Constructor12', () => {
test('Constructor13', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['constructor13.py']);
TestUtils.validateResults(analysisResults, 0);
TestUtils.validateResults(analysisResults, 1);
});
test('Constructor14', () => {
@ -1620,7 +1620,7 @@ test('Constructor20', () => {
test('Constructor21', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['constructor21.py']);
TestUtils.validateResults(analysisResults, 1);
TestUtils.validateResults(analysisResults, 2);
});
test('Constructor22', () => {