Fixed regression in typed dict code found by unit tests.

This commit is contained in:
Eric Traut 2019-11-10 14:10:38 -08:00
parent 21a125d983
commit 406f167e17
2 changed files with 15 additions and 11 deletions

View File

@ -3475,7 +3475,8 @@ export function createExpressionEvaluator(diagnosticSink: TextRangeDiagnosticSin
ClassType.addBaseClass(classType, enumClass);
const classFields = ClassType.getFields(classType);
classFields.set('__class__', Symbol.createWithType(SymbolFlags.ClassMember, classType));
classFields.set('__class__', Symbol.createWithType(
SymbolFlags.ClassMember | SymbolFlags.IgnoredForProtocolMatch, classType));
if (argList.length < 2) {
addError('Expected enum item string as second parameter', errorNode);
@ -3588,7 +3589,8 @@ export function createExpressionEvaluator(diagnosticSink: TextRangeDiagnosticSin
}
const classFields = ClassType.getFields(classType);
classFields.set('__class__', Symbol.createWithType(SymbolFlags.ClassMember, classType));
classFields.set('__class__', Symbol.createWithType(
SymbolFlags.ClassMember | SymbolFlags.IgnoredForProtocolMatch, classType));
if (argList.length < 2) {
addError('Expected dict as second parameter', errorNode);
@ -3629,6 +3631,9 @@ export function createExpressionEvaluator(diagnosticSink: TextRangeDiagnosticSin
// Record names in a map to detect duplicates.
entryMap.set(entryName, true);
// Cache the annotation type.
getTypeOfAnnotation(entry.valueExpression);
const newSymbol = new Symbol(SymbolFlags.InstanceMember);
const declaration: VariableDeclaration = {
type: DeclarationType.Variable,
@ -3675,7 +3680,8 @@ export function createExpressionEvaluator(diagnosticSink: TextRangeDiagnosticSin
ClassType.addBaseClass(classType, builtInNamedTuple);
const classFields = ClassType.getFields(classType);
classFields.set('__class__', Symbol.createWithType(SymbolFlags.ClassMember, classType));
classFields.set('__class__', Symbol.createWithType(
SymbolFlags.ClassMember | SymbolFlags.IgnoredForProtocolMatch, classType));
const builtInTupleType = getBuiltInType(errorNode, 'Tuple');
if (builtInTupleType.category === TypeCategory.Class) {

View File

@ -13,11 +13,10 @@ import { DiagnosticAddendum } from '../common/diagnostic';
import StringMap from '../common/stringMap';
import { ParameterCategory } from '../parser/parseNodes';
import { ImportLookup } from './analyzerFileInfo';
import * as AnalyzerNodeInfo from './analyzerNodeInfo';
import { DeclarationType } from './declaration';
import { getTypeForDeclaration, hasTypeForDeclaration } from './declarationUtils';
import { Symbol, SymbolFlags, SymbolTable } from './symbol';
import { getDeclaredTypeOfSymbol, getEffectiveTypeOfSymbol,
getLastTypedDeclaredForSymbol,
isTypedDictMemberAccessedThroughIndex } from './symbolUtils';
import { AnyType, ClassType, combineTypes, FunctionParameter, FunctionType, FunctionTypeFlags,
InheritanceChain, isAnyOrUnknown, isNoneOrNever, isSameWithoutLiteralValue, isTypeSame,
@ -1550,14 +1549,13 @@ export function getTypedDictMembersForClassRecursive(classType: ClassType,
// Add any new typed dict entries from this class.
ClassType.getFields(classType).forEach((symbol, name) => {
const declarations = symbol.getDeclarations();
if (declarations.length > 0) {
const firstDecl = declarations[0];
if (firstDecl.type === DeclarationType.Variable &&
firstDecl.node && hasTypeForDeclaration(firstDecl)) {
if (!symbol.isIgnoredForProtocolMatch()) {
// Only variables (not functions, classes, etc.) are considered.
const lastDecl = getLastTypedDeclaredForSymbol(symbol);
if (lastDecl && lastDecl.type === DeclarationType.Variable) {
keyMap.set(name, {
valueType: getTypeForDeclaration(firstDecl) || UnknownType.create(),
valueType: getDeclaredTypeOfSymbol(symbol) || UnknownType.create(),
isRequired: !ClassType.isCanOmitDictValues(classType),
isProvided: false
});