Skip to content

Commit 7840403

Browse files
authored
Handle generic parameter types as return types of builtins in DocBuilder (#1046)
* Handle generic parameter types as return types of builtins in DocBuilder * Fix handling of by-refs of generic types * Enable nullable annotations in DynamicHelpers * Undo changes to PythonType * Support generic parameter types by PythonType
1 parent 59269e8 commit 7840403

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

Src/IronPython/Runtime/Types/DocBuilder.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public static OverloadDoc GetOverloadDoc(MethodBase info, string name, int endPa
305305
paramDoc.Add(
306306
new ParameterDoc(
307307
pi.Name ?? "", // manufactured methods, such as string[].ctor(int) can have no parameter names.
308-
pi.ParameterType.IsGenericParameter ? pi.ParameterType.Name : GetPythonTypeName(pi.ParameterType),
308+
GetPythonTypeName(pi.ParameterType),
309309
paramDocString,
310310
flags
311311
)
@@ -402,6 +402,10 @@ private static string GetPythonTypeName(Type type) {
402402
type = type.GetElementType();
403403
}
404404

405+
if (type.IsGenericParameter) {
406+
return type.Name;
407+
}
408+
405409
return DynamicHelpers.GetPythonTypeFromType(type).Name;
406410
}
407411

Src/IronPython/Runtime/Types/DynamicHelpers.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using System;
8+
69
using Microsoft.Scripting;
710
using Microsoft.Scripting.Generation;
811
using Microsoft.Scripting.Utils;
@@ -17,14 +20,14 @@ public static class DynamicHelpers {
1720
return PythonType.GetPythonType(type);
1821
}
1922

20-
public static PythonType GetPythonType(object o) {
23+
public static PythonType GetPythonType(object? o) {
2124
if (o is IPythonObject dt) return dt.PythonType;
2225

2326
return GetPythonTypeFromType(CompilerHelpers.GetType(o));
2427
}
2528

26-
public static ReflectedEvent.BoundEvent MakeBoundEvent(ReflectedEvent eventObj, object instance, Type type) {
27-
return new ReflectedEvent.BoundEvent(eventObj, instance, DynamicHelpers.GetPythonTypeFromType(type));
29+
public static ReflectedEvent.BoundEvent MakeBoundEvent(ReflectedEvent eventObj, object? instance, Type type) {
30+
return new ReflectedEvent.BoundEvent(eventObj, instance, GetPythonTypeFromType(type));
2831
}
2932
}
3033
}

Src/IronPython/Runtime/Types/PythonType.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ internal IList<PythonType> ResolutionOrder {
764764
/// <summary>
765765
/// Gets the dynamic type that corresponds with the provided static type.
766766
///
767-
/// Returns null if no type is available. TODO: In the future this will
767+
/// TODO: In the future this will
768768
/// always return a PythonType created by the DLR.
769769
/// </summary>
770770
/// <param name="type"></param>
@@ -777,7 +777,7 @@ internal IList<PythonType> ResolutionOrder {
777777
if (!_pythonTypes.TryGetValue(type, out res)) {
778778
res = new PythonType(type);
779779

780-
_pythonTypes.Add(type, res);
780+
_pythonTypes.Add(type, res);
781781
}
782782
}
783783
}
@@ -2325,7 +2325,17 @@ private void AddSystemInterfaces(List<PythonType> mro) {
23252325
lock (_bases) {
23262326
_bases = bases;
23272327
}
2328-
2328+
2329+
if (_underlyingSystemType.IsGenericParameter) {
2330+
// add all interfaces into the MRO
2331+
foreach (Type t in interfaces) {
2332+
Debug.Assert(t.IsInterface);
2333+
2334+
mro.Add(DynamicHelpers.GetPythonTypeFromType(t));
2335+
}
2336+
return; // end of story for generic parameter types
2337+
}
2338+
23292339
foreach (Type iface in interfaces) {
23302340
#if NET46
23312341
// causes failures on Mono: https://github.com/mono/mono/issues/14712

0 commit comments

Comments
 (0)