mirror of
https://github.com/microsoft/pyright.git
synced 2024-10-07 13:29:17 +03:00
Fixed a bug that resulted in a false positive error when accessing members from a type
instance or a Type[T]
.
This commit is contained in:
parent
3d204d6b9d
commit
2349041a68
@ -3919,6 +3919,24 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
|
|||||||
if (typeResult?.isIncomplete) {
|
if (typeResult?.isIncomplete) {
|
||||||
isIncomplete = true;
|
isIncomplete = true;
|
||||||
}
|
}
|
||||||
|
} else if (ClassType.isBuiltIn(baseType, 'type') && objectType && isClassInstance(objectType)) {
|
||||||
|
// Handle the case where the base type is an instance of 'type'. We'll
|
||||||
|
// treat it as an instantiable subclass of 'object'.
|
||||||
|
const typeResult = getTypeFromClassMember(
|
||||||
|
node.memberName,
|
||||||
|
ClassType.cloneAsInstantiable(objectType),
|
||||||
|
memberName,
|
||||||
|
usage,
|
||||||
|
diag,
|
||||||
|
MemberAccessFlags.None,
|
||||||
|
baseTypeResult.bindToType
|
||||||
|
? (convertToInstance(baseTypeResult.bindToType) as ClassType | TypeVarType)
|
||||||
|
: undefined
|
||||||
|
);
|
||||||
|
type = typeResult?.type;
|
||||||
|
if (typeResult?.isIncomplete) {
|
||||||
|
isIncomplete = true;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Handle the special case of 'name' and 'value' members within an enum.
|
// Handle the special case of 'name' and 'value' members within an enum.
|
||||||
if (ClassType.isEnumClass(baseType)) {
|
if (ClassType.isEnumClass(baseType)) {
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
# This sample tests the case where a member is accessed from a "type"
|
||||||
|
# instance or a Type[T].
|
||||||
|
|
||||||
|
# pyright: strict
|
||||||
|
|
||||||
|
from typing import Type, TypeVar
|
||||||
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
|
||||||
|
def func1(t: Type[T]) -> Type[T]:
|
||||||
|
def __repr__(self: T) -> str:
|
||||||
|
...
|
||||||
|
|
||||||
|
t.__repr__ = __repr__
|
||||||
|
return t
|
||||||
|
|
||||||
|
|
||||||
|
def func2(t: type) -> type:
|
||||||
|
def __repr__(self: object) -> str:
|
||||||
|
...
|
||||||
|
|
||||||
|
t.__repr__ = __repr__
|
||||||
|
return t
|
@ -405,6 +405,11 @@ test('MemberAccess15', () => {
|
|||||||
TestUtils.validateResults(analysisResults, 0);
|
TestUtils.validateResults(analysisResults, 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('MemberAccess16', () => {
|
||||||
|
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['memberAccess16.py']);
|
||||||
|
TestUtils.validateResults(analysisResults, 0);
|
||||||
|
});
|
||||||
|
|
||||||
test('DataClass1', () => {
|
test('DataClass1', () => {
|
||||||
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['dataclass1.py']);
|
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['dataclass1.py']);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user