1919using Microsoft . Python . Analysis . Analyzer . Evaluation ;
2020using Microsoft . Python . Analysis . Documents ;
2121using Microsoft . Python . Analysis . Modules ;
22+ using Microsoft . Python . Analysis . Specializations . Typing ;
2223using Microsoft . Python . Analysis . Types ;
2324using Microsoft . Python . Analysis . Types . Collections ;
2425using Microsoft . Python . Analysis . Values ;
@@ -239,6 +240,11 @@ private void MergeStub() {
239240
240241 var sourceVar = Eval . GlobalScope . Variables [ v . Name ] ;
241242 var sourceType = sourceVar ? . Value . GetPythonType ( ) ;
243+
244+ // If stub says 'Any' but we have better type, keep the current type.
245+ if ( ! IsStubBetterType ( sourceType , stubType ) ) {
246+ continue ; ;
247+ }
242248
243249 // If types are the classes, merge members.
244250 // Otherwise, replace type from one from the stub.
@@ -250,9 +256,15 @@ private void MergeStub() {
250256 var stubMember = stubType . GetMember ( name ) ;
251257 var member = cls . GetMember ( name ) ;
252258
259+ var memberType = member ? . GetPythonType ( ) ;
260+ var stubMemberType = stubMember . GetPythonType ( ) ;
261+ if ( ! IsStubBetterType ( memberType , stubMemberType ) ) {
262+ continue ; ;
263+ }
264+
253265 // Get documentation from the current type, if any, since stubs
254266 // typically do not contain documentation while scraped code does.
255- member ? . GetPythonType ( ) ? . TransferDocumentationAndLocation ( stubMember . GetPythonType ( ) ) ;
267+ memberType ? . TransferDocumentationAndLocation ( stubMemberType ) ;
256268 cls . AddMember ( name , stubMember , overwrite : true ) ;
257269 }
258270 } else {
@@ -268,5 +280,9 @@ private void MergeStub() {
268280 }
269281 }
270282 }
283+
284+ private static bool IsStubBetterType ( IPythonType sourceType , IPythonType stubType )
285+ // If stub says 'Any' but we have better type, keep the current type.
286+ => sourceType . IsUnknown ( ) || ! ( stubType . DeclaringModule is TypingModule ) || stubType . Name != "Any" ;
271287 }
272288}
0 commit comments