mirror of
https://github.com/microsoft/pyright.git
synced 2024-09-11 07:55:56 +03:00
Did a small cleanup pass on the implementation of PEP 746.
This commit is contained in:
parent
ac7f6b7f74
commit
2ade792ba8
@ -15479,10 +15479,8 @@ export function createTypeEvaluator(
|
||||
|
||||
// Enforces metadata consistency as specified in PEP 746.
|
||||
function validateAnnotatedMetadata(errorNode: ExpressionNode, annotatedType: Type, metaArgs: TypeResultWithNode[]) {
|
||||
const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
|
||||
|
||||
// This is an experimental feature because PEP 746 hasn't been accepted.
|
||||
if (!fileInfo.diagnosticRuleSet.enableExperimentalFeatures) {
|
||||
if (!AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.enableExperimentalFeatures) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -15499,17 +15497,15 @@ export function createTypeEvaluator(
|
||||
}
|
||||
|
||||
// "Call" the __supports_type__ method to determine if the type is supported.
|
||||
const argList: FunctionArgument[] = [
|
||||
{
|
||||
argumentCategory: ArgumentCategory.Simple,
|
||||
typeResult: { type: convertToInstance(annotatedType) },
|
||||
},
|
||||
];
|
||||
|
||||
const callResult = useSpeculativeMode(errorNode, () =>
|
||||
validateCallArguments(
|
||||
errorNode,
|
||||
argList,
|
||||
[
|
||||
{
|
||||
argumentCategory: ArgumentCategory.Simple,
|
||||
typeResult: { type: convertToInstance(annotatedType) },
|
||||
},
|
||||
],
|
||||
{ type: supportsTypeMethod },
|
||||
/* typeVarContext */ undefined,
|
||||
/* skipUnknownArgCheck */ true,
|
||||
@ -15518,18 +15514,24 @@ export function createTypeEvaluator(
|
||||
)
|
||||
);
|
||||
|
||||
if (!callResult.isTypeIncomplete && callResult.returnType) {
|
||||
if (callResult.argumentErrors || !canBeTruthy(callResult.returnType)) {
|
||||
addDiagnostic(
|
||||
DiagnosticRule.reportInvalidTypeArguments,
|
||||
LocMessage.annotatedMetadataInconsistent().format({
|
||||
metadataType: printType(metaArg.type),
|
||||
type: printType(convertToInstance(annotatedType)),
|
||||
}),
|
||||
metaArg.node
|
||||
);
|
||||
}
|
||||
if (callResult.isTypeIncomplete || !callResult.returnType) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If there are no errors and the return type is potentially truthy,
|
||||
// we know that the type is supported by this metadata object.
|
||||
if (!callResult.argumentErrors && canBeTruthy(callResult.returnType)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
addDiagnostic(
|
||||
DiagnosticRule.reportInvalidTypeArguments,
|
||||
LocMessage.annotatedMetadataInconsistent().format({
|
||||
metadataType: printType(metaArg.type),
|
||||
type: printType(convertToInstance(annotatedType)),
|
||||
}),
|
||||
metaArg.node
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -47,11 +47,11 @@ class MetaA:
|
||||
return isinstance(ParentA, str)
|
||||
|
||||
|
||||
a1: Annotated[ParentA, MetaA()] = ParentA()
|
||||
a1: Annotated[ParentA, 1, "", MetaA()] = ParentA()
|
||||
a2: Annotated[ChildA, MetaA(), 1, ""] = ChildA()
|
||||
|
||||
# This should generate an error.
|
||||
a3: Annotated[int, MetaA()] = 1
|
||||
a3: Annotated[int, 1, "", MetaA(), ""] = 1
|
||||
|
||||
|
||||
class MetaInt:
|
||||
@ -64,7 +64,7 @@ i1: Annotated[int, MetaInt()] = 1
|
||||
i2: Annotated[float, MetaInt()] = 1.0
|
||||
|
||||
|
||||
class MetaWithOverride:
|
||||
class MetaWithOverload:
|
||||
@overload
|
||||
def __supports_type__(self, obj: int, /) -> bool: ...
|
||||
@overload
|
||||
@ -74,11 +74,11 @@ class MetaWithOverride:
|
||||
def __supports_type__(self, obj: Any, /) -> bool: ...
|
||||
|
||||
|
||||
v1: Annotated[int, MetaWithOverride()] = 1
|
||||
v2: Annotated[None, MetaWithOverride()] = None
|
||||
v1: Annotated[int, MetaWithOverload()] = 1
|
||||
v2: Annotated[None, MetaWithOverload()] = None
|
||||
|
||||
# This should generate an error.
|
||||
v3: Annotated[str, MetaWithOverride()] = ""
|
||||
v3: Annotated[str, MetaWithOverload()] = ""
|
||||
|
||||
# This should generate an error.
|
||||
v4: Annotated[complex, MetaWithOverride()] = 3j
|
||||
v4: Annotated[complex, MetaWithOverload()] = 3j
|
||||
|
Loading…
Reference in New Issue
Block a user