@@ -25,15 +25,13 @@ static void TupleConversionsGeneric<T, TTuple>()
2525 TupleCodec < TTuple > . Register ( ) ;
2626 var tuple = Activator . CreateInstance ( typeof ( T ) , 42 , "42" , new object ( ) ) ;
2727 T restored = default ;
28- using ( var scope = Py . CreateScope ( ) )
29- {
30- void Accept ( T value ) => restored = value ;
31- using var accept = new Action < T > ( Accept ) . ToPython ( ) ;
32- scope . Set ( nameof ( tuple ) , tuple ) ;
33- scope . Set ( nameof ( accept ) , accept ) ;
34- scope . Exec ( $ "{ nameof ( accept ) } ({ nameof ( tuple ) } )") ;
35- Assert . That ( actual : restored , Is . EqualTo ( expected : tuple ) ) ;
36- }
28+ using var scope = Py . CreateScope ( ) ;
29+ void Accept ( T value ) => restored = value ;
30+ using var accept = new Action < T > ( Accept ) . ToPython ( ) ;
31+ scope . Set ( nameof ( tuple ) , tuple ) ;
32+ scope . Set ( nameof ( accept ) , accept ) ;
33+ scope . Exec ( $ "{ nameof ( accept ) } ({ nameof ( tuple ) } )") ;
34+ Assert . That ( actual : restored , Is . EqualTo ( expected : tuple ) ) ;
3735 }
3836
3937 [ Test ]
@@ -46,15 +44,13 @@ static void TupleConversionsObject<T, TTuple>()
4644 TupleCodec < TTuple > . Register ( ) ;
4745 var tuple = Activator . CreateInstance ( typeof ( T ) , 42.0 , "42" , new object ( ) ) ;
4846 T restored = default ;
49- using ( var scope = Py . CreateScope ( ) )
50- {
51- void Accept ( object value ) => restored = ( T ) value ;
52- using var accept = new Action < object > ( Accept ) . ToPython ( ) ;
53- scope . Set ( nameof ( tuple ) , tuple ) ;
54- scope . Set ( nameof ( accept ) , accept ) ;
55- scope . Exec ( $ "{ nameof ( accept ) } ({ nameof ( tuple ) } )") ;
56- Assert . That ( actual : restored , Is . EqualTo ( expected : tuple ) ) ;
57- }
47+ using var scope = Py . CreateScope ( ) ;
48+ void Accept ( object value ) => restored = ( T ) value ;
49+ using var accept = new Action < object > ( Accept ) . ToPython ( ) ;
50+ scope . Set ( nameof ( tuple ) , tuple ) ;
51+ scope . Set ( nameof ( accept ) , accept ) ;
52+ scope . Exec ( $ "{ nameof ( accept ) } ({ nameof ( tuple ) } )") ;
53+ Assert . That ( actual : restored , Is . EqualTo ( expected : tuple ) ) ;
5854 }
5955
6056 [ Test ]
@@ -92,7 +88,7 @@ public void ListDecoderTest()
9288 var codec = ListDecoder . Instance ;
9389 var items = new List < PyObject > ( ) { new PyInt ( 1 ) , new PyInt ( 2 ) , new PyInt ( 3 ) } ;
9490
95- using var pyList = new PyList ( items . ToArray ( ) ) ;
91+ using var pyList = new PyList ( [ .. items ] ) ;
9692
9793 using var pyListType = pyList . GetPythonType ( ) ;
9894 Assert . IsTrue ( codec . CanDecode ( pyListType , typeof ( IList < bool > ) ) ) ;
@@ -119,7 +115,7 @@ public void ListDecoderTest()
119115 IList < string > stringList = null ;
120116 Assert . DoesNotThrow ( ( ) => { codec . TryDecode ( pyList , out stringList ) ; } ) ;
121117 Assert . That ( 3 , Is . EqualTo ( stringList . Count ) ) ;
122- Assert . Throws ( typeof ( InvalidCastException ) , ( ) => { var x = stringList [ 0 ] ; } ) ;
118+ Assert . Throws < InvalidCastException > ( ( ) => { var x = stringList [ 0 ] ; } ) ;
123119
124120 //can't convert python iterable to list (this will require a copy which isn't lossless)
125121 using var foo = GetPythonIterable ( ) ;
@@ -134,7 +130,7 @@ public void SequenceDecoderTest()
134130 var items = new List < PyObject > ( ) { new PyInt ( 1 ) , new PyInt ( 2 ) , new PyInt ( 3 ) } ;
135131
136132 //SequenceConverter can only convert to any ICollection
137- using var pyList = new PyList ( items . ToArray ( ) ) ;
133+ using var pyList = new PyList ( [ .. items ] ) ;
138134 using var listType = pyList . GetPythonType ( ) ;
139135 //it can convert a PyList, since PyList satisfies the python sequence protocol
140136
@@ -162,7 +158,7 @@ public void SequenceDecoderTest()
162158 //the IList will report a Count of 3.
163159 ICollection < string > stringCollection = null ;
164160 Assert . DoesNotThrow ( ( ) => { codec . TryDecode ( pyList , out stringCollection ) ; } ) ;
165- Assert . That ( stringCollection . Count ( ) , Is . EqualTo ( 3 ) ) ;
161+ Assert . That ( stringCollection . Count , Is . EqualTo ( 3 ) ) ;
166162 Assert . Throws < InvalidCastException > ( ( ) => {
167163 string [ ] array = new string [ 3 ] ;
168164 stringCollection . CopyTo ( array , 0 ) ;
@@ -177,7 +173,7 @@ public void SequenceDecoderTest()
177173 Assert . IsFalse ( codec . CanDecode ( fooType , typeof ( ICollection < int > ) ) ) ;
178174
179175 //python tuples do satisfy the python sequence protocol
180- var pyTuple = new PyTuple ( items . ToArray ( ) ) ;
176+ var pyTuple = new PyTuple ( [ .. items ] ) ;
181177 var pyTupleType = pyTuple . GetPythonType ( ) ;
182178
183179 Assert . IsTrue ( codec . CanDecode ( pyTupleType , typeof ( ICollection < float > ) ) ) ;
@@ -215,7 +211,7 @@ public void IterableDecoderTest()
215211 var codec = IterableDecoder . Instance ;
216212 var items = new List < PyObject > ( ) { new PyInt ( 1 ) , new PyInt ( 2 ) , new PyInt ( 3 ) } ;
217213
218- var pyList = new PyList ( items . ToArray ( ) ) ;
214+ var pyList = new PyList ( [ .. items ] ) ;
219215 var pyListType = pyList . GetPythonType ( ) ;
220216 Assert . IsFalse ( codec . CanDecode ( pyListType , typeof ( IList < bool > ) ) ) ;
221217 Assert . IsTrue ( codec . CanDecode ( pyListType , typeof ( System . Collections . IEnumerable ) ) ) ;
@@ -249,13 +245,13 @@ public void IterableDecoderTest()
249245 IEnumerable < string > stringEnumerable = null ;
250246 Assert . DoesNotThrow ( ( ) => { codec . TryDecode ( pyList , out stringEnumerable ) ; } ) ;
251247
252- Assert . Throws ( typeof ( InvalidCastException ) , ( ) => {
248+ Assert . Throws < InvalidCastException > ( ( ) => {
253249 foreach ( string item in stringEnumerable )
254250 {
255251 var x = item ;
256252 }
257253 } ) ;
258- Assert . Throws ( typeof ( InvalidCastException ) , ( ) => {
254+ Assert . Throws < InvalidCastException > ( ( ) => {
259255 stringEnumerable . Count ( ) ;
260256 } ) ;
261257
@@ -309,7 +305,7 @@ def CanEncode(self, clr_type):
309305 public void ExceptionEncoded ( )
310306 {
311307 PyObjectConversions . RegisterEncoder ( new ValueErrorCodec ( ) ) ;
312- void CallMe ( ) => throw new ValueErrorWrapper ( TestExceptionMessage ) ;
308+ static void CallMe ( ) => throw new ValueErrorWrapper ( TestExceptionMessage ) ;
313309 var callMeAction = new Action ( CallMe ) ;
314310 using var scope = Py . CreateScope ( ) ;
315311 scope . Exec ( @"
@@ -385,7 +381,9 @@ public void ExceptionDecodedNoInstance()
385381 }
386382 }
387383
384+ #pragma warning disable IDE0060 // Remove unused parameter
388385 public static void AcceptsDateTime ( DateTime v ) { }
386+ #pragma warning restore IDE0060 // Remove unused parameter
389387
390388 [ Test ]
391389 public void As_Object_AffectedByDecoders ( )
@@ -412,15 +410,14 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
412410 }
413411 }
414412
415- class ValueErrorWrapper : Exception
413+ class ValueErrorWrapper ( string message ) : Exception ( message )
416414 {
417- public ValueErrorWrapper ( string message ) : base ( message ) { }
418415 }
419416
420417 class ValueErrorCodec : IPyObjectEncoder , IPyObjectDecoder
421418 {
422419 public bool CanDecode ( PyType objectType , Type targetType )
423- => this . CanEncode ( targetType )
420+ => CanEncode ( targetType )
424421 && PythonReferenceComparer . Instance . Equals ( objectType , PythonEngine . Eval ( "ValueError" ) ) ;
425422
426423 public bool CanEncode ( Type type ) => type == typeof ( ValueErrorWrapper )
@@ -480,24 +477,18 @@ class ObjectToEncoderInstanceEncoder<T> : IPyObjectEncoder
480477 /// Decodes object of specified Python type to the predefined value <see cref="DecodeResult"/>
481478 /// </summary>
482479 /// <typeparam name="TTarget">Type of the <see cref="DecodeResult"/></typeparam>
483- class DecoderReturningPredefinedValue < TTarget > : IPyObjectDecoder
480+ class DecoderReturningPredefinedValue < TTarget > ( PyObject objectType , TTarget decodeResult ) : IPyObjectDecoder
484481 {
485- public PyObject TheOnlySupportedSourceType { get ; }
486- public TTarget DecodeResult { get ; }
487-
488- public DecoderReturningPredefinedValue ( PyObject objectType , TTarget decodeResult )
489- {
490- this . TheOnlySupportedSourceType = objectType ;
491- this . DecodeResult = decodeResult ;
492- }
482+ public PyObject TheOnlySupportedSourceType { get ; } = objectType ;
483+ public TTarget DecodeResult { get ; } = decodeResult ;
493484
494485 public bool CanDecode ( PyType objectType , Type targetType )
495486 => PythonReferenceComparer . Instance . Equals ( objectType , TheOnlySupportedSourceType )
496487 && targetType == typeof ( TTarget ) ;
497488 public bool TryDecode < T > ( PyObject pyObj , out T value )
498489 {
499490 if ( typeof ( T ) != typeof ( TTarget ) )
500- throw new ArgumentException ( nameof ( T ) ) ;
491+ throw new ArgumentException ( null , nameof ( T ) ) ;
501492 value = ( T ) ( object ) DecodeResult ;
502493 return true ;
503494 }
0 commit comments