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:
Eric Traut 2021-11-07 00:54:41 -07:00
parent 3d204d6b9d
commit 2349041a68
3 changed files with 47 additions and 0 deletions

View File

@ -3919,6 +3919,24 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (typeResult?.isIncomplete) {
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 {
// Handle the special case of 'name' and 'value' members within an enum.
if (ClassType.isEnumClass(baseType)) {

View File

@ -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

View File

@ -405,6 +405,11 @@ test('MemberAccess15', () => {
TestUtils.validateResults(analysisResults, 0);
});
test('MemberAccess16', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['memberAccess16.py']);
TestUtils.validateResults(analysisResults, 0);
});
test('DataClass1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['dataclass1.py']);