1515
1616using System . Collections . Generic ;
1717using System . Linq ;
18+ using Microsoft . Python . Analysis . Diagnostics ;
1819using Microsoft . Python . Analysis . Modules ;
1920using Microsoft . Python . Analysis . Specializations . Typing . Types ;
2021using Microsoft . Python . Analysis . Types ;
@@ -61,7 +62,7 @@ private void SpecializeMembers() {
6162 o = new PythonFunctionOverload ( fn . Name , location ) ;
6263 // When called, create generic parameter type. For documentation
6364 // use original TypeVar declaration so it appear as a tooltip.
64- o . SetReturnValueProvider ( ( interpreter , overload , args ) => CreateTypeAlias ( args . Values < IMember > ( ) ) ) ;
65+ o . SetReturnValueProvider ( ( interpreter , overload , args ) => CreateTypeAlias ( args ) ) ;
6566 fn . AddOverload ( o ) ;
6667 _members [ "NewType" ] = fn ;
6768
@@ -81,41 +82,41 @@ private void SpecializeMembers() {
8182
8283 _members [ "Iterable" ] = new GenericType ( "Iterable" , typeArgs => CreateListType ( "Iterable" , BuiltinTypeId . List , typeArgs , false ) , this ) ;
8384 _members [ "Sequence" ] = new GenericType ( "Sequence" , typeArgs => CreateListType ( "Sequence" , BuiltinTypeId . List , typeArgs , false ) , this ) ;
84- _members [ "MutableSequence" ] = new GenericType ( "MutableSequence" ,
85+ _members [ "MutableSequence" ] = new GenericType ( "MutableSequence" ,
8586 typeArgs => CreateListType ( "MutableSequence" , BuiltinTypeId . List , typeArgs , true ) , this ) ;
86- _members [ "List" ] = new GenericType ( "List" ,
87+ _members [ "List" ] = new GenericType ( "List" ,
8788 typeArgs => CreateListType ( "List" , BuiltinTypeId . List , typeArgs , true ) , this ) ;
8889
89- _members [ "MappingView" ] = new GenericType ( "MappingView" ,
90+ _members [ "MappingView" ] = new GenericType ( "MappingView" ,
9091 typeArgs => CreateDictionary ( "MappingView" , typeArgs , false ) , this ) ;
9192
9293 _members [ "KeysView" ] = new GenericType ( "KeysView" , CreateKeysViewType , this ) ;
9394 _members [ "ValuesView" ] = new GenericType ( "ValuesView" , CreateValuesViewType , this ) ;
9495 _members [ "ItemsView" ] = new GenericType ( "ItemsView" , CreateItemsViewType , this ) ;
9596
96- _members [ "Set" ] = new GenericType ( "Set" ,
97+ _members [ "Set" ] = new GenericType ( "Set" ,
9798 typeArgs => CreateListType ( "Set" , BuiltinTypeId . Set , typeArgs , true ) , this ) ;
98- _members [ "MutableSet" ] = new GenericType ( "MutableSet" ,
99+ _members [ "MutableSet" ] = new GenericType ( "MutableSet" ,
99100 typeArgs => CreateListType ( "MutableSet" , BuiltinTypeId . Set , typeArgs , true ) , this ) ;
100- _members [ "FrozenSet" ] = new GenericType ( "FrozenSet" ,
101+ _members [ "FrozenSet" ] = new GenericType ( "FrozenSet" ,
101102 typeArgs => CreateListType ( "FrozenSet" , BuiltinTypeId . Set , typeArgs , false ) , this ) ;
102103
103104 _members [ "Tuple" ] = new GenericType ( "Tuple" , CreateTupleType , this ) ;
104105
105- _members [ "Mapping" ] = new GenericType ( "Mapping" ,
106+ _members [ "Mapping" ] = new GenericType ( "Mapping" ,
106107 typeArgs => CreateDictionary ( "Mapping" , typeArgs , false ) , this ) ;
107- _members [ "MutableMapping" ] = new GenericType ( "MutableMapping" ,
108+ _members [ "MutableMapping" ] = new GenericType ( "MutableMapping" ,
108109 typeArgs => CreateDictionary ( "MutableMapping" , typeArgs , true ) , this ) ;
109- _members [ "Dict" ] = new GenericType ( "Dict" ,
110+ _members [ "Dict" ] = new GenericType ( "Dict" ,
110111 typeArgs => CreateDictionary ( "Dict" , typeArgs , true ) , this ) ;
111- _members [ "OrderedDict" ] = new GenericType ( "OrderedDict" ,
112+ _members [ "OrderedDict" ] = new GenericType ( "OrderedDict" ,
112113 typeArgs => CreateDictionary ( "OrderedDict" , typeArgs , true ) , this ) ;
113- _members [ "DefaultDict" ] = new GenericType ( "DefaultDict" ,
114+ _members [ "DefaultDict" ] = new GenericType ( "DefaultDict" ,
114115 typeArgs => CreateDictionary ( "DefaultDict" , typeArgs , true ) , this ) ;
115116
116117 _members [ "Union" ] = new GenericType ( "Union" , CreateUnion , this ) ;
117118
118- _members [ "Counter" ] = Specialized . Function ( "Counter" , this , GetMemberDocumentation ( "Counter" ) ,
119+ _members [ "Counter" ] = Specialized . Function ( "Counter" , this , GetMemberDocumentation ( "Counter" ) ,
119120 new PythonInstance ( Interpreter . GetBuiltinType ( BuiltinTypeId . Int ) ) ) ;
120121
121122 _members [ "SupportsInt" ] = Interpreter . GetBuiltinType ( BuiltinTypeId . Int ) ;
@@ -217,13 +218,25 @@ private IPythonType CreateItemsViewType(IReadOnlyList<IPythonType> typeArgs) {
217218 return Interpreter . UnknownType ;
218219 }
219220
220- private IPythonType CreateTypeAlias ( IReadOnlyList < IMember > typeArgs ) {
221+ private IPythonType CreateTypeAlias ( IArgumentSet args ) {
222+ var typeArgs = args . Values < IMember > ( ) ;
221223 if ( typeArgs . Count == 2 ) {
222224 var typeName = ( typeArgs [ 0 ] as IPythonConstant ) ? . Value as string ;
223225 if ( ! string . IsNullOrEmpty ( typeName ) ) {
224226 return new TypeAlias ( typeName , typeArgs [ 1 ] . GetPythonType ( ) ?? Interpreter . UnknownType ) ;
225227 }
226- // TODO: report incorrect first argument to NewVar
228+
229+ var firstArgType = ( typeArgs [ 0 ] as PythonInstance ) ? . Type . Name ;
230+ var eval = args . Eval ;
231+ var expression = args . Expression ;
232+
233+ eval . ReportDiagnostics (
234+ eval . Module ? . Uri ,
235+ new DiagnosticsEntry ( Resources . NewTypeFirstArgNotString . FormatInvariant ( firstArgType ) ,
236+ expression . GetLocation ( eval . Module ) . Span ,
237+ Diagnostics . ErrorCodes . NewTypeArguments ,
238+ Severity . Error , DiagnosticSource . Analysis )
239+ ) ;
227240 }
228241 // TODO: report wrong number of arguments
229242 return Interpreter . UnknownType ;
@@ -331,7 +344,7 @@ private IPythonType CreateGenericClassParameter(IReadOnlyList<IPythonType> typeA
331344 return Interpreter . UnknownType ;
332345 }
333346
334- private IPythonType ToGenericTemplate ( string typeName , IGenericTypeDefinition [ ] typeArgs , BuiltinTypeId typeId )
347+ private IPythonType ToGenericTemplate ( string typeName , IGenericTypeDefinition [ ] typeArgs , BuiltinTypeId typeId )
335348 => _members [ typeName ] is GenericType gt
336349 ? new GenericType ( CodeFormatter . FormatSequence ( typeName , '[' , typeArgs ) , gt . SpecificTypeConstructor , this , typeId , typeArgs )
337350 : Interpreter . UnknownType ;
0 commit comments