This repository was archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
Make sure we do not try and create specific type for non-generics #1316
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,8 @@ namespace Microsoft.Python.Analysis.Types { | |
| [DebuggerDisplay("Class {Name}")] | ||
| internal class PythonClassType : PythonType, IPythonClassType, IPythonTemplateType, IEquatable<IPythonClassType> { | ||
| private static readonly string[] _classMethods = { "mro", "__dict__", @"__weakref__" }; | ||
|
|
||
| private Dictionary<string, PythonClassType> _specificTypeCache; | ||
| private IPythonClassType _processing; | ||
| private List<IPythonType> _bases; | ||
| private IReadOnlyList<IPythonType> _mro; | ||
|
|
@@ -350,19 +352,20 @@ public IPythonType CreateSpecificType(IArgumentSet args) { | |
| } | ||
|
|
||
| // For still undefined parameters try matching passed types in order | ||
| for (var i = 0; i < args.Arguments.Count; i++) { | ||
| for (int i = 0, gtIndex = 0; i < args.Arguments.Count; i++) { | ||
| var arg = args.Arguments[i]; | ||
| if (Equals(arg.Type)) { | ||
| continue; | ||
| continue; // Typically 'self'. | ||
| } | ||
|
|
||
| if (arg.Value is IMember member) { | ||
| var type = member.GetPythonType(); | ||
| if (!type.IsUnknown()) { | ||
| var gtd = i < genericTypeDefinitions.Count ? genericTypeDefinitions[i] : null; | ||
| var gtd = gtIndex < genericTypeDefinitions.Count ? genericTypeDefinitions[gtIndex] : null; | ||
| if (gtd != null && !specificClassTypeParameters.ContainsKey(gtd.Name)) { | ||
| specificClassTypeParameters[gtd.Name] = type; | ||
| } | ||
| gtIndex++; | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -374,7 +377,11 @@ public IPythonType CreateSpecificType(IArgumentSet args) { | |
| .ToArray(); | ||
|
|
||
| var specificName = CodeFormatter.FormatSequence(Name, '[', specificTypes); | ||
| var classType = new PythonClassType(specificName, new Location(DeclaringModule)); | ||
| _specificTypeCache = _specificTypeCache ?? new Dictionary<string, PythonClassType>(); | ||
| if (_specificTypeCache.TryGetValue(specificName, out var classType)) { | ||
| return classType; | ||
| } | ||
| _specificTypeCache[specificName] = classType = new PythonClassType(specificName, new Location(DeclaringModule)); | ||
|
|
||
| // Methods returning generic types need to know how to match generic | ||
| // parameter name to the actual supplied type. | ||
|
|
@@ -487,7 +494,7 @@ private void SetClassMembers(PythonClassType classType, IArgumentSet args) { | |
| // Functions handle generics internally upon the call to Call. | ||
| foreach (var m in members) { | ||
| switch (m.Value) { | ||
| case IPythonTemplateType tt: { | ||
| case IPythonTemplateType tt when tt.IsGeneric(): { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
| var specificType = tt.CreateSpecificType(args); | ||
| classType.AddMember(m.Key, specificType, true); | ||
| break; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parameter index does not always equal generic parameter index (consider
self)