@@ -141,60 +141,53 @@ private void DeclareParameters(bool declareVariables) {
141141 Eval . DeclareVariable ( p0 . Name , new PythonInstance ( _self ) , VariableSource . Declaration , p0 . NameExpression ) ;
142142 }
143143 // Set parameter info.
144- var pi = new ParameterInfo ( Ast , p0 , _self , false ) ;
145- pi . SetType ( _self ) ;
144+ var pi = new ParameterInfo ( Ast , p0 , _self , null , false ) ;
146145 parameters . Add ( pi ) ;
147146 skip ++ ;
148147 }
149148 }
150149
151150 // Declare parameters in scope
151+ IMember defaultValue = null ;
152+ var meaningfuleDefautValue = false ;
152153 for ( var i = skip ; i < FunctionDefinition . Parameters . Length ; i ++ ) {
153154 var isGeneric = false ;
154155 var p = FunctionDefinition . Parameters [ i ] ;
155156 if ( ! string . IsNullOrEmpty ( p . Name ) ) {
156- // If parameter has default value, look for the annotation locally first
157- // since outer type may be getting redefined. Consider 's = None; def f(s: s = 123): ...
158157 IPythonType paramType = null ;
159158 if ( p . DefaultValue != null ) {
159+ defaultValue = Eval . GetValueFromExpression ( p . DefaultValue ) ;
160+ // If parameter has default value, look for the annotation locally first
161+ // since outer type may be getting redefined. Consider 's = None; def f(s: s = 123): ...
160162 paramType = Eval . GetTypeFromAnnotation ( p . Annotation , out isGeneric , LookupOptions . Local | LookupOptions . Builtins ) ;
161- if ( paramType == null ) {
162- var defaultValue = Eval . GetValueFromExpression ( p . DefaultValue ) ;
163- if ( ! defaultValue . IsUnknown ( ) ) {
164- paramType = defaultValue . GetPythonType ( ) ;
165- }
163+ // Default value of None does not mean the parameter is None, just says it can be missing.
164+ defaultValue = defaultValue . IsUnknown ( ) || defaultValue . IsOfType ( BuiltinTypeId . NoneType ) ? null : defaultValue ;
165+ if ( paramType == null && defaultValue != null ) {
166+ paramType = defaultValue . GetPythonType ( ) ;
166167 }
167168 }
168169 // If all else fails, look up globally.
169- paramType = paramType ?? Eval . GetTypeFromAnnotation ( p . Annotation , out isGeneric ) ;
170-
171- var pi = new ParameterInfo ( Ast , p , paramType , isGeneric ) ;
172- DeclareParameter ( p , i , pi , declareVariables ) ;
170+ paramType = paramType ?? Eval . GetTypeFromAnnotation ( p . Annotation , out isGeneric ) ?? Eval . UnknownType ;
171+ var pi = new ParameterInfo ( Ast , p , paramType , defaultValue , isGeneric ) ;
172+ if ( declareVariables ) {
173+ DeclareParameter ( p , pi ) ;
174+ }
173175 parameters . Add ( pi ) ;
174176 }
175177 }
176178 _overload . SetParameters ( parameters ) ;
177179 }
178180
179- private void DeclareParameter ( Parameter p , int index , ParameterInfo pi , bool declareVariables ) {
181+ private void DeclareParameter ( Parameter p , ParameterInfo pi ) {
180182 IPythonType paramType ;
181-
182183 // If type is known from annotation, use it.
183184 if ( pi != null && ! pi . Type . IsUnknown ( ) && ! pi . Type . IsGenericParameter ( ) ) {
184185 // TODO: technically generics may have constraints. Should we consider them?
185186 paramType = pi . Type ;
186187 } else {
187- var defaultValue = Eval . GetValueFromExpression ( p . DefaultValue ) ?? Eval . UnknownType ;
188-
189- paramType = defaultValue ? . GetPythonType ( ) ;
190- if ( ! paramType . IsUnknown ( ) ) {
191- pi ? . SetDefaultValueType ( paramType ) ;
192- }
193- }
194-
195- if ( declareVariables ) {
196- Eval . DeclareVariable ( p . Name , new PythonInstance ( paramType ) , VariableSource . Declaration , p . NameExpression ) ;
188+ paramType = pi ? . DefaultValue ? . GetPythonType ( ) ?? Eval . UnknownType ;
197189 }
190+ Eval . DeclareVariable ( p . Name , new PythonInstance ( paramType ) , VariableSource . Declaration , p . NameExpression ) ;
198191 }
199192 }
200193}
0 commit comments