11import { TextDocument } from "vscode" ;
2- import { PythonType as PythonType , DataTypeContainer , getDataTypeContainer } from "./python" ;
2+ import { PythonType as PythonType , DataTypeContainer } from "./python" ;
33import { TypeSearch , VariableSearchResult } from "./typeSearch" ;
44import { TypingHintProvider } from "./typingHintProvider" ;
55import { TypeHintSettings } from "./settings" ;
@@ -12,27 +12,20 @@ export class TypeHintProvider {
1212 private _typingHintProvider : TypingHintProvider ;
1313
1414 private likelyTypes : PythonType [ ] = [
15- PythonType . String ,
1615 PythonType . List ,
17- PythonType . Dict ,
16+ PythonType . Dict ,
17+ PythonType . String ,
1818 PythonType . Bool ,
1919 PythonType . Int ,
2020 PythonType . Tuple ,
2121 PythonType . Float
2222 ] ;
2323 private _providedTypeHints : string [ ] = [ ] ;
24- private _typeContainer : DataTypeContainer ;
25- private _typingImported = false ;
2624
2725 constructor ( typeContainer : DataTypeContainer ) {
28- this . _typeContainer = typeContainer ;
2926 this . _typingHintProvider = new TypingHintProvider ( typeContainer ) ;
3027 }
3128
32- public get typingImported ( ) {
33- return this . _typingImported ;
34- }
35-
3629 /**
3730 * Estimates a parameter's type and returns type hints for it.
3831 * The returned hints are ordered with the most likely type being first.
@@ -43,14 +36,13 @@ export class TypeHintProvider {
4336 public async estimateTypeHints ( param : string , documentText : string ) : Promise < string [ ] > {
4437 const typeHints : string [ ] = [ ] ;
4538
46- const typingSearch = this . _typingHintProvider . detectTypingImport ( documentText ) ;
4739 const variableSearch = TypeSearch . variableWithSameName ( param , documentText ) ;
4840
41+ const isTypingImported = this . _typingHintProvider . detectTypingImport ( documentText ) ;
4942 let typeName = this . getTypeParamEndsWith ( param , "_" ) ;
50- this . _typingImported = await typingSearch ;
5143 if ( typeName ) {
5244 this . add ( typeName , typeHints ) ;
53- this . tryAddTypingHint ( typeName , typeHints ) ;
45+ this . tryAddTypingHint ( typeName , typeHints , isTypingImported ) ;
5446 }
5547
5648 const typesFound = typeHints . length > 0
@@ -64,15 +56,15 @@ export class TypeHintProvider {
6456
6557 if ( searchResult !== null && ! TypeSearch . invalidTernaryOperator ( searchResult ) ) {
6658 this . add ( searchResult . typeName , typeHints ) ;
67- this . tryAddTypingHints ( searchResult , typeHints ) ;
68- this . tryAdd ( this . typeGuessFor ( param , typeHints ) , typeHints ) ;
59+ this . tryAddTypingHints ( searchResult , typeHints , isTypingImported ) ;
60+ this . tryAdd ( this . typeGuessFor ( param ) , typeHints ) ;
6961 } else {
7062 typeName = this . getTypeParamEndsWith ( param , "" ) ;
7163 if ( typeName ) {
7264 this . add ( typeName , typeHints ) ;
73- this . tryAddTypingHint ( typeName , typeHints ) ;
65+ this . tryAddTypingHint ( typeName , typeHints , isTypingImported ) ;
7466 } else {
75- this . tryAdd ( this . typeGuessFor ( param , typeHints ) , typeHints ) ;
67+ this . tryAdd ( this . typeGuessFor ( param ) , typeHints ) ;
7668 }
7769 }
7870 return typeHints ;
@@ -82,15 +74,7 @@ export class TypeHintProvider {
8274 * Returns hints for types that have not been provided yet.
8375 */
8476 public remainingTypeHints ( ) : PythonType [ ] {
85- const result : PythonType [ ] = [ ] ;
86-
87- for ( const type of Object . values ( this . _typeContainer ) ) {
88- if ( this . hintNotProvided ( type . name ) ) {
89- result . push ( type . name ) ;
90- }
91- }
92-
93- return result ;
77+ return Object . values ( PythonType ) . filter ( type => this . hintNotProvided ( type ) ) ;
9478 }
9579
9680 /**
@@ -107,7 +91,7 @@ export class TypeHintProvider {
10791 /**
10892 * If the param ends with a type name, the type is returned.
10993 * @param param The parameter name.
110- * @param prefix A prefix before the typename. For example, a param named x {prefix}list will return 'list'.
94+ * @param prefix A prefix before the typename. For example, a param named {prefix}list will return 'list'.
11195 */
11296 private getTypeParamEndsWith ( param : string , prefix : string ) : PythonType | null {
11397 const paramUpper = param . toUpperCase ( ) ;
@@ -120,7 +104,7 @@ export class TypeHintProvider {
120104 return null ;
121105 }
122106
123- private typeGuessFor ( param : string , typeHints : string [ ] ) : string | null {
107+ private typeGuessFor ( param : string ) : string | null {
124108 const typeGuesses : { [ key : string ] : string } = {
125109 "string" : PythonType . String ,
126110 "text" : PythonType . String ,
@@ -133,7 +117,6 @@ export class TypeHintProvider {
133117 "num" : PythonType . Int
134118 } ;
135119 if ( param in typeGuesses ) {
136- this . add ( typeGuesses [ param ] , typeHints ) ;
137120 return typeGuesses [ param ] ;
138121 }
139122 return null ;
@@ -156,8 +139,8 @@ export class TypeHintProvider {
156139 return false ;
157140 }
158141
159- private tryAddTypingHints ( searchResult : VariableSearchResult | null , typeHints : string [ ] ) {
160- if ( this . _typingImported ) {
142+ private tryAddTypingHints ( searchResult : VariableSearchResult | null , typeHints : string [ ] , typingImported : boolean ) {
143+ if ( typingImported ) {
161144 const hints : string [ ] | null = this . _typingHintProvider . getHints ( searchResult ) ;
162145 if ( hints ) {
163146 for ( const hint of hints ) {
@@ -167,8 +150,8 @@ export class TypeHintProvider {
167150 }
168151 }
169152
170- private tryAddTypingHint ( typeName : string , typeHints : string [ ] ) {
171- if ( this . _typingImported ) {
153+ private tryAddTypingHint ( typeName : string , typeHints : string [ ] , typingImported : boolean ) {
154+ if ( typingImported ) {
172155 const typingHint = this . _typingHintProvider . getHint ( typeName ) ;
173156 if ( typingHint ) {
174157 this . add ( typingHint , typeHints ) ;
0 commit comments