Skip to content

Commit e47fcbe

Browse files
authored
Enable nullable on some modules (#1901)
* Enable nullable on some modules * Remove unnecessary NotNone attributes * Add more nullable annotations * Add NotNone to params * Fix failing test * Cleanup * Add NotNone to params
1 parent 65a7afc commit e47fcbe

Some content is hidden

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

78 files changed

+528
-510
lines changed

eng/scripts/generate_set.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def simple_op_worker(cw, t, arg_t, name):
6464
)
6565

6666
def enter_multiarg_op(cw, t, name):
67-
cw.enter_block('public %s %s([NotNone] params object[]/*!*/ sets)' % (t, name))
67+
cw.enter_block('public %s %s([NotNone] params object[] sets)' % (t, name))
6868

6969
def union_multiarg(cw, mutable):
7070
t = get_type(mutable)

src/core/IronPython.Modules/IterTools.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public class chain : IterBase {
142142

143143
private chain() { }
144144

145-
public chain(params object[] iterables) {
145+
public chain([NotNone] params object[] iterables) {
146146
SetInnerEnumerator(PythonTuple.MakeTuple(iterables));
147147
}
148148

@@ -667,15 +667,15 @@ public class zip_longest : IEnumerator {
667667
private readonly object _fill;
668668
private PythonTuple _current;
669669

670-
public zip_longest(params object[] iterables) {
670+
public zip_longest([NotNone] params object[] iterables) {
671671
_iters = new IEnumerator[iterables.Length];
672672

673673
for (int i = 0; i < iterables.Length; i++) {
674674
_iters[i] = PythonOps.GetEnumerator(iterables[i]);
675675
}
676676
}
677677

678-
public zip_longest([ParamDictionary] IDictionary<object, object> paramDict, params object[] iterables) {
678+
public zip_longest([ParamDictionary] IDictionary<object, object> paramDict, [NotNone] params object[] iterables) {
679679
object fill;
680680

681681
if (paramDict.TryGetValue("fillvalue", out fill)) {
@@ -762,12 +762,12 @@ private static Exception UnexpectedKeywordArgument(IDictionary<object, object> p
762762
public class product : IterBase {
763763
private PythonTuple[] tuples;
764764

765-
public product(CodeContext context, params object[] iterables) {
765+
public product(CodeContext context, [NotNone] params object[] iterables) {
766766
tuples = ArrayUtils.ConvertAll(iterables, x => new PythonTuple(PythonOps.GetEnumerator(x)));
767767
InnerEnumerator = Yielder(tuples);
768768
}
769769

770-
public product(CodeContext context, [ParamDictionary] IDictionary<object, object> paramDict, params object[] iterables) {
770+
public product(CodeContext context, [ParamDictionary] IDictionary<object, object> paramDict, [NotNone] params object[] iterables) {
771771
object repeat;
772772
int iRepeat = 1;
773773
if (paramDict.TryGetValue("repeat", out repeat)) {

src/core/IronPython.Modules/ResourceMetaPathImporter.cs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
15
using System;
26
using System.Collections.Generic;
37
using System.IO;
48
using System.Linq;
59
using System.Reflection;
10+
611
using IronPython.Runtime;
712
using IronPython.Runtime.Exceptions;
813
using IronPython.Zlib;
14+
915
using Microsoft.Scripting;
1016
using Microsoft.Scripting.Runtime;
1117

@@ -68,7 +74,7 @@ fully qualified (dotted) module name. It returns the importer
6874
The optional 'path' argument is ignored -- it's there for compatibility
6975
with the importer protocol."
7076
)]
71-
public object find_module(CodeContext /*!*/ context, string fullname, params object[] args) {
77+
public object find_module(CodeContext /*!*/ context, string fullname, [NotNone] params object[] args) {
7278
var packedName = MakeFilename(fullname);
7379

7480
foreach (var entry in SearchOrder) {
@@ -131,8 +137,7 @@ public object load_module(CodeContext /*!*/ context, string fullname) {
131137
modules.Add(fullname, mod);
132138
try {
133139
script.Run(mod.Scope);
134-
}
135-
catch (Exception) {
140+
} catch (Exception) {
136141
modules.Remove(fullname);
137142
throw;
138143
}
@@ -175,8 +180,7 @@ private byte[] GetCodeFromData(CodeContext /*!*/ context, bool isbytecode, Packe
175180
if (data != null) {
176181
if (isbytecode) {
177182
// would put in code to unmarshal the bytecode here...
178-
}
179-
else {
183+
} else {
180184
code = data;
181185
}
182186
}
@@ -221,8 +225,8 @@ public static PackedResourceInfo Create(string fullName, int compress,
221225
#if DEBUG
222226
public override string ToString() {
223227
var sizeDesc = String.Format("{0} bytes", _fileSize);
224-
if (Convert.ToDouble(_fileSize)/1024.0 > 1.0)
225-
sizeDesc = String.Format("{0} KB", Math.Round(Convert.ToDouble(_fileSize)/1024.0, 1));
228+
if (Convert.ToDouble(_fileSize) / 1024.0 > 1.0)
229+
sizeDesc = String.Format("{0} KB", Math.Round(Convert.ToDouble(_fileSize) / 1024.0, 1));
226230
return String.Format("{0} ({1})", FullName, sizeDesc);
227231
}
228232
#endif
@@ -259,26 +263,25 @@ where isPyFile
259263
let path = lineage.Take(lineage.Length - 1).ToArray()
260264
orderby fileName
261265
select new {
262-
name = fileName,
263-
path,
264-
dottedPath = String.Join(".", path),
265-
entry
266-
};
266+
name = fileName,
267+
path,
268+
dottedPath = String.Join(".", path),
269+
entry
270+
};
267271
var moduleContents =
268272
from source in parsedSources
269273
orderby source.dottedPath
270274
group source by source.dottedPath
271275
into moduleGroup
272276
select new {
273-
moduleGroup.Key,
274-
Items = moduleGroup.Select(item => item.entry).ToArray()
275-
};
277+
moduleGroup.Key,
278+
Items = moduleGroup.Select(item => item.entry).ToArray()
279+
};
276280
modules = moduleContents.ToDictionary(
277281
moduleGroup => moduleGroup.Key,
278282
moduleGroup => moduleGroup.Items);
279283
return true;
280-
}
281-
catch (Exception exception) {
284+
} catch (Exception exception) {
282285
files = null;
283286
modules = null;
284287
unpackingError = String.Format("{0}: {1}", exception.GetType().Name, exception.Message);
@@ -310,7 +313,7 @@ private bool ReadZipDirectory(out IDictionary<string, PackedResourceInfo> result
310313
var endofCentralDir = new byte[22];
311314

312315
reader.BaseStream.Seek(-22, SeekOrigin.End);
313-
var headerPosition = (int) reader.BaseStream.Position;
316+
var headerPosition = (int)reader.BaseStream.Position;
314317
if (reader.Read(endofCentralDir, 0, 22) != 22) {
315318
unpackingError = "Can't read ZIP resource: Invalid ZIP Directory.";
316319
return false;
@@ -332,8 +335,7 @@ private bool ReadZipDirectory(out IDictionary<string, PackedResourceInfo> result
332335
.ToDictionary(entry => entry.FullName);
333336
return true;
334337
}
335-
}
336-
catch (Exception exception) {
338+
} catch (Exception exception) {
337339
unpackingError = String.Format("{0}: {1}", exception.GetType().Name, exception.Message);
338340
return false;
339341
}
@@ -410,26 +412,23 @@ public bool GetData(PackedResourceInfo tocEntry, out byte[] result, out string u
410412
byte[] rawData;
411413
try {
412414
rawData = reader.ReadBytes(compress == 0 ? dataSize : dataSize + 1);
413-
}
414-
catch {
415+
} catch {
415416
unpackingError = "Can't read data";
416417
return false;
417418
}
418419

419420
if (compress != 0) {
420-
rawData[dataSize] = (byte) 'Z';
421+
rawData[dataSize] = (byte)'Z';
421422
}
422423

423424
result = compress == 0 ? rawData : ZlibModule.Decompress(rawData, -15);
424425
return true;
425426
}
426-
}
427-
catch (Exception exception) {
427+
} catch (Exception exception) {
428428
unpackingError = String.Format("{0}: {1}", exception.GetType().Name, exception.Message);
429429
return false;
430430
}
431431
}
432432
}
433-
434433
}
435434
}

src/core/IronPython.Modules/SimpleSignalState.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using IronPython.Runtime;
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#nullable enable
66

77
#if FEATURE_PROCESS
88

9+
using System;
10+
11+
using IronPython.Runtime;
12+
913
namespace IronPython.Modules {
1014
public static partial class PythonSignal {
1115
internal class SimpleSignalState : PythonSignalState {
@@ -14,9 +18,9 @@ public SimpleSignalState(PythonContext pc)
1418
Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
1519
}
1620

17-
private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) {
21+
private void Console_CancelKeyPress(object? sender, ConsoleCancelEventArgs e) {
1822
int pySignal;
19-
switch(e.SpecialKey) {
23+
switch (e.SpecialKey) {
2024
case ConsoleSpecialKey.ControlC:
2125
pySignal = SIGINT;
2226
break;
@@ -28,7 +32,7 @@ private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) {
2832
default:
2933
throw new InvalidOperationException("unreachable");
3034
}
31-
35+
3236
lock (PySignalToPyHandler) {
3337
if (PySignalToPyHandler[pySignal].GetType() == typeof(int)) {
3438
int tempId = (int)PySignalToPyHandler[pySignal];

src/core/IronPython.Modules/_bisect.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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;
68
using System.Collections;
79
using System.Reflection;
@@ -29,9 +31,9 @@ common approach.
2931

3032
#region Private Implementation Details
3133

32-
private static int InternalBisectLeft(CodeContext/*!*/ context, PythonList list, object item, int lo, int hi) {
34+
private static int InternalBisectLeft(CodeContext/*!*/ context, PythonList list, object? item, int lo, int hi) {
3335
int mid;
34-
object litem;
36+
object? litem;
3537

3638
if (lo < 0) {
3739
throw PythonOps.ValueError("lo must be non-negative");
@@ -52,7 +54,7 @@ private static int InternalBisectLeft(CodeContext/*!*/ context, PythonList list,
5254
return lo;
5355
}
5456

55-
private static int InternalBisectLeft(CodeContext/*!*/ context, object list, object item, int lo, int hi) {
57+
private static int InternalBisectLeft(CodeContext/*!*/ context, object? list, object? item, int lo, int hi) {
5658
int mid;
5759
object litem;
5860

@@ -63,6 +65,7 @@ private static int InternalBisectLeft(CodeContext/*!*/ context, object list, obj
6365
if (hi == -1) {
6466
hi = PythonOps.Length(list);
6567
}
68+
6669
IComparer comparer = context.LanguageContext.GetLtComparer(GetComparisonType(context, list));
6770
while (lo < hi) {
6871
mid = (int)(((long)lo + hi) / 2);
@@ -75,8 +78,8 @@ private static int InternalBisectLeft(CodeContext/*!*/ context, object list, obj
7578
return lo;
7679
}
7780

78-
private static int InternalBisectRight(CodeContext/*!*/ context, PythonList list, object item, int lo, int hi) {
79-
object litem;
81+
private static int InternalBisectRight(CodeContext/*!*/ context, PythonList list, object? item, int lo, int hi) {
82+
object? litem;
8083
int mid;
8184

8285
if (lo < 0) {
@@ -98,7 +101,7 @@ private static int InternalBisectRight(CodeContext/*!*/ context, PythonList list
98101
return lo;
99102
}
100103

101-
private static int InternalBisectRight(CodeContext/*!*/ context, object list, object item, int lo, int hi) {
104+
private static int InternalBisectRight(CodeContext/*!*/ context, object list, object? item, int lo, int hi) {
102105
object litem;
103106
int mid;
104107

@@ -122,7 +125,7 @@ private static int InternalBisectRight(CodeContext/*!*/ context, object list, ob
122125
return lo;
123126
}
124127

125-
private static Type GetComparisonType(CodeContext/*!*/ context, object a) {
128+
private static Type GetComparisonType(CodeContext/*!*/ context, object? a) {
126129
if (PythonOps.Length(a) > 0) {
127130
// use the 1st index to determine the type - we're assuming lists are
128131
// homogeneous
@@ -183,7 +186,7 @@ before the leftmost x already there.
183186
Optional args lo (default 0) and hi (default len(a)) bound the
184187
slice of a to be searched.
185188
")]
186-
public static object bisect_left(CodeContext/*!*/ context, object a, object x, int lo = 0, int hi = -1) {
189+
public static object bisect_left(CodeContext/*!*/ context, object? a, object? x, int lo = 0, int hi = -1) {
187190
if (a is PythonList l && l.GetType() == typeof(PythonList)) {
188191
return InternalBisectLeft(context, l, x, lo, hi);
189192
}
@@ -201,7 +204,7 @@ public static object bisect_left(CodeContext/*!*/ context, object a, object x, i
201204
Optional args lo (default 0) and hi (default len(a)) bound the
202205
slice of a to be searched.
203206
")]
204-
public static void InsortRight(CodeContext/*!*/ context, object a, object x, int lo=0, int hi=-1) {
207+
public static void InsortRight(CodeContext/*!*/ context, object a, object x, int lo = 0, int hi = -1) {
205208
if (a is PythonList l && l.GetType() == typeof(PythonList)) {
206209
l.Insert(InternalBisectRight(context, l, x, lo, hi), x);
207210
return;
@@ -227,7 +230,7 @@ public static void InsortRight(CodeContext/*!*/ context, object a, object x, int
227230
Optional args lo (default 0) and hi (default len(a)) bound the
228231
slice of a to be searched.
229232
")]
230-
public static void insort_left(CodeContext/*!*/ context, object a, object x, int lo=0, int hi=-1) {
233+
public static void insort_left(CodeContext/*!*/ context, object? a, object? x, int lo = 0, int hi = -1) {
231234
if (a is PythonList l && l.GetType() == typeof(PythonList)) {
232235
l.Insert(InternalBisectLeft(context, l, x, lo, hi), x);
233236
return;

0 commit comments

Comments
 (0)