Skip to content

Commit ac74383

Browse files
committed
fixed all warnings except explicit ones
mostly nullability annotations removed a little bit of dead code
1 parent 8754ed1 commit ac74383

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+164
-160
lines changed

src/runtime/Codecs/DecoderGroup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void Add(IPyObjectDecoder item)
3030
public bool CanDecode(PyType objectType, Type targetType)
3131
=> this.decoders.Any(decoder => decoder.CanDecode(objectType, targetType));
3232
/// <inheritdoc />
33-
public bool TryDecode<T>(PyObject pyObj, out T value)
33+
public bool TryDecode<T>(PyObject pyObj, out T? value)
3434
{
3535
if (pyObj is null) throw new ArgumentNullException(nameof(pyObj));
3636

@@ -65,7 +65,7 @@ public static class DecoderGroupExtensions
6565
/// that can decode from <paramref name="objectType"/> to <paramref name="targetType"/>,
6666
/// or <c>null</c> if a matching decoder can not be found.
6767
/// </summary>
68-
public static IPyObjectDecoder GetDecoder(
68+
public static IPyObjectDecoder? GetDecoder(
6969
this IPyObjectDecoder decoder,
7070
PyType objectType, Type targetType)
7171
{

src/runtime/Codecs/EncoderGroup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void Add(IPyObjectEncoder item)
2828
/// <inheritdoc />
2929
public bool CanEncode(Type type) => this.encoders.Any(encoder => encoder.CanEncode(type));
3030
/// <inheritdoc />
31-
public PyObject TryEncode(object value)
31+
public PyObject? TryEncode(object value)
3232
{
3333
if (value is null) throw new ArgumentNullException(nameof(value));
3434

src/runtime/CollectionWrappers/IterableWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public IEnumerator<T> GetEnumerator()
3535
iterObject.Dispose();
3636
break;
3737
}
38-
yield return iterObject.Current.As<T>();
38+
yield return iterObject.Current.As<T>()!;
3939
}
4040
}
4141
}

src/runtime/CollectionWrappers/ListWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public T this[int index]
1616
{
1717
var item = Runtime.PyList_GetItem(pyObject, index);
1818
var pyItem = new PyObject(item);
19-
return pyItem.As<T>();
19+
return pyItem.As<T>()!;
2020
}
2121
set
2222
{

src/runtime/CustomMarshaler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static ICustomMarshaler GetInstance(string cookie)
7474
return Instance;
7575
}
7676

77-
public static string PtrToStringUni(IntPtr p)
77+
public static string? PtrToStringUni(IntPtr p)
7878
{
7979
if (p == IntPtr.Zero)
8080
{
@@ -134,7 +134,7 @@ public static IntPtr Py3UnicodePy2StringtoPtr(string s)
134134
/// <returns>
135135
/// Managed String
136136
/// </returns>
137-
public static string PtrToPy3UnicodePy2String(IntPtr p)
137+
public static string? PtrToPy3UnicodePy2String(IntPtr p)
138138
{
139139
return PtrToStringUni(p);
140140
}
@@ -184,7 +184,7 @@ public override IntPtr MarshalManagedToNative(object managedObj)
184184
return mem;
185185
}
186186

187-
public static ICustomMarshaler GetInstance(string cookie)
187+
public static ICustomMarshaler GetInstance(string? cookie)
188188
{
189189
return Instance;
190190
}

src/runtime/Python.Runtime.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
</ItemGroup>
6161

6262
<ItemGroup>
63+
<PackageReference Include="Lost.Compat.NullabilityAttributes" Version="0.0.4" PrivateAssets="All" />
64+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
6365
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
64-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
6566
</ItemGroup>
6667
</Project>

src/runtime/ReflectedClrType.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ internal void Restore(InterDomainContext context)
5353
{
5454
var cb = context.Storage.GetValue<ClassBase>("impl");
5555

56-
cb.Load(this, context);
56+
Debug.Assert(cb is not null);
57+
58+
cb!.Load(this, context);
5759

5860
Restore(cb);
5961
}

src/runtime/StateSerialization/CLRWrapperCollection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System.Collections.ObjectModel;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace Python.Runtime;
45

56
public class CLRWrapperCollection : KeyedCollection<object, CLRMappedItem>
67
{
7-
public bool TryGetValue(object key, out CLRMappedItem value)
8+
public bool TryGetValue(object key, [NotNullWhen(true)] out CLRMappedItem? value)
89
{
910
if (Dictionary == null)
1011
{

src/runtime/StateSerialization/ClassManagerState.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
namespace Python.Runtime.StateSerialization;
55

6+
// Workaround for the lack of required properties: https://github.com/dotnet/csharplang/issues/3630
7+
// Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
8+
#pragma warning disable CS8618
9+
610
[Serializable]
711
internal class ClassManagerState
812
{

src/runtime/StateSerialization/ImportHookState.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33

44
namespace Python.Runtime.StateSerialization;
55

6+
// Workaround for the lack of required properties: https://github.com/dotnet/csharplang/issues/3630
7+
// Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
8+
#pragma warning disable CS8618
9+
610
[Serializable]
711
internal class ImportHookState
812
{
9-
public PyModule PyCLRModule { get; set; }
10-
public PyObject Root { get; set; }
11-
public Dictionary<PyString, PyObject> Modules { get; set; }
13+
public PyModule PyCLRModule { get; init; }
14+
public PyObject Root { get; init; }
15+
public Dictionary<PyString, PyObject> Modules { get; init; }
1216
}

src/runtime/StateSerialization/MaybeMemberInfo.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ namespace Python.Runtime
77
[Serializable]
88
internal struct MaybeMemberInfo<T> : ISerializable where T : MemberInfo
99
{
10-
public static implicit operator MaybeMemberInfo<T>(T ob) => new MaybeMemberInfo<T>(ob);
11-
1210
// .ToString() of the serialized object
1311
const string SerializationDescription = "d";
1412
// The ReflectedType of the object
@@ -50,8 +48,8 @@ public override string ToString()
5048
public MaybeMemberInfo(T fi)
5149
{
5250
info = fi;
53-
Description = info?.ToString();
54-
if (info?.DeclaringType is not null)
51+
Description = info.ToString();
52+
if (info.DeclaringType is not null)
5553
Description += " of " + info.DeclaringType;
5654
deserializationException = null;
5755
}

src/runtime/StateSerialization/MaybeMethodBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Reflection;
34
using System.Runtime.Serialization;
45
using System.Linq;
@@ -21,7 +22,7 @@ internal struct MaybeMethodBase<T> : ISerializable where T: MethodBase
2122

2223
public static implicit operator MaybeMethodBase<T> (T? ob) => new (ob);
2324

24-
string name;
25+
string? name;
2526
MethodBase? info;
2627

2728
[NonSerialized]
@@ -48,7 +49,8 @@ public T Value
4849
}
4950

5051
public T UnsafeValue => (T)info!;
51-
public string Name {get{return name;}}
52+
public string? Name => name;
53+
[MemberNotNullWhen(true, nameof(info))]
5254
public bool Valid => info != null;
5355

5456
public override string ToString()

src/runtime/StateSerialization/MetatypeState.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Python.Runtime.StateSerialization;
44

5+
// Workaround for the lack of required properties: https://github.com/dotnet/csharplang/issues/3630
6+
// Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
7+
#pragma warning disable CS8618
8+
59
[Serializable]
610
internal class MetatypeState
711
{

src/runtime/StateSerialization/PythonNetState.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
namespace Python.Runtime.StateSerialization;
44

5+
// Workaround for the lack of required properties: https://github.com/dotnet/csharplang/issues/3630
6+
// Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
7+
#pragma warning disable CS8618
8+
59
[Serializable]
610
internal class PythonNetState
711
{
8-
public MetatypeState Metatype { get; set; }
9-
public SharedObjectsState SharedObjects { get; set; }
10-
public TypeManagerState Types { get; set; }
11-
public ClassManagerState Classes { get; set; }
12-
public ImportHookState ImportHookState { get; set; }
12+
public MetatypeState Metatype { get; init; }
13+
public SharedObjectsState SharedObjects { get; init; }
14+
public TypeManagerState Types { get; init; }
15+
public ClassManagerState Classes { get; init; }
16+
public ImportHookState ImportHookState { get; init; }
1317
}

src/runtime/StateSerialization/SharedObjectsState.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33

44
namespace Python.Runtime.StateSerialization;
55

6+
// Workaround for the lack of required properties: https://github.com/dotnet/csharplang/issues/3630
7+
// Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
8+
#pragma warning disable CS8618
9+
610
[Serializable]
711
internal class SharedObjectsState
812
{
9-
public Dictionary<PyObject, CLRObject> InternalStores { get; set; }
10-
public Dictionary<PyObject, ExtensionType> Extensions { get; set; }
11-
public RuntimeDataStorage Wrappers { get; set; }
12-
public Dictionary<PyObject, InterDomainContext> Contexts { get; set; }
13+
public Dictionary<PyObject, CLRObject> InternalStores { get; init; }
14+
public Dictionary<PyObject, ExtensionType> Extensions { get; init; }
15+
public RuntimeDataStorage Wrappers { get; init; }
16+
public Dictionary<PyObject, InterDomainContext> Contexts { get; init; }
1317
}

src/runtime/StateSerialization/TypeManagerState.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
namespace Python.Runtime.StateSerialization;
55

6+
// Workaround for the lack of required properties: https://github.com/dotnet/csharplang/issues/3630
7+
// Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
8+
#pragma warning disable CS8618
9+
610
[Serializable]
711
internal class TypeManagerState
812
{

src/runtime/Util.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,6 @@ internal static void WriteCLong(BorrowedReference type, int offset, Int64 value)
119119
}
120120
}
121121

122-
/// <summary>
123-
/// Null-coalesce: if <paramref name="primary"/> parameter is not
124-
/// <see cref="IntPtr.Zero"/>, return it. Otherwise return <paramref name="fallback"/>.
125-
/// </summary>
126-
internal static IntPtr Coalesce(this IntPtr primary, IntPtr fallback)
127-
=> primary == IntPtr.Zero ? fallback : primary;
128-
129122
/// <summary>
130123
/// Gets substring after last occurrence of <paramref name="symbol"/>
131124
/// </summary>
@@ -149,5 +142,14 @@ internal static string ReadStringResource(this System.Reflection.Assembly assemb
149142
}
150143

151144
public static IEnumerator<T> GetEnumerator<T>(this IEnumerator<T> enumerator) => enumerator;
145+
146+
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> source)
147+
where T: class
148+
{
149+
foreach (var item in source)
150+
{
151+
if (item is not null) yield return item;
152+
}
153+
}
152154
}
153155
}

src/runtime/assemblymanager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public static List<string> GetNames(string nsname)
358358
//Dictionary<string, int> seen = new Dictionary<string, int>();
359359
var names = new List<string>(8);
360360

361-
List<string> g = GenericUtil.GetGenericBaseNames(nsname);
361+
List<string>? g = GenericUtil.GetGenericBaseNames(nsname);
362362
if (g != null)
363363
{
364364
foreach (string n in g)

src/runtime/classbase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public virtual NewReference type_subscript(BorrowedReference idx)
6767
return Exceptions.RaiseTypeError(type.DeletedMessage);
6868
}
6969

70-
Type target = GenericUtil.GenericForType(type.Value, types.Length);
70+
Type? target = GenericUtil.GenericForType(type.Value, types.Length);
7171

7272
if (target != null)
7373
{
@@ -396,7 +396,7 @@ protected override void OnSave(BorrowedReference ob, InterDomainContext context)
396396
context.Storage.AddValue("impl", this);
397397
}
398398

399-
protected override void OnLoad(BorrowedReference ob, InterDomainContext context)
399+
protected override void OnLoad(BorrowedReference ob, InterDomainContext? context)
400400
{
401401
base.OnLoad(ob, context);
402402
var gcHandle = GCHandle.Alloc(this);

src/runtime/classderived.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ internal static NewReference ToPython(IPythonDerivedType obj)
144144
internal static Type CreateDerivedType(string name,
145145
Type baseType,
146146
BorrowedReference py_dict,
147-
string namespaceStr,
147+
string? namespaceStr,
148148
string? assemblyName,
149149
string moduleName = "Python.Runtime.Dynamic.dll")
150150
{
@@ -669,7 +669,7 @@ public class PythonDerivedType
669669
/// method binding (i.e. it has been overridden in the derived python
670670
/// class) it calls it, otherwise it calls the base method.
671671
/// </summary>
672-
public static T InvokeMethod<T>(IPythonDerivedType obj, string methodName, string origMethodName, object[] args)
672+
public static T? InvokeMethod<T>(IPythonDerivedType obj, string methodName, string origMethodName, object[] args)
673673
{
674674
var self = GetPyObj(obj);
675675

@@ -776,7 +776,7 @@ public static void InvokeMethodVoid(IPythonDerivedType obj, string methodName, s
776776
args);
777777
}
778778

779-
public static T InvokeGetProperty<T>(IPythonDerivedType obj, string propertyName)
779+
public static T? InvokeGetProperty<T>(IPythonDerivedType obj, string propertyName)
780780
{
781781
var self = GetPyObj(obj);
782782

src/runtime/clrobject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ internal static void Restore(object ob, BorrowedReference pyHandle, InterDomainC
5959
co.OnLoad(pyHandle, context);
6060
}
6161

62-
protected override void OnLoad(BorrowedReference ob, InterDomainContext context)
62+
protected override void OnLoad(BorrowedReference ob, InterDomainContext? context)
6363
{
6464
base.OnLoad(ob, context);
6565
GCHandle gc = GCHandle.Alloc(this);

src/runtime/converterextensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ internal static bool TryDecode(BorrowedReference pyHandle, BorrowedReference pyT
126126
Debug.Assert(PyType.IsType(sourceTypeRef));
127127
var pyType = new PyType(sourceTypeRef, prevalidated: true);
128128

129-
IPyObjectDecoder decoder;
129+
IPyObjectDecoder? decoder;
130130
lock (decoders)
131131
{
132132
decoder = decoders.GetDecoder(pyType, targetType);

src/runtime/extensiontype.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static int tp_clear(BorrowedReference ob)
9595
return res;
9696
}
9797

98-
protected override void OnLoad(BorrowedReference ob, InterDomainContext context)
98+
protected override void OnLoad(BorrowedReference ob, InterDomainContext? context)
9999
{
100100
base.OnLoad(ob, context);
101101
SetupGc(ob, Runtime.PyObject_TYPE(ob));

src/runtime/fieldobject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class FieldObject : ExtensionType
1414

1515
public FieldObject(FieldInfo info)
1616
{
17-
this.info = info;
17+
this.info = new MaybeFieldInfo(info);
1818
}
1919

2020
/// <summary>

0 commit comments

Comments
 (0)